Wednesday, March 7, 2018

Getting Status Data from a Huawei E8372 USB Dongle

I am working on creating a remote solar-powered station that will report weather data from a location in Canada using a cellular connection. At the heart of it will be a Raspberry Pi Zero W running in headless mode. It connects to the weather station and then will send the data to the Weather Underground over the internet. The location is on a pretty remote island; it is just withing cell coverage, but with no commercial services at all, particularly no power. The location is occupied much of the time in summer, but during the winter - from about October until May there will be no one at the location at all.

This presents a number of problems for a system that has to function without any local interaction; there will be no one to power cycle the devices, look to see if any lights are lit, and there will be no one to push a reset button if that is needed. The entire system has to be setup so that it can do these things and act autonomously to recover from all sorts of situations that might arise. For instance, after the snow falls, the solar panel may get covered and stop producing power; weather may also cause it to lose connection to the cellular internet.

This means adding some smarts to the system to detect when these states might happen. To those ends, I have purchased a current and voltage monitor board, a solar charge controller, a real time clock module and a USB extension board for the Zero. For cellular connectivity, I will be using a link to the T-Mobile coverage in Canada - on the island it is limited to 3G, but since the data being transmitted is simple weather data, it should work fine.
There are a number of things that need to be taken care of so that the RPi does not get corrupted and most of those deal with monitoring the power supply and allowing the system to gracefully shutdown when required. All that will be the subject of another post. This post will deal with the fairly simple task of determining if the cellular connection is established.

The device that I purchased is a bit different, it is a USB modem, but it has some extra features. When powered up and configured correctly, it brings up a WiFi network that devices can connect to and access the internet through it's cellular interface. We could use that feature with the RPi W, but when the device is plugged into the USB port of the Pi, it creates a network interface (eth0) that is used by the Pi to reach the internet. It is a physical connection to the same LAN that the WiFi feature of the dongle offers to the WiFi clients. When the device is powered on in this mode (there are other modes) it offers a web interface to check status and configure it. That web interface can not really be used by a headless RPi, since it requires JavaScript, but it does use a number of API calls to provide the data to the web interface and we will use these to get our information. My main intent to to ask the device if it has been able to establish a connection to the internet using it's cellular modem yet. The code below accomplishes that task and the received data will be parsed to get the specific data I need.

Create a script in the pi that contains the following code. Do a chmod to make it executable and then run it to see the results.

#!/bin/sh

MODEM_IP="192.168.8.1"
curl -s -X GET "http://$MODEM_IP/api/webserver/SesTokInfo" > ses_tok.xml
COOKIE=`grep "SessionID=" ses_tok.xml | cut -b 10-147`
TOKEN=`grep "TokInfo" ses_tok.xml | cut -b 10-41`

curl -X GET "http://$MODEM_IP/api/monitoring/status" \
-H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" > modem_status.xml

cat modem_status.xml 

And here is a sample output:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<ConnectionStatus>902</ConnectionStatus>
<WifiConnectionStatus></WifiConnectionStatus>
<SignalStrength></SignalStrength>
<SignalIcon>4</SignalIcon>
<CurrentNetworkType>3</CurrentNetworkType>
<CurrentServiceDomain>3</CurrentServiceDomain>
<RoamingStatus>0</RoamingStatus>
<BatteryStatus></BatteryStatus>
<BatteryLevel></BatteryLevel>
<BatteryPercent></BatteryPercent>
<simlockStatus>0</simlockStatus>
<WanIPAddress></WanIPAddress>
<WanIPv6Address></WanIPv6Address>
<PrimaryDns></PrimaryDns>
<SecondaryDns></SecondaryDns>
<PrimaryIPv6Dns></PrimaryIPv6Dns>
<SecondaryIPv6Dns></SecondaryIPv6Dns>
<CurrentWifiUser>0</CurrentWifiUser>
<TotalWifiUser>10</TotalWifiUser>
<currenttotalwifiuser>10</currenttotalwifiuser>
<ServiceStatus>2</ServiceStatus>
<SimStatus>1</SimStatus>
<WifiStatus>1</WifiStatus>
<CurrentNetworkTypeEx>3</CurrentNetworkTypeEx>
<maxsignal>5</maxsignal>
<wifiindooronly>0</wifiindooronly>
<wififrequence>0</wififrequence>
<classify>wingle</classify>
<flymode>0</flymode>
<cellroam>1</cellroam>
</response> 


And here is a snippet of what some of the statuses mean:
# ConnectionStatus:
# 112 = no autoconnect
# 113 = no autoconnect on roaming
# 114 = no reconnect
# 115 = no reconnect on roaming
# 900 = connecting
# 901 = connected
# 902 = disconnected
# 903 = disconnecting
#
#              |   discon |   con    |
#              |  2G | 3G | 2G | 3G  |
# NetworkType: |  3  | 4  | 3  | 4/7 |
# 3 = 2G / 4 = 3G (UMTS) / 7 = 3G+ (HSDPA)
#
# SignalStrength: strength in percent
#
# ServiceStatus: 1 = enter PIN / 2 = PIN correct
# SimStatus: 0 = no SIM access / 1 = SIM acc

11 comments:

  1. I tried your bash script which produced

    % Total % Received % Xferd Average Speed Time Time Time Current
    Dload Upload Total Spent Left Speed
    0 0 0 0 0 0 0 0 --:--:-- 0:00:33 --:--:-- 0

    after about half a minute, but I cant find the file with the connection status & network type etc?

    ReplyDelete
  2. I finally got this going Matt. Is there a list of what the different connection status numbers mean? eg 901?

    ReplyDelete
  3. Awesome! I updated the post because the comments I was making kept getting re-formatted. I have a couple of new scripts and I am using a library too now. I will make a post describing them.

    ReplyDelete
  4. Neat! I don't suppose you've figured out how to get the E8372 permanently into modem mode? I have to wait at least 14s after power up before I can modeswitch it from storage mode (:1f01) to modem mode (:14db) The it's another 14s before I can run your script to see if it's got a connection.

    ReplyDelete
  5. Kris, I had to do that with a different dongle, this one always comes up as a network device plugged into my pi, never as a storage one. When I used the one that needed mode switch, there was a way to program it to always come up in modem mode by giving it some funky AT style commands. I can try to look for that but I essentially googled for it and found a guy who documented how to do it.

    ReplyDelete
  6. That's interesting Matt. I'm familiar with AT style modems like the sierra 320U that create a WWAN on the pi. My E8372 powers up as 12d1:1f01 and I have to coerce it to 12d1:14db. I'm sure I should be able to get it to power up in modem mode like yours does but the technique need eludes me so far.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hey Matt the windows driver for the E8372 shows RSRP, do you know how to get hold of that? The 1-5 range from SignalIcon is a bit coarse & SignalStrength is always empty.

    ReplyDelete
  9. Hi Kris, I never used it on Windows, so no idea on that. Yes, the signal icon is coarse, but I have never been able to get any other indication of signal strength. If you can get the AT command level using a serial connection you might be able to issue a command that returns it.

    ReplyDelete
  10. Adding these lines to your script brings up a range of signal strengths Matt
    curl -s -m2 -X GET "http://$MODEM_IP/api/monitoring/diagnosis" \
    -H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" \
    -H "Content-Type: text/xml" >> modem_status.xml

    ReplyDelete
  11. Thanks for sharing your blog with us I am very inspired by your article kindly share some knowledge about Huawei Solar Inverter want to know about it

    ReplyDelete