Setting up the Sonoff Tasmota MQTT Switch

sonoff-switchNormally the Sonoff switches don’t support MQTT but you can buy flashed devices that run the Tasmota firmware that does.

The Sonoff-Tasmota firmware provides three interfaces: MQTT, web and serial that can be used for controlling the switch.

In this tutorial I will cover basic setup of the Sonoff Tasmota MQTT Basic Switch and how to use MQTT to control and configure the switch.



Control Button

There is just a single button that is used to control the switch.

It can be used to manually toggle the switch on/off or to place into various configuration mode depending on how often it is pressed.

This link here has an overview of the button options.

Switch Configuration

There are two steps to the configuration:

  • Wi-fi Setup
  • MQTT Client configuration

Wi-Fi Setup

To configure the Tasmota switch you need to connect it to a Wi-Fi network.

The switch connects to a Wi-Fi network just like you mobile does and so s you will need to configure the SSID and password of that network.

There are several ways of doing this and the configuration methods are covered here under button usage

The easiest method is to use WPS followed by the android App.

If you use the manual method 4 short presses then note the following:

With this option the switch starts as a Wireless Access point. The SSID on mine was sonoffbasic-1122.

You then need to connect a computer to this access point and go to the IP address 192.168.1.4.

You can then enter the SSID and password of the Wi-Fi network you will be connecting the device to.

When you enter this information into the webpage the switch will restart, connect to the Wi-Fi network and acquire an IP address.

Now in order to configure it further you will need to know the IP address it has acquired.

This isn’t as easy as it sounds but the device name on the network seems to be set to the same as the access point.

So you could try to ping this network name which should give you the IP address.

On my network the BT Home Hub displays all connected devices and was able to find the device IP address there. (screenshot below).

sonoff-ip-address

Configuring MQTT

The switch will start as an MQTT client and also a web server.

Initial device configuration is done via the web server. To access the configuration page go to

http://Device-IP/cn  e.g

configure-sonoff

Click on configure MQTT button and you should see the screen similar to the one below:

configure-mqtt-sonoff

You need to enter:

  • The IP address or domain name of the MQTT broker
  • The Port (default 1883)
  • Client – This is used as the client -id  and needs to be unique
  • Username – Optional
  • Password – Optional
  • Topic – The client will publish and subscribe on this topic. The name should be meaningful.

Note: You can use an external broker like test.mosquitto.org but it is better to use a local one. See installing mosquitto or installing mosca on node-red

When finished save the configuration and the switch should restart.

Controlling the Switch

You can control the switch i.e turn it on/off using either http or MQTT.

This is important to note as some systems may require http and if you have an existing control system that uses http then it might be easier to use that rather than going to MQTT.

This tutorial will mainly cover MQTT but I will also be covering http in another tutorial.

Turning on/off using mqtt

To turn it on/off you publish to the topic

topic/cmnd where topic is the topic you entered in the MQTT configuration. In my case sonoff-light the message is simple text on or off as shown in the example below using the mosquitto_pub client.

sonoff-on-off-mqtt

Turning on/off using http

You can use the curl command or easier still a web browser.

Enter the following

http://sonoff ip address/cm?cmnd=Power%20On

my ip address is 192.168.1.134 so I enter

http://192.168.1.134/cm?cmnd=Power%20On

Note: %20 s a space.

sonoff-on-off-http

MQTT Default Topics

The switch publishes and receives information on several topics they are:

Topic Sent by Use
cmnd PC,Smart Phone using an MQTT client To control the Sonoff,set configuration and get  status information
stat Sent by the Sonoff Sent in response to a command
tele Status information Report status information at set intervals

The above topics are prefixed with the switch topic e.g.

topicbase/stat/

MQTT Commands and Responses

Besides turning the switch on and off, commands can be used to set various settings like MQTT broker, port retain flag etc and the get information about the switch.

The switch uses a command response structure. Each command is acknowledged with a response.

MQTT commands use the topic structure:

topicbase/cmnd/COMMAND

the result of the command uses the topic

topicbase/stat/RESULT

The result is contained in the message payload.

The screenshot below shows the command response messages for setting the MQTT retain flag on the mqtt client.

sonoff-command-response

The screenshot below show the status messages that the Sonoff sends at regular intervals,

Sonoff-status-messages

Controlling Using Python Demo Script

If you use python I have written a short demo script that you might find useful. You can download it here.

Notes

Because changing certain parameters results in a reboot you can change several parameters in one command using the backlog topic see MQTT Features for more details

Resources

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *