SmartHome Wireless Communication: the Extreme Basics of MQTT

Introduction: SmartHome Wireless Communication: the Extreme Basics of MQTT

About: My name is Logan Carlson, I am a recent graduate of Florida Polytechnic University with a degree in Computer Engineering. I try to build all the time and post my projects, but it is time consuming so I have no…

MQTT Basics:

**I am going to be doing a Home Automation series, I will be going through the steps I took to learn everything I have done in the future. This Instructable is the baseline on how to setup MQTT for use in my future Instructables. However, the teachings in this content will be applicable to any project one would like to take on.**

Internet of Things:

The Internet of Things is taking over the world, and especially in communities like our own here at Instructables. Ever since I joined this community it has been entrenched with people building

Devices connected to and controlled over the internet. When working with Internet of Things it is hard not to come by the protocol MQTT. This is a communications protocol like others used around the Internet today such as HTTP or FTP, however the way it works is different which makes it ideal for Internet of Things applications.

What is MQTT:

MQTT (Message Queuing Telemetry Transport is a lightweight communication protocol utilizing a publish/subscribe architecture. HTTP, which is what most of the Internet uses, is built on a request/response system. This means it gets a request from a client, and sends a response to that client. MQTT also has a server (called a broker) as well as many clients. In contrast to HTTP, MQTT allows for clients to publish or subscribe to specific “topics”. What this allows for is more broad communication through a centralized point, the broker. Each node can publish to a topic on the broker, and any node subscribed to that topic will receive the message. Clients can subscribe to multiple topics as well and can then receive multiple directives or updates.

The whole system is event-driven and allows for messages from the broker to be pushed to each subscribed client. So rather than HTTP, where the client requests the information, the client is pushed the information directly from the broker upon receipt. There are services built-in as well allowing for various safeguards, such as QOS specifications. A QOS specification allows for a broker to determine whether or not a message needs to be delivered at most once, at least once, or exactly once. This ensures that data is delivered in the required way to each client. Clients are also able to request that published messages to their topic are buffered in the broker in the event that they are disconnected from it for any reason. Once it comes back online, that data will be pushed to the client.

A topic is nothing special, it is just a variety of strings that are combined and separated by slashes. The format in the example that will be used below is the following: home/bedroom/ceiling_light. Each slash is placed after a topic to signify a subtopic. So a message could be published to home, where all devices in the house will receive it. It could be published directly to home/bedroom, where all devices in the bedroom will receive the message. And it can go down to the specific device as first shown home/bedroom/ceiling_light where only the ceiling light in the bedroom will receive the message. The way we can abstract individual devices like this all the way up to an entire ecosystem is extremely handy, especially when it comes to Home Automation. There are further ways to break topics down, and I will go into them further in future Instructables where the application makes most sense.

Supplies

Only one of the following is needed:

Ubuntu: https://ubuntu.com

Windows Subsystem for Linux: https://ubuntu.com/wsl (Only if you do not have Linux/MacOS)

MacOS: Requires a MacBook

This is required:

Mosquitto MQTT Broker - Downloaded using apt-get (Documentation: https://mosquitto.org)

Step 1: Initial Setup (Depending on Your OS)

MacOS/Linux:

No setup necessary just open up your terminal and skip over to Mosquitto Setup!

Windows:

If you are on Windows, you will need to install the Windows Subsystem for Linux. This is a super easy-to-use and extremely valuable tool that allows you to run a Ubuntu terminal within Windows. No need to install and dual boot Ubuntu just to test development in the terminal!

Installation Steps:

1. Go to the Windows Store and search for ubuntu

2. Download and install the Windows Subsystem for Linux

3. Open the application and follow the instructions to complete setup and you are ready to continue!

Step 2: Mosquitto Setup:

So as discussed in the introduction about MQTT, the protocol requires a broker (server). This broker is the base point for all established connections to each client. All the messages are passed through and queued in this broker. There are many options to choose from for a broker, and you can find these online, but the one we will be using is probably the most common: Mosquitto.

Mosquitto is a Linux-based MQTT Broker with a ton of functionality. I won’t get into the specifics of that functionality right now, but a few basic requirements it fulfills are user/password authentication, and TLS encryption support all of which are useful in the development of Internet of Things devices.

Steps:

All of these steps should be completed in a terminal window.

1. Install Mosquitto and MQTT Clients

sudo apt-get install mosquitto mosquitto-clients

2. Subscribe to a Topic

mosquitto_sub -t "test"

What this does is subscribes to a topic. This topic is denoted by "-t" and the topic value is "test". This value following the "-t" can be anything you want save for a few special reservations.

3. Open a new terminal window and publish a message to topic "test"

mosquitto_pub -t "test" -m "Hello World with MQTT!"

This publishes a message to the topic "test", allowing our other terminal instance to receive the message on the subscription side. The published message is denoted by a "-m" and the message value is "Hello World With MQTT". This message, just like the topic, can be changed to whatever you want!

4. Go to the first terminal window to see your results! You should receive a message that says "Hello World With MQTT" displayed. If you do not see this, make sure that you typed the right topic in. If you have successfully completed this, keep playing with it. Try different topics, with subtopics and different messages!

Step 3: Wrap Up!

Thats it! Once you have it all completed you understand the basics of how MQTT works. This is a very rudimentary tutorial that just shows the bare minimum of the MQTT protocol. Future Instructables will walk through in more depth how the protocol works in practice with the Internet of Things, particularly with ESP8266 modules running Arduino. My first practical application will be the smart coffee maker I have currently in my room. If you would like to learn how to make a coffee maker that can be controlled from both your phone and Alexa, make sure to follow me for more tutorials.

Be the First to Share

    Recommendations

    • Lamps and Lighting Contest

      Lamps and Lighting Contest
    • Make it Move Challenge

      Make it Move Challenge
    • Halloween Contest

      Halloween Contest

    Comments