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.
log=m_logger(log_dir,log_recs,number_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..

e.g.

log.log_json(data)

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
    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.

Usage:

Type  python mqtt-ping.py -h

mqtt-ping-usage

Examples

  • 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-Ping-Example

MQTT- Monitor -Monitors Selected topics

Usage:

Type  python mqtt-monitor.py -h

mqtt-monitor-usage

Examples

  • 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

mqtt-monitor-example-use

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

Usage:

mosquitto-conf-file-checker

Examples

  • -f filename – check this file

mosquitto-conf-file-checker-example

 Retained Message Scripts

retained-messsage.py

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

Example

list-clear-retianed-messages

create-retained-messsage.py

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.

edit-create-retained-message-script

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 *