This is part II of a series of tutorials that break down the process of building an IoT weather station with ESP8266. If you missed the first part, you can find it here: Part I. In this post, we're going to go over the protocol for MQTT before we set up 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 built 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 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:

MQTT Functioning Pattern Source: Hivemq

Related: An Introduction to the Internet of Things

With the rising popularity of IoT and MQTT as de facto and 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.

Mosquitto

Source: IBM

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 now, or brew services start mosquitto if we want to have mosquitto startup now and again after restarting. I decided to go with the first option because I don't need mosquitto every day and want to keep control of 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 a video showing you how to do that:


Here's step by step of what we did:

  1. We started mosquitto by executing mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf 
  2. 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.
  3. 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.
  4. 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.
  5. 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.

    Related: Mobile Development for Arduino Part 4

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 reaches the MQTT server, we're going to display the current data and historical data. Stay tuned for the next part of this series!

To learn how to wire the hardware for an IoT weather station, click below!

CLICK HERE

stable-kernel-mobile-app-development

About The Author

Florian Harr is an iOS software engineer at stable|kernel. He studied computer science with a focus on mobile development in Stuttgart, Germany where he's originally from. He has been doing iOS development since the release of the App Store with iPhoneOS 2 and has worked with several Fortune 500 companies, helping them to develop mobile platform solutions. He once ran his own mobile development agency in Germany before moving to Atlanta, Georgia. If he's not riding his electric unicycle in his off time you might be able to find him scuba diving or tinkering with his 3D printer.