MQTT example using Mosquitto and Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Minor example using MQTT and Python.


Requires Mosquitto to be installed.

On Linux with the apt-get package manager:

sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

Note: mosquitto-clients is to get the mosquitto_pub to make it simple to try stuff from the command line. You could leave it out and only use the supplied

Also install virtualenv if you want to use it (recommended):

sudo apt-get install python-virtualenv


The use of virtualenv is optional but recommended for playing around with this example code.

Simply clone this repo, setup virtualenv and use pip to install requirements.

git clone
cd mqtt-mosquitto-example
virtualenv .
source bin/activate
pip install -r requirements.txt

Test the subscriber example

First start the subscriber which will enter a loop waiting for new messages:


Then open a new terminal and send a message:

mosquitto_pub -d -h localhost -q 0 -t adult/pics -m "can i haz moar kittenz"

This should generate a message in the terminal running the subscriber.

Take a look at to see how to publish messages using python. Or just open another terminal and run it from command line while is running:

source bin/activate

Configure SSL/TLS

See mosquitto-tls on how to generate certificates and keys.

Once created you need to adjust /etc/mosquitto/mosquitto.conf and accordingly and then use mosquitto_pub with cert and key:

mosquitto_pub -d -h localhost --cafile --cert c1.crt --key c1.key -q 0 -t adult/pics -m "can i haz moar kittenz"