-
Notifications
You must be signed in to change notification settings - Fork 13
4. MQTT Guide
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:
- Topics: these are like addresses for a message, here is how a topic would look like:
Home/Livingroom/Lamp/Turn
`Message: ON``
here the topic is Home/Livingroom/Lamp 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.
For example, 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 in 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:
jasds3421FDC1CDSCDSfdsas/Bedroom/Temperature
message:25
while this would be an invalid topic:
Bedroom/Temperature
message:25
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
- 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
- DeviceID/report/connected -> The device sends to the server whenever it connects it must have the message true, (you can use false but it does not make much sense).
- 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.
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 commands:
- 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 the topic must be a valid Id from one of your devices
- You must avoid sending the reserved topics too often every 500 ms is fine as long as it will not be sending them all day long every 500 ms, but in general, avoid to put them in infinite for loops without delays.
devices that don't follow these rules are automatically disconnected and blocked by the system, the first block is for 5 minutes, the second block is for 10 minutes the third for 20 and so on.