Building a browser based User Interface with MQTT

This post is focused on use with a Raspberry Pi but should also apply to most Linux systems.

It all started when I was trying to find a way to have Python code in the SIP irrigation control software  project update an image on a page of the program’s web UI. When I tried goggling “call JavaScript from python” I got links to things like python to JavaScript compilers, web frameworks, and running JavaScript code within Python programs. Not exactly what I wanted. The SIP project already uses the Web.py framework.

At about the same time I was updating a user contributed SIP plugin that uses MQTT and discovered that there are MQTT clients for both Python and JavaScript (as well as other languages).  This gave me an idea to try MQTT as a way to have Python and JavaScript talk to each other. I found some example code on the web and put together a proof of concept which is now available on GitHub. It includes a small Python program (< 60 lines of code),  a JavaScript file (37 lines) and an HTML file (27 lines).

The Python program sends messages to JavaScript which then updates text on the html page much like AJAX. When a button on the page is clicked JavaScript sends a message telling the Python program to run a function which answers back and restarts a counter on the Python side.

mqtt-1

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.

This post describes the steps I used to set up a proof of concept that has Python and JavaScript working together using mosquitto.

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.

Install nginx:

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:

/usr/share/nginx/www/

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.

Install pip:

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

Download the JavaScript MQTT client:

cd /usr/share/nginx/www
sudo wget git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/plain/src/mqttws31.js

This will download the JS MQTT client to the nginx www directory. There is also a minified version available.

Download JQuery:

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:

cd ~
sudo git clone https://github.com/Dan-in-CA/Python-to-JS.git
cd Python-to-JS

Copy files to the web directory:

sudo cp index.html /usr/share/nginx/www/
sudo cp jsClient.js /usr/share/nginx/www/

Edit jsClient.js

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! 

Things are now set up and you can begin experimenting with Python-to-JavaScript two way communication.

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.

Happy coding!

Advertisements

2 comments

  1. Thanks for the update on the post ‘Installing Mosquitto MQTT Broker on Raspberry PI (With Websockets)’ I believe that part is working now… As follow on. I tried to test things out using the instructions from ‘Building a Browser Based User Interface with MQTT’..

    A couple of things..

    when installing nginx.. there is no /usr/share/nginx/www folder. However there is /usr/share/nginx/html folder so i replaced the lines above with the /html folder and things go Ok. When I try to test the index.html (Which shows up in my browser, as expected.) and the python_client.py.. I see an error in the browser console:

    at line: message = new Paho.MQTT.Message(msg_str); <- Error: Paho is not defined

    When I check the install of the paho library via the terminal..
    I get the following message:

    pi@raspberrypi:~ $ sudo pip install paho-mqtt
    Requirement already satisfied (use –upgrade to upgrade): paho-mqtt in /usr/local/lib/python2.7/dist-packages
    Cleaning up…

    Any ideas?

    Thanks again for your prompt response from my last question..

  2. I’ll look into it but that post is getting out of date.

    I will be posting new information with a working example based on work I have done more recently. There is a much better way to handle the messages between Python and JavaScript. Also,
    Mosquitto can act as a web server eliminating the need to install nginx.

    The new post will be a practical example of a program that can control a relay board connected to the Pi using the browser based UI.

    It sounds like you have the Paho Python package installed but from the error message it looks like the JavaScript client is not installed. See the steps under “Download the JavaScript MQTT client:”

    Dan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: