This is part II of a series of tutorials that build up on each other. If you missed the first part, you can find it here: Part I
Part II is going to be a little bit slower than part I, simply because we have to make a few preparations in order to keep going faster again in part III. We’re going to take a look at MQTT by going over the idea what this protocol does before we go ahead and setup a MQTT server that we’re going to use in part III to communicate with our little weather station.
MQTT stands for MQ Telemetry Transport and is a lightweight messaging protocol that was build as an easy way to exchange information without much overhead. MQ stands for Message Queueing even though there’s actually no message queueing happening. It’s been standardized since 2013 as the protocol for IOT devices. The standardization society OASIS (Organization for the Advancement of Structured Information Standards) describes MQTT best as:
“Providing a lightweight publish/subscribe reliable messaging transport protocol suitable for communication in M2M/IoT contexts where a small code footprint is required and/or network bandwidth is at a premium.” The protocol functions in a publish-subscribed way where clients, such as sensors and actors, subscribe to a MQTT broker that opens a channel and processes further actions based on the incoming data. Processing on the broker can be really anything, from further publishing the data to other actors to simply displaying data or triggering another server.
This function pattern is best displayed in a graph:
With the recent popularity of IOT and MQTT being defacto the main protocol for most IOT applications there’s a huge variety of software available. A few examples for MQTT brokers that are free for you to use: Mosquitto, Eclipse Paho, Emitter or emqttd. Most of them are open source with a fairly substantial community backing the software. I decided to go with Mosquitto, as it’s one of the more lightweight brokers that’s also available for basically any platform out there.
I’m going to install Mosquitto on my MacBook Pro. As a big fan of homebrew, the missing macOS package manager, I’m going to use the package they provide for mosquitto.
To install Mosquitto with homebrew we only have to run
brew install mosquitto and homebrew will do the rest. After the successful installation of mosquitto, homebrew showed us how we can run mosquitto by either executing
mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf if we want to run it just now, or
brew services start mosquitto if we want to have mosquitto startup now and at restart.
I decided to go with the first option, as I don’t need mosquitto every day and want to keep control when to launch it.
Once we launched it with
mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf we want to know if our server works. So we’re going to open a channel by subscribing to a new topic and then sending a new message. Here’s another screen capture showing you how to do that:
Here’s step by step what we did:
- We started mosquitto by executing
mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
- Once mosquitto started and told us that the server is up and running on port 1883, we opened a new tab on the terminal. This is important! Don’t close the old one as we want the server to keep running.
- In our new tab, we issued the following command:
mosquitto_sub -v -t 'test/topic'. The -v option gave us some more output (verbose), the -t option did let us choose a topic name.
- Next, we open another tab and issue
mosquitto_pub -t 'test/topic' -m 'helloWorld'. We use -t again to specify the topic and -m for the message.
- If we go back to our second tab now, we see the messages we’ve sent to our topic and if we go back to our first tab, we can see when we connected to our server and when we disconnected.
Congratulations! You’ve just setup a MQTT server, published a channel/topic and sent MQTT messages to that channel.
In the next part of this series we’re going to use our weather server to publish sensor data to our MQTT server. Once the data reached the MQTT server, we’re going to display the current data and historical data. Stay tuned for the next part of this series!