MQTT

Starting the Service

This is Fedorish, your mileage might vary:

# systemctl start mosquitto.service
# netstat -antp|grep mosquitto
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      53767/mosquitto
tcp6       0      0 :::1883                 :::*                    LISTEN      53767/mosquitto

Ah, runs on port 1883.

See if it works. In one terminal, subscribe to a random topic. (Topic are created as soon as they are mentioned.)

$ mosquitto_sub --host localhost --port 1883 --topic /random/topic
...sit and wait for message...

In another terminal, publish a message,

$ mosquitto_pub --host localhost --port 1883 --topic /random/topic --message blah

You should see “blah” as the output of mosquitto_sub in the other terminal.

Publishing a Message in Python

This is the easiest, so lets start with that.

#!/usr/bin/env python

from paho.mqtt import client

c = client.Client()
c.connect('localhost', 1883)
c.publish('/random/topic', 'blech')

Run it, and in the terminal running mosquitto_sub you’ll see "blech" on stdout.

Subscribing a Topic in Python

A little more complicated - we have to

  • run an event loop: we want to see more than one message coming in

  • register a callback function that is called by MQTT to notify us about an incoming message

#!/usr/bin/env python

from paho.mqtt import client

def message_received(client, userdata, message):
    print(message.payload)

c = client.Client()
c.connect('localhost', 1883)
c.on_message = message_received

c.subscribe('/random/topic')
c.loop_forever()

Run it, possibly side by side with mosquitto_sub. Publish a message, using either the publish.py program above, or mosqitto_pub.

$ ./subscribe.py
b'blech'

Message Payload?

Note the b in the output: what comes in is not a string. MQTT’s transport is encoding-free; what is sent is completely up to the communicating parties.

You should probably read up on

  • Encoding: Strings and Encoding

  • JSON, a popular transport format, used for MQTT message transfer, and on The Web which is full of it.

Graph

cluster_python Python cluster_python_swdev Software Development cluster_python_basics Basics cluster_python_drafts Python Drafts python_swdev_python_1050_oo Object Oriented Programming python_basics_python_0270_functions Functions python_swdev_python_1050_oo->python_basics_python_0270_functions python_basics_python_0450_dictionaries More on Dictionaries python_swdev_python_1050_oo->python_basics_python_0450_dictionaries python_swdev_python_1200_modules Modules and Packages python_swdev_python_1200_modules->python_swdev_python_1050_oo python_swdev_python_1200_modules->python_basics_python_0270_functions python_basics_python_0140_variables Variables python_swdev_python_1200_modules->python_basics_python_0140_variables python_basics_python_0170_if The if Statement python_basics_python_0160_boolean Boolean python_basics_python_0170_if->python_basics_python_0160_boolean python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_basics_python_0270_functions->python_basics_python_0150_datatypes_overview python_basics_python_0270_functions->python_basics_python_0140_variables python_basics_python_0220_for for Loops python_basics_python_0200_sequential_types Sequential Datatypes python_basics_python_0220_for->python_basics_python_0200_sequential_types python_basics_python_0193_while while Loops python_basics_python_0220_for->python_basics_python_0193_while python_basics_python_0450_dictionaries->python_basics_python_0150_datatypes_overview_compound python_basics_python_0450_dictionaries->python_basics_python_0220_for python_basics_python_0160_boolean->python_basics_python_0150_datatypes_overview python_basics_python_0200_sequential_types->python_basics_python_0150_datatypes_overview python_basics_python_0110_blahblah Blahblah python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0193_while->python_basics_python_0170_if python_basics_python_0193_while->python_basics_python_0160_boolean python_basics_python_0120_helloworld Hello World python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_drafts_mqtt MQTT python_drafts_venv Virtual Environments python_drafts_mqtt->python_drafts_venv python_drafts_pip Python Package Index python_drafts_import The import Statement (incomplete) python_drafts_pip->python_drafts_import python_drafts_venv->python_drafts_pip python_drafts_venv->python_drafts_import python_drafts_import->python_swdev_python_1200_modules