DisclaimerPlease, proceed carefully following the tips published in this blog, specially when Main Power is involved. I'm not responsible for any damages caused by what is written in this blog.
Home Automation System - SONOFF and MQTT
by Marco Lamanna
Ok, it's time to get all the power of SONOFF.
Just to summarize, I've got a SONOFF Pow and a SONOFF Basic, both of them with TASMOTA firmware on board.
Using this fantastic firmware, I could control the device through serial comms, web api (http) and MQTT, a very good protocol for IoT purpose, thanks to its lightweight footprint. I spoke about MQTT in a previous post (see https://www.domoticsduino.cloud/en/blog/1/iot-smarthome/post/2017/07/home-automation-system-discovering.html)
I will use mostly the MQTT protocol; so, I have to setup SONOFFs to use it, entering the MQTT broker informations and the topics. As usual, the official Wiki pages help us (see https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features)
For those not able to run a local MQTT broker (Mosquitto is a good free broker), it is possible to use, only for testing purpose, some free public MQTT broker (example: Mosquitto Test Server, Hive MQ and Eclipse)
Browsing the configuration pages on the SONOFF web console, you could enter all the MQTT informations. You can open the SONOFF web console using your browser and typing the ip address in the address bar. A web page similar to the following one will be shown:
Clicking on "Configuration" ==>
We will use mainly the "Configure MQTT" option: the others are self-explained.
Here, I entered the Host (ip address or host name) and the Port (1883 is the MQTT default one) for the MQTT broker.
The Client ID field is a simple string which identifies the device on the MQTT broker. It will be better to be univocal among all the devices.
User and Password fields are used for broker authentication (highly recommended!!!)
Topic and Full Topic fields are the most important one for the MQTT protocol. Here you can find some information about them: https://github.com/arendst/Sonoff-Tasmota/wiki/MQTT-Features#mqtt-topic-definition
- Topic: normally used to identify the device; it will be part of the MQTT Full Topic. It will be better to be univocal among all the devices.
- Full Topic: is the complete topic used for MQTT messages.
Inside Full Topic, we could use two variables called %topic% and %prefix%.
%topic% will be replaced by one of the following system variables, according to the real situation: Topic, GroupTopic, ButtonTopic, SwitchTopic e MqttClient.
%prefix% identifies the type of message and could take three distinct values, corresponding to the folloing system variables: Prefix1, Prefix2 and Prefix3
- Prefix1 = cmnd, used for command messages
- Prefix2 = stat, used for status messages
- Prefix3 = tele, used for telemetry messages
When I speak about system variables, I mean a list of configurable TASMOTA properties for every device. There are so many...Here you can find the complete list: https://github.com/arendst/Sonoff-Tasmota/wiki/Commands#command-overview
Here is my SONOFF Basic configuration:
Topic = sonoffbasic01
Full Topic = tasmota/garden/light/%topic%/%prefix%/
For Topic and Full Topic I followed a naming convention, in order to understand the mean of a message, just reading its topic.
The Topic, as said before, identifies the single device; so I used the type of device followed by a progressive number. For example, the topic of the SONOFF Pow will be sonoffpow02. The same topic string will be used also as network name and mqtt client id for the device.
The naming convention for the Full Topic is: firmware/zone/type/%topic%/%prefix%/
I would like to clarify that these are not rules but only suggestions. I chose this naming convention, you could use another one. It's up to you...
The final step is to enable MQTT protocol on the device, looking for the MQTT enable flag in the Configure Other page.
Well, finally now I'm ready to test my SONOFF with MQTT.
A small update about a configuration error I found later