I mentioned before that I have a MiFi device. It’s a TP-Link M5350. It’s light, portable and easy to use. However one thing I noticed was getting the battery level.
It has an OLED display, which shows the signal bars, number of connected devices, if WiFi is running, the network you are on, the up and down speeds, the current usage and a battery icon.
From quick tests it would always show empty to full animation when on charge and it only had around 4 icons for normal usage.
But what I wanted to know was a percentage level for the device.
Logging in to the web-gui for the router you get more icons along the top, however a title element on the battery shows the current level of charge. Although getting this number to appear on my phone was going to be tricky.
So I wondered if I could get my devices to make a HTTP request to the device, get past the security, find the relevant code and output that in a clear way.
My first step was opening the gui in Chrome on my laptop. Viewing the source showed a collection of frames, with most of the text being built using JavaScript. So instead I opened up the Developer Tools, and clicked to the Network tab.
Here I could see regular requests to http://192.168.0.1/userRpm/deviceStatus.htm?dataRequestOnly=1
Loading this page in my browser however resulted in a blank page, clicking on the Preview tab in the Dev Tools also showed blank. Puzzled I checked the Response tab, turned out the page returned javascript code
<script language="javascript" type="text/javascript">
var devStatusDataOnlyInfo = new Array(
0, 1, 32, 4, 0, 1,
0, 106, 32, 83, 0, 1,
0,
0,0 );
</script>
So the good news is that I had an array to work with, the bad news was what did it all mean.
So I looked back at the source of the page I first opened, and this included the script to build the text.
Working through each of the lines, I could see that the second item was related to signal status bars, the forth was for battery bars (1-4, 5 for charging, 6 for empty), others found for SMS, WLAN, SD status, and then the 106 was the signal strength in -dBm and the 83 was the battery percentage.
Unfortunately the code suggested that if the battery was on charge, it wouldn’t report a battery percentage, sticking the device on charge showed this to be true, with the value dropping to 0.
Now I had a method to get the data, and knew how to grab what I needed the next step was to get around the authentication. Using a local webserver and PHP I tried to file_get_contents() on the page, fingers crossed it would just hand over the data. However what was returned was the login screen.
So back to Chrome, and looking at the Headers sent to the page showed we had some cookies set.
Authorization=Basic%20GiBBEr1SH%3D;
subType=pcSub;
TPLoginTimes=1
Maybe if I could pass these along with the request it might work. file_get_contents doesn’t support cookies, and I could have tried cURL, but here I jumped to Tasker on my phone.
I created a TPLink task, and my first action was a Net->HTTP Get. I filled in the boxes and then added a task to Flash %HTTPD. Running it worked, I had the Javascript I had been seeing in the browser appear as a flash on my phone.
Now to extract the battery status. For past Tasker projects I normally have my own webserver sat in the middle, with PHP doing the grunt of the work returning plain text repsonses, but in this case the requests to the MiFi need to come via a device connected to it. Looking at variable functions in Tasker my first attempt was to Variable Split the response (saved as %TPLINK) along the comma. This gave me 15 parts, the first and last had the extra bits of javascript attached to them, but as I wanted the 4th (%TPLINK4) and 10th (TPLINK10) I was ok.
Do an
IF %TPLINK4 < 5
FLASH "Battery at %TPLINK10%"
END IF
did the job, but there was some excess whitespace which had been included.
So using the Variable Search Replace action against %TPLINK, searching for \s (whitespace) and replacing with nothing over multi-lines before my split resulted in what I needed.
I then rebuilt the code on my Tablet, added some rules to only check if the SSID was that of the MiFi, and then used the Zooper Plugin to store a variable of Charging/Empty or x%.
Then in Zooper I'm able to display the value on my homescreen.
Finally a profile to run the task every 60 minutes and it’s all done.
Tasker Profile Export
TP Link Battery (5)
A1: Variable Split [ Name:%WIFII Splitter: Delete Base:Off ]
A2: If [ %WIFII4 ~ "MiFi-Scruff" ]
A3: HTTP Get [ Server:Port:192.168.0.1 Path:userRpm/deviceStatus.htm Attributes:dataRequestOnly=1 Cookies:Authorization=Basic%20GiBBEr1SH%3D;subType=pcSub;TPLoginTimes=1 Timeout:30 Mime Type: Output File: ]
A4: Variable Set [ Name:%TPLINK To:%HTTPD Do Maths:Off Append:Off ]
A5: Variable Search Replace [ Variable:%TPLINK Search:\s Ignore Case:Off Multi-Line:On One Match Only:Off Store Matches In: Replace Matches:On Replace With: ]
A6: Variable Split [ Name:%TPLINK Splitter:, Delete Base:Off ]
A7: If [ %TPLINK4 = 5 ]
A8: Variable Set [ Name:%TPLINKBAT To:Charging Do Maths:Off Append:Off ]
A9: Else If [ %TPLINK4 = 6 ]
A10: Variable Set [ Name:%TPLINKBAT To:Empty Do Maths:Off Append:Off ]
A11: Else
A12: Variable Set [ Name:%TPLINKBAT To:%TPLINK10% Do Maths:Off Append:Off ]
A13: End If
A14: Zooper Widget Pro Variable [ Configuration:#Ttplinkbat# = %TPLINKBAT Package:org.zooper.zwpro Name:Zooper Widget Pro Variable ]
A15: End If
Addendum: I took a further look in to the login script on the MiFi to ensure that the cookies would continue to work. Turns out that the Authorization=Basic%20GiBBEr1SH%3D; is just an Base64 of the string “admin:PASSWORD”, not exactly the strongest method, but one which results in the same text until the password has been changed.
Notes: This code is based on a device running the most recent firmware M5350_V1_130809 which you can download here.
Thank you very much, very useful info, Ryan.
Do you have an idea how I can use/address micro memory card in tp link M5350 ?
I know it works with cable, any chance to use memory via wifi ?
Thank you & best regards
Wolfgang
It’s been a while but maybe Mac/Linux users may find this useful:
1. First go to e.g. https://www.base64encode.org/ and paste:
admin:YOUR_WIFI_PASS
and get that as HASHTOKEN
2. Then run the following in your bash:
while true; do sleep 1; curl -sf -H “Authorization: Basic HASHTOKEN” http://192.168.0.1/userRpm/deviceStatus.htm?dataRequestOnly=1 | awk -v RS=” ‘BEGIN { FS=OFS=”,” } {print “Signal strength (dBm, negative):”$11″, Signal strength (0-4):”$4”, Battery: “$13″%”}’; done
it’ll read the data each second and return result in the following format:
Signal strength (dBm, negative): 95, Signal strength (0-4): 3, Battery: 83%
unfortunately I didn’t figure out if any of those values represent 2G or 3G so it’s not ideal.
@wolfgang: Yes, I was able to read values from my M5350 with no USB cable connection, just through wifi connectivity and the above network request from command line.