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.
Here I could see regular requests to http://192.168.0.1/userRpm/deviceStatus.htm?dataRequestOnly=1
var devStatusDataOnlyInfo = new Array(
0, 1, 32, 4, 0, 1,
0, 106, 32, 83, 0, 1,
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.
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.
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 ]
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.