Day 20: MQTT (Message Queue Telemetry Transport)
For the last days of this Advent, you’ll now become familiar with a subject that plays a large role in the realm of the IoT. We’re dealing with the messaging protocol MQTT, which was specially developed for the communication from machine to machine (M2M). The protocol is thus so brilliant because e.g. control data, measurement data or texts can be exchanged very easily and compactly. The overhead (data that’s transferred alongside the actual user data) is much lower than with HTTP and can be processed readily even by embedded devices, meaning microcontrollers like our NanoESP. At the same time, the controller doesn’t constantly have to establish a connection and query whether there’s been a change; instead, the connection is continually maintained and the client automatically gives information about changes.
A so-called broker forms the basis for the communication. A broker is a server application that processes all data. A device can now connect to the broker and "publish" messages, i.e. send them to the broker. Another device can "subscribe" to specific topics and then immediately receives a new message published to this topic delivered by the broker. So that you don’t have to set up your own broker, a public broker has been set up for you at the address broker.fkainka.de.
The topics are structured hierarchically, where subtopics are each separated with a slash. There are also so-called wild cards. With a "+" or "#" you can subscribe to multiple topics at the same time. Publishing with the wild cards "#" or "+" isn’t possible, however. For example, the topics
can thus be received at the same time by subscribing to the topic
– The plus thereby always replaces only one topic level. The # replaces all at once. With the topic
you thus receive all messages that are published in all subtopics of MyHome. The data that are published to a topic can be virtually anything, from pure texts to measurement values or simple 1/0 commands for switching a light.
Behind today’s door is a button. In the test, we primarily want to make a couple of dry runs on the subject of MQTT. So load today’s programme to the board and open the serial monitor. After a short time, you’ll see more or less the following messages:
The board has thus connected first to the WLAN and then to the online broker. So that the many different users don’t fall into the queue, a special structure must be maintained. You can therefore write only in subtopics of the form NanoESP/MyMQTTiD/… , where you have to replace MyMQTTiD with the ID that you receive from the board, displayed on the serial monitor. For today, that means that everything is written to the individually adapted topic NanoESP/MyMQTTiD/test.
So that you can also see what you’re writing in a topic, we use an online tool from the company HiveMQ on the computer. With this tool, you can likewise connect to the broker and subscribe to your personal topics or write to them. Now go to the page www.hivemq.com/try-out/ and click there on “Try the websocket client”. Now you first have to establish a connection to the broker by entering the address broker.fkainka.de in the menu option "Connection" under "Host." The port and the ClientID can remain the same, and the other parameters are also correct. Click "Connect." If everything worked, the text "connected" appears next to a green dot at the top right and you have the option in other areas to publish in a topic or to subscribe to a topic.
First click on “Add New Topic Subscription” and enter NanoESP/MyMQTTiD/test in the topic field, where you replace MyMQTTiD with the ID from the serial monitor. Now reset the NanoESP by pressing the Reset button on the board. After a short time, you should receive the message “Hello from my NanoESP” in the browser under the "Messages" item. You can also send a message to the board by going into the topic field NanoESP/MyMQTTiD/test under "Publish" and entering any message you choose, like “Hello from my Browser,” under "Message". Now you receive the message displayed on the serial monitor. So the communication is up.