4. MQTT Guide

Here you will find the basic concepts of MQTT and the specification of how the messages are send to and from your device.

What is MQTT?

MQTT is a lightweight simple messaging protocol, designed for constrained devices with low-bandwidth. This means that it is the perfect solution for the Internet of Things and Home automation applications and it's usually more flexible stable and reliable than other protocols like WebSockets. MQTT allows you to send commands to control outputs, read and publish data from your devices and more.

Basics of MQTT

In MQTT there are 3 basic concepts:

Topics: these are like addresses for a message, here is how a topic would look like:

Topic: Home/Livingroom/Lamp/Turn

Message: ON

here the topic is Home/Livingroom/Lamp/Turn and the message is ON, and this is the basic structure of all messages that are sent through MQTT

Publish/Subscribe system:

Every device can subscribe to topics, and it will receive all the messages sent with the topics they are subscribed to, they can subscribe to multiple topics and also subtopics.

Also when publishing a message the devices must provide the topic so the message can be successfully delivered.

A device who is subscribed to Home/# will receive messages sent to Home/Livingroom and also those sent to Home/Bedroom just by using the # sign.

Broker:

Is the central device who allows and restricts connections and also, receives, filters, redirects and publishes messages to all devices connected. In this case, the broker is Smartnest Server.

Is MQTT only a tool to talk to the server?

No, you can also interconnect your devices using this protocol, for example, if you have a motion sensor and a lighting system, and they are too far away from each other, you don't need to install cables across the room to let them communicate, you can just make the light to subscribe to the topic Bedroom/Light and you can have the sensor publish to that topic, at the same time they will be connected to Smartnest and all the integrations.

Smartnest and MQTT

At Smartnest every device has a unique ID, this ID must be used as the first part of a topic.

If you are sending a message to the broker this must be the Id of the device which is sending the message.

If you are sending the message to another of your devices this should be the Id of the device you are sending the message to.

The following parts of the topic can be customized by the user, for example, this would be a valid topic from the device to Smartnest Broker:

jasds3421FDC1CDSCDSfdsas/Bedroom/Temperature Message:25

while this would be an invalid topic:

Bedroom/Temperature Message:25

Smartnest MQTT commands

There are some subtopics reserved for special purposes and here is the list of all the reserved topics:

  • DeviceID/directive/powerState -> Tells the device that there is a power action the message will specify ON or OFF. Supported by all device Types.

  • DeviceID/directive/percentage -> Tells the device that there is a percentage change action, this works with the percentage controller. Supported by light type.

  • DeviceID/report/powerState-> The device sends to the server whenever there is a change in the power state the message must specify ON or OFF. Supported by all device Types.

  • DeviceID/report/percentage -> The device sends to the server whenever the is a change in the percentage value. Supported by light type.

  • DeviceID/report/connected -> The device sends to the server whenever it connects it must have the message true, (you can also use false but it does not make much sense). Supported by all device Types.

  • DeviceID/report/info -> this can be any text or variable values that you would like to send to the server so they will be shown in the web app for easy monitoring. Supported by all device Types.

  • DeviceID/request/time -> this method can be used by the devices in order to get the real time, the message can have optional the timezone GMT+X, if timezone is not present GMT+0 will be sent.

  • DeviceID/response/time -> the server will use this topic to answer the time request and it will send the time in 24 h format: HH:MM:SS

Smartnest MQTT Rules

At Smartnest we are a community of Home Automation and we are all using the same resources so here are the rules that everyone must follow when using MQTT:

  1. The connection will be only allowed when providing valid username and password

  2. The connection will be allowed when the device ID used belongs to the username used

  3. The first member of any published topic must be a valid Id from one of the devices from the username used

  4. You must avoid sending the reserved topics too often every 500 ms is fine, but in general, avoid to put them in infinite for loops without delays.

The devices that don't follow these rules are automatically disconnected and blocked by the system, the first block is for 1 minute, the second is for 5 minutes, the third is for 10 minutes and so on.