I like the concept of measuring flows and so have sensors on my water main and my electric mains. Naturally, I wanted to add a reading of how much bandwidth I’m using and get it displayed in my living room. I already have the following in place:
- My LED matrix infopanel
- A router running OpenWRT
- A local MQTT server (with bridge to a remote one, but that’s irrelevant for this post)
As it turns out, this is enough to get live internet usage numbers showing with just a few simple scripts.
First we need to just get the speed from the router. Bmon is a great choice for this. Before long I had figured out the command to show my WAN transmit and receive rates on stdout.
This refreshes every 5 seconds. Then I just wanted to send that to the MQTT server so I wrote this bash script:
The script takes 3 arguments: interface, download speed, tx speed. It converts from bytes/s to kB/s using the bc (note that bash can only do integer division) and then sends the tx and rx speeds to the mqtt server at host.int
.
To get this all to work on my router I did have to opkg install
a few tools, like bmon
, bc
, bash
, an upgraded xargs, and mosquitto_clients
.
Then to tie it all together I wrote another script based on the upper command. It uses xargs
on every newline to pass the three columns above as three arguments into this MQTT submission script. Pretty easy:
So that will just run forever, updating the local MQTT service with the latest speeds every 5 seconds. Then I just made it a service by making a file in /etc/init.d
.
And then in the infopanel I just set up a few new simple sprites and scene for all the flows.
And that’s it! Very nice.