MQTT with Mosquitto

The Internet of things! I love things, and now I can make them talk with each other! I figured my arduino had been laying around unused for to long, so when my company decided to have a one day code camp it was definitely time to do some tinkering and learning new stuff along the way.

For a introduction to mqtt, see here: http://www.hivemq.com/mqtt-essentials-part-1-introducing-mqtt/

Installing Mosquitto on Ubuntu:

sudo apt-get install mosquitto mosquitto-client

This installs software for the message-broker and the client (which is what I wanted for testing on my computer). For real implementations you would install mosquitto-client on the nodes that send and receive data, and mosquitto on the server that serves as the message broker.

After installing mosquitto you probably need to open the mqtt default port:

sudo iptables -A INPUT -p tcp -m tcp --dport 1883 -j ACCEPT

Some terms you may see in this context:

  • message broker – the mailman, the service that passes messages to all nodes
  • client – the receiver and/or sender of a message
  • subscribe – how a client starts listening to the message broker
  • publish – how a client sends a message to the message broker
  • topic – what you subscribe to. the message broker may have many topics that it passes messages to, but you will only receive messages for the topics you have subscribed to. Topics are written almost like paths in a directory browser, and may contain wildcards like * (single level wildcard) and # (all sub-levels wildcard).

mqtt overview

Source: eurotech

Testing mqtt with mosquitto

To test mosquitto in terminal you can subscribe to a topic with:

mosquitto_sub -d -t this/topic

and then publish to the topic (in another terminal window) with:

mosquitto_pub -d -t this/topic -m "Message for subscribers"

Here’s a link to mosquitto’s man-pages: http://manpages.ubuntu.com/manpages/precise/en/man8/mosquitto.8.html

Pretty neat!

Adding a python library to the mix

In addition to mosquitto I installed a python library (2.x) called paho-mqtt with the python package manager. If you don’t have the package manager you can install it with:

sudo apt-get install python-pip

Then you can install the library with:

sudo pip install paho-mqtt

Here’s the wiki for paho-mqtt 1.1: https://pypi.python.org/pypi/paho-mqtt

Testing with python

So, now we have a python library for communication using mqtt which is nice. Let’s put it to use!
Here’s a .py-file that imports the os-library and calls the publish-command for mosquitto like we did above: test.py

This is the simplest implementation I could imagine using python. Another example using the paho-mqtt-package: test_using_library.py

My next step was adding the Arduino with some sensors so we have some data to read, make it write it to serial, and reading from serial with a python-script that then pushes messages to subscribers if the values read from the serial port has changed since last read. You can find all the code on GitHub:
TM89/mqtt-lightsensor

I will be adding a diagram that shows how I connected the sensors to the Arduino breakoutboard, but any sensor-data that you can write over serial should work for this test.

Since the time was a bit limited I haven’t added a client yet, but the plan is to add a Android client that reads the messages from the messagebroker and presents them to the user. I will update this post once it is ready.

Leave a comment

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