Auto Shutdown for Inseego M2000 Hotspot: Python 3 Gist

Nice for those who forget to power it off when they arrive at work/home...

Posted by TechBeret on June 26, 2023 · 5 mins read

A Very First-World Problem

So a number of years ago (2020 I believe) I read a post by Cory Doctorow about getting a hotspot from Calyx Institute (referral code included), and have since then annually made a donation and had access to a hotspot running on T-Mobile. It is particularly great for when I’m traveling for work, as I am able to use the hotspot instead of trying to use a questionable hotel internet.

It is also very convenient for usage with my laptop on the bus to or from work, so that I’m not draining my phone battery or worrying about using up my phone plan’s tethering allotment. This has been getting much more use as I have begun returning to the office as opposed to working from home, adding transit time to my day on my area’s public transit system.

Unfortunately I have run into problems with my habit of turning on the hotspot while riding on public transit. My home does not have great T-Mobile coverage, and devices (phone, tablet, laptop, etc.) have a habit of staying connected to whatever network they were on. As such I would arrive home and realize while waiting for my phone’s web browser to load that I was still wirelessly connected to my hotspot instead of my much faster home network. I would then proceed to locate my backpack and turn off the hotspot.

Even when I would go out of range of the hotspot in my backpack so my phone would connect to the “right” network, leaving it on also has the side effect of running down the battery such that the next day when I went to turn it on it would be dead, requiring me to tether to my phone. I know, the horror. Again, a very first world problem.

A Very Geeky Solution: An Auto Shutdown Inseego M2000 Python 3 Script

So I have a problem with my hotspot staying on when I arrive from home / work. One solution is to remember to turn off the hotspot. But I’m a geek, and also very forgetful. I also have a number of Raspberry Pis lying around from various projects I was working on (all acquired pre-pandemic shortages, thankfully). So I wrote up a Python 3 script which logs into the hotspot, retrieves a variety of “interesting” info, and then shuts down the hotspot. All without needing to physically interact with the hotspot.

The “interesting” info includes items like connected clients, data usage, network type, battery status, etc. The script (if you have the associated flag enabled) uploads all the info to AWS DynamoDB, so you can run analysis on the data in the future if you are so inclined. I consider this useful since Calyx does not provide you statistics on your monthly data usage, and at least I find it useful for monitoring how much data I’m using.

DynamoDB Data From Script

The script also supports notifying the user via a Pushover notification if the battery level is below a specified percentage value. I find this useful to remind me to plug it in so that I am not surprised by a low/dead battery the following day.

I have the script installed on two Raspberry Pi devices, one at home and one at work. The script runs in a every-minute cronjob (again, quick and hacky). I connected the RasPis to the M2000 hotspot wireless network, and configured them to automatically connect whenever it is detected. I also have the execution blocked on a successful ping 192.168.1.1, so we don’t go through the effort of trying to shut down the hotspot if we’re not connected to it at all.

I have posted the script as a github gist. It was quickly and hackily developed, and was my first experience with performing an automated web login in Python, so I don’t know if it is the best example to follow. For my purposes it does the trick however. Note that if you want to use it you will need to create a local_secrets.py file which contains things like the hotspot password, along with AWS / Pushover credentials if you want to use either of those.

I hope it is helpful to others who run into the same (unusual) issues that I was experiencing, or even just can be used to see how to programmatically login to the Inseego M2000.

Also, I really wanted this to run on an ESP32, but the “management” interface on the M2000 is only accessible on the primary interface which I have on the 5GHz band. The 2.4GHz band (the only one the ESP32 can connect to) is off on the 2.4GHz band, which is assigned to the “guest” network. If only the ESP32-C5 would become widely available…