My MQTT Python Scripts

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.

Script List

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.

Example Usage

I use the logger in a few scripts. One is a topic monitor which logs.

  • Message time
  • Message topic
  • message

The following lines are added to the on_message callback.

if log_data_flag: #log all messages

You Can Download the class here

 MQTT Ping – Test if broker is up.


Type  python -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

Example Run:


MQTT- Monitor -Monitors Selected topics


Type  python -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 -h



This script will create retained messages from a list of topics. This is useful when testing

create-retained-message-scriptThe 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

Please rate? And use Comments to let me know more
[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *