This post is focused on use with a Raspberry Pi but should also apply to most Linux systems.
MQTT (Message Queue Telemetry Transport) Is a fast light weight Publish/Subscribe protocol that acts as a simple, common interface.
It is often referred to as a Machine-to-Machine, Internet-of-things protocol but of course hardware does not generally communicate on its own, it is software that does the job.
MQTT is really a Software-to-Software protocol.
There is an interesting discussion about using MQTT with mobile devices.
Mosquitto is an Open Source MQTT v3.1/v3.1.1 Broker written by Roger Light.
The hardest part was building Mosquitto with websockets enabled. Its not really hard but it takes a while. The steps for that are described in the related post Installing Mosquitto MQTT broker on Raspberry Pi.
Once mosquitto is set up there are a few more files that need to be put into place. They are described below.
Although the mosquitto broker can serve HTML pages (see mosquitto.conf line ~153.) and this may be all you need to develop a browser based UI using something like Bootstrap, I decided to use a dedicated web server for my experiments.
Nginx (pronounced engine x) is a light weight, fast, and secure web server that is less resource intensive than Apache on the Raspi.
sudo apt-get install nginx
sudo service nginx start
You should now be able to access the default web page nginx provides at
http://[url of your Raspi]
The directory that web page files should be placed in is:
In order to make working with files in that directory easier I changed the owner to the pi user:
sudo chown pi /usr/share/nginx/www
This allows the editing of html and JaveScript files as the Pi user.
This will allow easy installation of the Python MQTT client. If pip is already installed, skip this step.
Note: There is a version of pip that can be installed using “apt-get install” but it is an old version and will also install the older Python 2.6. So don’t use it.
sudo apt-get install python-setuptools
sudo easy_install pip
Install the Python MQTT client:
sudo pip install paho-mqtt
This will download the JS MQTT client to the nginx www directory. There is also a minified version available.
sudo wget http://code.jquery.com/jquery-1.11.3.js
This will put jQuery in the web directory. If you use a different version of jQuery you will need to edit index.html from the GitHub Python-to-JS repo accordingly.
Clone the proof of concept files from GitHub:
sudo git clone https://github.com/Dan-in-CA/Python-to-JS.git
Copy files to the web directory:
sudo cp index.html /usr/share/nginx/www/
sudo cp jsClient.js /usr/share/nginx/www/
sudo nano /usr/share/nginx/www/jsClient.js
On the first line replace [IP address of pi] with the URL of your Raspi (keep the surrounding quotes).
If you’ve gotten this far Congratulations!
point your web browser to your Raspi and you should see the test page.
From the Python-to-JS directory start the Python client:
sudo python python_client.py
Things should start happening on the test page. Click the button to see what happens.
All the work that has gone into to setting things up will now make controlling web pages from Python and Python from web pages very easy.
As an added bonus you now have a powerful tool for working with the Internet of Things.