Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Follow publication

Using MQTT to Push Messages Across Devices

Learn how to use MQTT in your Python applications

Wei-Meng Lee
Level Up Coding
Published in
6 min readMar 19, 2022

Photo by Pat Whelen on Unsplash

If you are familiar with Internet of Things (IoT), chances are you have heard of this term called MQTT. MQTT (which stands for Message Queueing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. It is often used to transport messages between devices in low-bandwidth environments. A good case study is as shown below:

In an IoT environment, sensors such as a temperature sensor or a motion detector records readings continuously (such as the temperature sensor which records temperature every second) or when it is triggered (such as the motion sensor only returning a signal when motion is detection). All these data are often routed to devices where they can be recorded and analyzed. As all these sensors generate data at different rates, MQTT is ideally suited as the protocol to relay real-time information from one device to another. Sensors that want to relay the data to another device can publish the data to a MQTT broker, an intermediary entity that enables MQTT clients to communicate. Devices that want to receive the data from the sensor can subscribe to it.

With so many publishers and subscribers, how do the subscribers know which message to pick up? Turns out that MQTT solves this problem with a very simple concept known as topic.

A topic is a UTF-8 string that looks like your file path. For example, if you want to send temperature reading in your room, you can use the following topic — room. However, if you have multiple rooms, you can use different topics — house/room1,house/room2, house/room3, etc. Notice that you use the / character to break the topic down into further levels. You can use as many / as you want, e.g. house/room1/toilet, house/level1/room1/toilet, etc.

To receive messages from a topic, you simply subscribe to the topic that you want, e.g. house/room1. If you want to subscribe to all the topics that starts with house, you can use the # and + wildcards:

  • If you want all topics that starts with…

Written by Wei-Meng Lee

ACLP Certified Trainer | Blockchain, Smart Contract, Data Analytics, Machine Learning, Deep Learning, and all things tech (http://calendar.learn2develop.net).

No responses yet

Write a response