MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
Mosquitto is an open soure message broker that implements the MQ Telemetry Transport protocol.
So if you are on this page you have some idea about what MQTT is and/or what the terms broker,subscribe and publish mean. If not then I will just briefly describe what these terms mean:
Broker – This is the server where clients subscribe and publish to topics
Subscribe – A client subscribes to a topic and he can receive all the messages published to a particular topic
Publish – A client can publish messages to a particular topic
So lets start with the installation of first mosquitto 1.4 which acts as the broker.
Firstly update your Pi and make sure your repositories are up-to date. I am using 2014-06-20-wheezy-raspbian for this software.
sudo apt-get update && sudo apt-get upgrade
Then we install all dependencies needed for mosquitto
sudo apt-get install mercurial libssl-dev cmake uuid-dev python2.7-dev xsltproc docbook-xsl python-pip
Now that we have all the dependencies.
Let’s start by creating a new directory called mosquitto
mkdir mosquitto
cd mosquitto
Let’s first fetch websockets and compile it for the Pi
wget http://git.warmcat.com/cgi-bin/cgit/libwebsockets/snapshot/libwebsockets-1.3-chrome37-firefox30.tar.gz
tar -zxf libwebsockets-1.3-chrome37-firefox30.tar.gz
cd libwebsockets-1.3-chrome37-firefox30/
mkdir build
cd build/
cmake .. -DOPENSSL_ROOT_DIR=/usr/bin/openssl
make
sudo make install
The above commands fetches libwebsockets and installs it.
Next, We will be compiling mosquitto from source obviously and it can be fetched from a bit-bucket repository with the following command:
hg clone ssh://hg@bitbucket.org/oojah/mosquitto
If you get a error like this:
remote: Permission denied (publickey).
abort: no suitable response from remote hg!
Then run the following commands
ssh-keygen -t rsa -C pi@raspberrypi
cat ~/.ssh/id_rsa.pub
And then add it to the your bit-bucket account.
And then run hg clone ssh://hg@bitbucket.org/oojah/mosquitto
again.
If you don’t get the above error then proceed with the following commands:
cd mosquitto/
hg pull && hg update 1.4
After that edit the config.mk file and change the option to yes from no
WITH_WEBSOCKETS:=yes
Finally make and install mosquitto
make
sudo make install
sudo ldconfig
After this just run the following commands
mosquitto &
mosquitto_sub -d -t hello/world
Open a new terminal on your pi and run the following command from there
mosquitto_pub -d -t hello/world -m "Mosquitto Works"
Once you get a message on the terminal from which you had subscribed you can be sure that you have mosquitto setup on your pi.
Next we setup Eclipse Paho for Python on the Pi
Install eclipse paho with
sudo pip install paho-mqtt
And a basic python file for paho looks like this
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("hello/world")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.eclipse.org", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
#Non Blocking call interface
#It starts a thread and is a more preferred method for use
client.loop_start()
The example above is self explanatory and can be understood with the comments in the code.
Next we will write the code for using websockets with mqtt.
This is rather simple.
First we will copy the /etc/mosquitto/mosquitto.conf.example to /etc/mosquitto/mosquitto.conf and add the following lines in the section labeled “Extra Listeners”
listener 9001
protocol websockets
And start mosquitto with the following command in the background
mosquitto -c /etc/mosquitto/mosquitto.conf -d
To run websockets I used the following example code and cloned in from git with
git clone https://github.com/jpmens/simple-mqtt-websocket-example.git
Then you can move all the files to /var/www/mosquitto and navigate to {{ip of raspberrypi}}/mosquitto/index.html
If you do not get a page means you do not have apache installed and you can install it with
sudo apt-get install apache2 -y
So this post helps you install mosquitto and eclipse paho for python and gives you examples of running mqtt clients from python and websockets with javascript.
Cheers!