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.
In MQTT there are 3 basic concepts:
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
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.
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.
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.
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:
while this would be an invalid topic:
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
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:
The connection will be only allowed when providing valid username and password
The connection will be allowed when the device ID used belongs to the username used
The first member of any published topic must be a valid Id from one of the devices from the username used
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.