Simple Python script to send results of speedtest-cli over MQTT in JSON format
This program uses paho-mqtt, speedtest-cli, geopy, datetime, and schedule to:
- Perform a speedtest using
speedtest-cli. - Log the time that the speedtest was last run with
datetime. - Calculate the distance from the host computer and the server used during the
speedtest-clicall. - Take all of that data, turn it in to JSON, and publish it via MQTT.
Currently the script is set to perform a speed test every 6 hours but can be changed by modifiying schedule.every to whatever you want (reference the schedule docs for more info).
schedule.every(6).hours.do(job)The json data sent via MQTT is structured as follows:
{
"upload": "upload speed (Mb/s)",
"download": "download speed (Mb/s)",
"location": "server location",
"ping": "ping",
"distance": "distance between server and host (miles)",
"time": "timestamp of last speed test (MM-DD-YYYY HH:MM:SS) "
}You should create a file called config.py to store mqtt broker and payload information.
example config.py
mqtt_broker_address="http://broker-address"
mqtt_port=1882
mqtt_user="username"
mqtt_pass="password"
mqtt_topic="topic/foo/bar"Required packages can be found in requirements.txt
This program can be run as a service on Linux distros that have systemd. Just modify /path/to/speedtestmqttjson.py in ExecStart to point to wherever you have placed the python script.