These scripts are scripts that I’ve written while experimenting with MQTT.
You are welcome to download and use if you want to. Get them from the download Page.
Grateful any comments/problems or requests.
- Simple data Logger Class
- MQTT Ping – Test if broker is up
- MQTT- Monitor -Monitors Selected topics
- Mosquitto Comment Stripper
- Retained Message Scripts
Simple MQTT Data Logger Class
Most MQTT brokers don’t provide any mechanism for logging historical data for later analysis.
However due to the publish/subscribe nature of MQTT is is easy to monitor, and log a data stream or streams using an MQTT client.
In fact this method may in many circumstances better than logging the data on the broker/server especially when the broker is being shared.
To implement you simply connect the client to the MQTT broker and subscribe to the topics that you need to log.
The logger is implemented as a class called m_logger (message logger).
To create an instance you need to supply three parameters:
- The log directory
- Number of records to log per log
- Number of logs.
The logger creates the log files in the directory using the current date and time for the log names.
To log data call the json_log method.
The method takes a single parameter containing the data to log as a list or dictionary..
The log file will contain the data as JSON encoded data strings each on a newline.
I use the logger in a few scripts. One is a topic monitor which logs.
- Message time
- Message topic
The following lines are added to the on_message callback.
if log_data_flag: #log all messages data["time"]=tnow data["topic"]=topic data["message"]=msg log.log_json(data)
You Can Download the class here
MQTT Ping – Test if broker is up.
Type python mqtt-ping.py -h
- 1.use -c 10 for 10 pings
- 2. Use -d 5 for 5 second delay between pings
- use -s to only print end result
MQTT- Monitor -Monitors Selected topics
Type python mqtt-monitor.py -h
- Can add multiple topics by repeated use of -t option
- Use -v no to display only changed messages
- Use -c 100 to stop after 100 seconds
- Use -q value (0 or 1 or 2) to set the QOS of all topics
- Use -d yes to turn on debugging mode
3.Mosquito Config File Comment Stripper
If you are using the default mosquitto.conf file with all of the comments then finding your settings can be a little tricky.
This script prints the settings from a config file
- -f filename – check this file
Retained Message Scripts
This script will list topics that have a retained message and optionally delete the retained message. Can be use from the command prompt by supplying options.
Run with the -h option to list options.
>python retained-messsage.py -h
This script will create retained messages from a list of topics. This is useful when testing
The script can be run from command prompt but it is easier to edit, and run in the IDE or from the command prompt with no options.
Part of the MQTT-Script-tools package on downloads page