Foreword
Okay In 'Part 1 : IoT, Home Automation', you've set up and configured your 'Vanilla' Raspberry Pi (pardon the pun) and can connect to it remotely with PuTTY. Now we will set up a server which will establish your main household IoT infrastructure and test it works as expected.
Introduction
This Instructable documents how to set up an IoT MQTT Broker and verify it is working correctly by 'snooping' MQTT topics with MQTTSpy.
What Parts do I need?
What skills do I need?
IoT Devices
IoT devices rely upon an underlying protocol named 'Message Queuing Telemetry Transport' or 'MQTT' for short. This protocol is known as 'Publish/Subscribe' and is extremely simple and lightweight, being designed specifically for 'constrained devices' and low bandwidth systems. Which in normal language means a communications protocol so simple and small that even a microprocessor system like an ESP8266 or Arduino can support it. Once you couple this with a WiFi connection (ESP8266) or Ethernet link (Ethernet Shield) you have yourself the basis of an IoT device.
Publish/Subscribe
The second picture above is a generalised pictorial representation of a Publish/Subscribe protocol in action.
Here an IoT device or 'Client' has detected an unusually high temperature and 'Published' this 'Topic' to the MQTT 'Broker'. The MQTT Broker has then forwarded this information to all those 'Clients' who have chosen to 'Subscribe' to this news 'Topic'. In this case it could be an intelligent device which switches on an A/C unit or a passive data logging system or even an intelligent building management system which in its wisdom has chosen to open the windows.
Equally it could have been an engine management system publishing fuel consumption data or a smart meter publishing electricity consumption figures.
If this explanation is troubling you then think of it like YouTube. I upload a video to YouTube (the broker) and my many followers who have subscribed to my channel will get a notification of the new publication.
The Broker
This is the heart of the publish/subscribe protocol. The MQTT Broker is optimally designed to handle many thousands of concurrently connected MQTT clients.
It is primarily responsible for receiving all messages, filtering them, deciding who is interested in them and forwarding messages to all subscribed clients.
The Client
The MQTT Client, be it Subscriber or Publisher (or both in one device) is any device from small Microcontroller up to a fully fledged server, that has an MQTT library running and is connected to an MQTT Broker over any kind of network.
.
And that's all you need to know on MQTT. We won't be going into the protocol or how they plug in as there is simply no need at this point.
Series Links
To Part 1 : IoT - 'Intranet' of Things : Home Automation. Part 1 : IoT, Introduction
To Part 3 : Creating Your First IoT WiFi Device. Part 3 : IoT, Home Automation
We will be using the Mosquitto MQTT Broker from Eclipse as its free and runs under Linux. It is possible to use a PC based MQTT Broker however I specifically wanted to use a Raspberry Pi as they are cheap, physically very small and pack a large punch where processing power is concerned, better still I have a load of them handy. I used a B+ Pi.
Preamble
I am assuming you have followed the steps in my earlier Instructable named 'IoT - Intranet of Things : Home Automation. Part 1 : IoT, Introduction' and have a Raspberry Pi already set up and ready to go.
All you need to do is change the host name from 'MYHOSTNAME' to 'MQTTSVR'. You can do this by using PuTTY to connect to the Pi and from the command line enter;
Select (9) Advanced Options then (A2) Hostname.
Enter 'MQTTSVR' and save the changes. Exit then choose to reboot the Pi
You can now re-connect PuTTY to this Pi by using the new hostname 'MQTTSVR.local'. Create a PuTTY saved session named 'MQTTSvr', it will come in handy later.
Installation
The installation process is as follows;
Connect to your new MQTT server via PuTTY and remembering to copy from this Instructable and paste into PuTTY to avoid having to type any commands by hand.
Ensure you are in the home directory. If you are you will see the following prompt
pi@MQTTSVR:~ $
If you don't see this enter
Now complete the following commands;
then;
Now make this repository available to apt;
As we are using the Raspberry Pi Jessie Debian image enter the following;
Now enter the proceeding commands, which may take some time to complete. Also answer Yes (Y) to any questions;
To complete the install process enter the following, answering Yes (Y) to any questions;
Let the Pi do its thing and complete where you will see the command prompt.
pi@MQTTSVR:/etc/apt/sources.list.d $
.
.
That's it you're done. Mosquitto has now been installed as a service and will automatically start when you boot your Raspberry Pi.
Now we need to test it works.
.
Note : Before you remove power from your MQTT Broker enter the following command and wait for the Pi to shutdown.
.
Hint
If you want to quickly connect to your Raspberry Pi server using PuTTY without having to type in username and password each time. Do the following, assuming you have already created a saved session named 'MQTTSvr' as in Step 7, 'Part 1 : IoT, Automation' and your MQTTSVR username and password are unchanged from the example given.
Locate your PuTTY install and do the following;
This is a good time to have a quick discussion regarding MQTT Topics.
What is a Topic?
A topic is a string made up of UTF-8 (Unicode Transformation Format 8-Bit) characters and is the data sent by a Publisher to a Broker to be forwarded on to any Subscribers. In short its a basic text string.
How are they constructed?
Topics are broken down into one or more topic level each with a minimum of at least 1 character. These levels are separated by a forward slash '/' and can include ' '(spaces).For example (not including the quote marks);
'this/is/a/six/level/topic example with spaces'
Though arguably not very useful topic, hopefully gets the point over. Topics are case sensitive so;
'test/topic'
is different to;
'Test/Topic'
This is a single topic
'x'
as are these;
' ' (space) and '/'
There is no special action necessary to create a topic, the act of subscription or publication does this for you.
And that's about it.
Is there a special format or best practice for constructing topics?
The short answer is 'no' and there are lots of views surrounding how you should make them up (and I will cover the topic in a later Instructable). However you would need to consider the following when creating your topics;
Keep them short. They are sent over the network and need to be parsed (read) by some device/broker/server, so the longer and more intricate you make them the harder they are to decode.
Where you can, use meaningful names. It makes debugging easier. As a human its a lot simpler to type, or read 'sensor1' than 'Wcp10X!3wdrka2'.
Make them have meaning to you. The examples I have seen tend to be like a kind of address such as;
'myhouse/first floor/bedroom one/ceiling/light'
This assumes a fixed location IoT device and for the most part this will be true, though this type of nomenclature fails when considering mobile or re-deployable IoT devices, such as a temperature humidity sensor, or switchable extension socket. Should you decide to move a switchable extension socket from the living room to the bedroom to replace a failed device, you will need to reprogram the IoT device to respond to its new location to be consistent.
For my series on IoT based home automation I will be using topics which refer to the device only such as;
'/EthernetDevice/Led1Command'
or
'/WiFiDevice/Temp1Status'
This is because I ultimately want to use a MySQL database to hold the configuration and IoT device information. Which I will hopefully cover later.
Oh, and one final thing, it is usually convention not to precede the first topic with a '/', but you can see I've ignored this. :-)
Are there any short cuts?
Yes, MQTT offers what is know as topic 'wild cards'. These are special characters a subscriber can place into a subscribing topic to reduce the need to subscribe to many items. There are two types of wildcard characters; '+' known as 'Single Level' and '#' for 'Multi Level'. The Picture above gives examples of both.
A word of caution. If you use the '#' multi level wild card and have a lot of IoT devices in your system, then be prepared for a lot of messages!
Are there any special topics?
Yes, topics beginning with '$' are reserved for internal statistics of the MQTT Broker. At the time of writing this Instructable there is no clear official standardisation of topics that must be published by a Broker. However it is common practice to prefix with '$SYS/' for this type of information. The mosquitto MQTT Broker conforms to this '$SYS/broker/#'. (see Linux mosquitto man page for full details. ie. man mosquitto)
So if you want to see all the mosquitto MQTT Broker statistics you would need to subscribe MQTTSpy to the following (see next steps for further details);
'$SYS/broker/#'
Preamble
To test our new MQTT Broker we will first install a client which can subscribe and publish to a given topic on the Raspberry Pi. We will then monitor this exchange via tool named MQTTSpy from the PC (just like in the first picture above).
Why, test the install? Well it could save you a lot of time in the long run. To correctly set up a robust home automation system you start to rely upon and to which in the fullness of time be adding lots of useful IoT clients to, you need to make sure that all the links in the chain are working correctly, that way if you observe an issue with your new IoT device you can quickly rule out as many systems as possible and zero in on the actual issue.
Installing Pub/Sub Clients (Raspberry Pi)
From a PuTTY window enter the following command on your MQTT Broker. Ensure you are in the home directory;
if not enter;
followed by;
.
Installing MQTTSpy (PC)
In order to successfully run MQTTSpy you will need to have the Java Runtime Engine (JRE) greater than version 8 installed on your PC.
To check what version of the JRE you have installed, run up a 'dos in a box', ie a command line window (picture above).
From the Start button, enter into the search box with the grey text 'Search programs and files'
cmd
and hit enter. A dos in a box will appear into which you will need to type;
java -version
It will return 'java version "W.X.Y.ZZ"' and some other 'guff' (picture above) or an error. if you have anything other that greater than version 8 (actually 1.8.y.zz) you will need to follow these instructions;
Go to this Oracle downloads web page.
https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html
Select the 'Accept Licence Agreement' radio button, download and install the 'Java SE Runtime Environment 8u92', jre-8u92-windows-x64.exe (as in the picture above).
Ok, now go to the MQTTSpy download page given below;
https://github.com/kamilfb/mqtt-spy/wiki/Downloads
Select and download mqtt-spy jar file (picture above). At the time of writing version 0.5.0 was the latest available.
Save your 'mqtt-spy-0.5.0-jar-with-dependancies.jar' file to any directory.
To get the software to run you can double click it. It this doesn't work you can enter the following from the command line, in the directory you saved the jar file.
java -jar mqtt-spy-0.5.0-jar-with-dependancies.jar
Alternatively you could unzip and drop the RunMQTTSpy.bat file (given below) into the same directory and double click this to run MQTTSpy each time.
.
Ok, you 've now installed all your test software, let's configure it and run a test.
.
Hint
To open a command prompt at a specific directory do the following;
Out of the box, the minimal configuration required by MQTTSpy is as follows;
.
Here the video above shows how to connect to our mosquitto MQTT Broker 'MQTTSVR' and add a wild card to view all test topics and test the MQTT Broker. The running order is as follows;
.
Ok, so now we are accomplished MQTT Broker testers. What does this give us?
If we are having any issues with our Home Automation set up, we can now quickly run up MQTTSpy and check to see if the Broker is healthy or any published messages are 'malformed' etc. We can even publish messages, meaning we can test any IoT device we create.
Although in the examples I have given above I use PC based software to carryout my testing equally it is possible to use a smartphone app.
Pictured above is one such application named 'MQTTool' for the iOS. Although not as feature rich as MQTTSpy it is easy to use and allows you to do some basic testing.
From the play store I could see quite a few free apps, I tried many out but found the app pictured above to be the best of them 'MQTT Dashboard'. Though you need to put in an IP Address not the host name 'MQTTSVR.local' it does have a better method to publish to topics, allowing you to create text boxes, switches, sliders etc.
.
I included a screen grab from the Pi detailing how to get the IP Address of the Pi. To do this, connect via putty and enter;
The Pi will respond with the screen shot above and I have highlighted the IP address you will need to enter into MQTT Dashboard. I have assumed you are connecting to your network via Ethernet. If you have connected via WiFi read 'wlan0' for 'eth0'
In the next three Instructables we will create three simple IoT devices for your Intranet which are based around the ESP8266-01 and the Arduino ATMega2560 with Ethernet Shield. The first of which we will revisit to improve the design and extend its use to show just how easy it is to make your own IoT devices.
I used the following references to put this Instructable together;
Official MQTT site;
MQTTSpy Home Page;
MQTTSpy Downloads, Getting Started Guide and further reading;
Good place for MQTT Clients;
MQTT Spy advanced usage
Mosquitto Eclipse Home Page
http://projects.eclipse.org/projects/technology.mosquitto
Mosquitto MQTT Broker details, downloads, install process etc.;
Good background material
Good source of best practice for Topic naming conventions
Google groups protocol discussions
UTF-8 details