The heart of IoT is device-to-device communication, which generally involves quick, intermittent snippets of data. Getting one or two devices to talk to each other is easy enough, but once you get past that point, you need an organized system to link everything together.
What is MQTT?
MQTT, or MQ Telemetry Transport, has become a de facto standard in this world, as it’s easy to set up, and works well without a lot of computing overhead. “MQ” at one time stood for Message Queuing, but it has now apparently transcended its acronym status.
How Does MQTT Work?
MQTT uses a publish-subscribe methodology, where clients send and receive messages to each other through a centralized broker, also sometimes called a server. Clients both publish and subscribe to information channels called topics, and any data that passes on via the broker is tagged with a topic label. Once clients are pointed to the broker’s IP address, there’s no more system configuration involved. Clients simply send messages to a topic (that may or may not exist elsewhere) by publishing topic-tagged data to it. Clients listen to topics by subscribing to them.
Note that each client knows nothing about the other clients on the network; the broker merely takes care of data distribution. This can be to one client, many clients, or none, if nothing else is actually subscribed. All a client needs to know is where to find the broker/server. If a client’s IP address changes, or there are other modifications in the underlying system, as long as each client knows where to find the server, things will still function properly.
If this explanation doesn’t quite make sense, I highly recommend just doing the experiments in this MQTT tutorial presented below. This protocol is simple, but you may just have to see how it’s done before things click.
How to Setup Mosquito MQTT Broker on Raspberry Pi
Figure 1. Publish/subscribe terminals running on a single machine. Note that -h can be “localhost” or its IP address.
As an initial demo, let’s run both an MQTT broker and a client on a single Raspberry Pi. This will push data around internally in the same manner as with multiple clients running on separate devices. Note that clients could run on another Raspberry Pi, a PC, ESP8266 or ESP32 WiFi modules, and more. What each element is doesn’t matter to other clients, as long as it can communicate properly with the broker.
1. Starting with a fresh installation of Raspbian Buster Lite (burned here with Balena Etcher), enter sudo apt update then sudo apt upgrade in the command line to make sure you’ve got the latest and greatest running on your system.
2. Add the open source MQTT broker Mosquitto, along with Mosquitto client software on the Pi with sudo apt-get install mosquitto mosquitto-clients.
3. The new host address will be the IP address of the Pi, which is available by typing ifconfig. Installation will start the broker and allow it to start on boot. The mosquitto-clients portion of the install allows you to run a client on the machine as well as a broker.
4. In the Pi terminal, subscribe to the test topic with the command mosquitto_sub -h localhost -t “test”. This sends a subscription message to the MQTT broker running on the same system and lets it listen to any messages sent to this topic.
5. Open up another terminal and type in mosquitto_pub -h localhost -t “test” -m “Hello”, then hit enter. You’ll then see the received “Hello” message on the subscription terminal. You can also substitute in the system’s actual IP address instead of “localhost,” or even open up a third terminal to subscribe or publish in the same way.
6. Press ctrl+c when you’re ready for the terminal to stop actively listening to a topic.
Multiple MQTT Raspberry Pi Devices
Figure 2.
Testing on one machine is interesting as an academic exercise, but the real power of MQTT comes with multiple embedded devices working together. As it just so happens, I have a Raspberry Pi-based plant watering machine that could make a perfect MQTT client. Logging on to it over SSH via PuTTY, I entered: sudo apt-get install mosquitto-clients to install Mosquitto, as I don’t plan on using it as a broker.
Keep the original terminal window from the single-Pi test open, and subscribe to the test topic, or run mosquitto_sub -h localhost -t “test” if you’ve closed it out to listen to this messaging.
Run mosquitto_pub -h [BROKER_IPADDRESS] -t “test” -m “your message” on the first Pi terminal, where [BROKER_IPADDRESS] is the address at which your broker is running. You’ll soon see whatever message you input on the second terminal, as before with the same-Pi experiment.
As a final test, I logged into another Pi that I use to run my 3D printer, set up the Mosquitto client as before, and published to the test topic. The message promptly appeared on the other terminals that were subscribing, as expected. So that’s three separate devices linked together with little extra work on my part. Pretty cool.
As alluded to earlier, when you subscribe to a topic, no one actually has to be publishing to it. Likewise, when publishing, there may be no one subscribed. There’s no explicit setup on the broker—it just passes things along. It’s like the Twitter of IoT.
MQTT Real World Applications
Figure 3.
So, what shall we do with this newfound power? Again turning to the Keurig watering device, we have a simple moisture sensor built into the rig, so why not see if it can send a message out over MQTT when it needs a drink? We could even have it send a message when watering starts and stops.
Doing so is fairly easy, and code for water.py and buttonwater.py found here now include these feedback methods. As seen there, import os allows these python scripts to use command line functions, enabling the os.system() statements to be used. The line: os.system("mosquitto_pub -h [BROKER_IPADDRESS] -t \"test\" -m \"watering\"") enters the broker address, topic, and message options input manually before. \ escape characters are added so as not to have it treat internal quotation marks as the ending quote character of the os.system command itself.
This automatic programmed output can then be monitored on another MQTT system. One could also automate the reaction to these messages without human interaction. Your system could send out an electronic response, or implement some sort of physical output, like a remote light to let you know the plant is in need of water.
Beyond keeping your plants watered, this kind of functionality could be applied in an industrial environment. Perhaps you want to add warning lights over an existing plant network, then another, then another. No problem with MQTT, just connect, point each to the broker, subscribe to the correct topics, and you’re passing data back and forth in no time. Full home automation is also a great candidate for such a system, and this functionality is even available via the open source Home Assistant software package. Just set up your Raspberry Pi broker, point clients to it, and you’re in business!
Related Articles
3320 by Adafruit Industries | Combo Wireless Modules