All posts

What is MQTT? The IoT protocol and how MakerHex uses it

Published:

An ESP32 board sends its data to a server in the cloud, which passes it on to a dashboard on a laptop and a phone

Reading a sensor and seeing the value in the Serial Monitor is great. But when you want to see the same value from home, from school or on your phone, the internet comes in. Your board has to send the data somewhere, and you read it from there. In the IoT world the most common way to do this is MQTT, and MakerHex talks to devices over MQTT too.

In this post we explain MQTT for someone who has never heard of it, then show how MakerHex uses it. Prefer watching to reading? We cover the same topic in a video (in Turkish):

What is MQTT?

MQTT is a set of communication rules (a protocol) designed for devices to send short messages over the internet. It was created in 1999 to monitor oil pipelines over satellite links, so it was built to work with little data and on weak connections. Today it is used everywhere, from smart home devices to factories.

The easiest way to understand it is to think of a post office:

Post office analogy: devices drop their letters at the post office, and the post office hands each letter to whoever is waiting for that address

  • Devices drop their letters (messages) at the post office. They do not know who will receive them; they just write an address on the envelope.
  • The post office gives a copy of the letter to everyone waiting for that address.
  • If nobody is waiting for an address, the letter goes to no one.

Everything in this analogy has a name in MQTT:

Post office MQTT name Meaning
Post office Server (broker) The hub that receives and distributes messages. In MakerHex it is mqtt.makerhex.com.
Address on the letter Topic The address a message is sent to. Written like a folder path: school/greenhouse1/temperature
Sending a letter Publish Sending a message to a topic.
Waiting for an address Subscribe Listening to a topic and getting notified when a message arrives.

The key point: the sender and the receiver do not know each other. Your sensor board does not know who the dashboard is; it just writes to its own topic. That is why any number of screens, phones or other devices can read the same data at the same time.

Why is it so popular in IoT?

  • It is lightweight. A message's "envelope" is only a few bytes. Even small boards like the ESP32 handle it easily.
  • The connection stays open. The board connects to the server once and keeps the connection open. There is no need to reconnect for every message, so it is fast.
  • It works both ways. Over the same connection the board both sends data and receives commands.
  • No router setup at home. The board starts the connection, so you do not need to open ports on your router or get a fixed IP address.

How are topics written?

Topics are split into levels with /, like folder paths. For example:

school/greenhouse1/temperature
school/greenhouse1/humidity
school/greenhouse2/temperature

When subscribing you can use two wildcards:

  • + stands for a single level: school/+/temperature listens to the temperature of both greenhouse1 and greenhouse2.
  • # means "everything below this" and can only be used at the end: school/greenhouse1/# listens to all of greenhouse1's topics.

How does MakerHex use MQTT?

When you create a device in the MakerHex dashboard you get three details: a user name, a password and a Topic prefix. Your board uses them to connect to mqtt.makerhex.com.

The Topic prefix has two parts: your workspace name and your device's tag, for example your-workspace/greenhouse1. Every topic your device can use is under this prefix:

your-workspace/           ← your workspace
└── greenhouse1/          ← your device's tag
    ├── temperature       ← send a single value: 24.6
    ├── humidity          ← send a single value: 61
    ├── data              ← send several values in one message (JSON)
    └── set/
        └── led           ← command from the dashboard: 1 or 0

Two ways to send data

1. A single value: You send the plain value to the topic named after the variable.

topic:   your-workspace/greenhouse1/temperature
message: 24.6

This is the easiest way. In code it is one line: mqtt.publish(topic, "24.6");

2. Several values with JSON: You send several values at once to the data topic in JSON format.

topic:   your-workspace/greenhouse1/data
message: {"temperature":24.6,"humidity":61}

MakerHex matches each name in the JSON (temperature, humidity) with the MQTT Topic of the variables you defined in the dashboard. Names that are not defined there are ignored.

Which one should you use? If you send one value, the first way is enough. If you send two or more, use JSON: every publish counts as one message, and every plan has a message limit.

Receiving commands from the dashboard

If you mark a variable as Writable in the dashboard (for example an LED), you can send it a value from the dashboard. The value reaches your board on this topic:

topic:   your-workspace/greenhouse1/set/led
message: 1

Your board subscribes to this topic and turns the LED on when the message arrives. The dashboard sends these commands with widgets such as an On / Off switch, a button or a slider.

Message limits

MakerHex runs on a shared server. So that faulty code sending hundreds of messages a second cannot slow down everyone else, the number of messages each device can send is limited by plan:

Plan Per device
Free 1 message every 30 seconds
Maker 1 message every 5 seconds
Trial, Pro, Ultimate 1 message per second

Messages over the limit are rejected; a device that keeps going over has its MQTT access turned off. Details: Message rate and size limits.

Security

MQTT itself does not encrypt messages, so MakerHex never accepts unencrypted connections.

  • Encrypted connection (TLS): Your board connects to the server on port 8883, encrypted. Nobody in between can read the messages.
  • Verifying the server: Your board checks with a certificate that the server it connects to really is MakerHex. The long certificate text in our examples does this; it is not secret.
  • A separate identity for every device: Every device has its own user name and password and can only read and write topics under its own prefix. It cannot reach another workspace's or another device's topics.
  • Deleting a device ends its access: When you delete a device in the dashboard, its user name and password are deleted immediately.

Do not share your password, and remove the password line when you share your code online.

On a workshop desk, an ESP32 board with a sensor sends data to the MakerHex dashboard on a laptop

Give it a try

  1. Create your free account. No card needed.
  2. See your board connect to MakerHex with Device and connection test; no sensor needed and every line of the code is explained.
  3. Already have a working project? Connect it by adding a few lines with Connect your existing project to MakerHex.
  4. Continue with a real sensor in Connect your first device with an ESP32.

Questions or ideas? Write to support@makerhex.com.

MakerHex must not be used to control life-critical or safety-critical systems.

Related examples

Try it with your own device: start free, 14 days of Pro features included, no credit card.

Start free