Making smart devices smarter with Tasmota

Configuring WiFi

Which device or which firmware is running is immaterial if it is not reachable over your network. Most Internet of Thing (IoT) devices treat this problem in a similar manner by putting the device into a special mode that causes it to become an access point with a web server. You then connect to the access point and enter the details of your WiFi configuration, which are then subsequently used to connect to your network. Flashing the S20 with Tasmota will cause it to start up as an access point automatically the first time you power up. Now, you need to connect your laptop to the new WiFi network. In my case, the network was sonoff-3591.

Once you are connected to the access point network, you must open http://192.168.4.1 in your web browser. You will see a form similar to Figure 7 that allows you to enter your SSID and password for your local network. When this form opens, you have three minutes before the device automatically reboots. Pressing Save keeps the values and causes the device to reboot immediately.

Figure 7: Configuration dialog for WiFi.

When the device restarts, it should be reachable from your network. The device will bring up a very comprehensive menu that allows you to set up WiFi, MQTT, Domoticz, timers, and a few other settings, one of which is where you can download the most current Tasmota image when doing an OTA update (Figure 8).

Figure 8: Dialog for OTA updates.

Software, computers, and upgrades, despite rigorous testing, can occasionally encounter difficulties. Should this occur with the S20, it might be having a problem connecting to the web from the browser. If this happens, you still have a number of ways to communicate with your device. The S20 has a button that can be used for operation, such as toggling the power state; however, it can do much, much more. Most importantly, it can enable the access point. Pressing the button on the Sonoff device four times quickly causes the Sonoff to enable the access point. I couldn't find any clarification of how fast "quickly" is, but pressing the button four times in less than a second will successfully start the access point mode.

The different "commands" that can be enabled with button presses are listed in Table 1; however, if it isn't possible to connect to the Sonoff in the usual manner, this manual method might not help. Pressing the button for longer than 40 seconds will reset the settings to the defaults defined in user_config.h and restart the device, which might be enough to allow you to seize control without resorting to reflashing.

Table 1

S20 Button Press Functions

Button Presses

Function

1

Toggles Relay1 directly or sends an MQTT power-on message if MQTT is configured. Blinks the LED twice and sends an MQTT status message.

2

Toggles Relay2 directly or sends an MQTT power-on message if MQTT is configured. Blinks the LED twice and sends an MQTT status message. Relay2 is supported on devices with two relays.

3

Starts the WiFi with the ESP8266 SmartConfig app, allowing for SSID and password configuration from an Android mobile phone. The LED blinks during the config period.

4

Starts the WiFi manager, providing an access point with IP address 192.168.4.1. A web server allows the WiFi configuration. The LED blinks during the config period.

5

Starts the WiFi Protected Setup (WPS), allowing for SSID and password configuration with the router's WPS button or from a web page. The LED blinks during the config period.

6

Restarts the module.

7

Starts OTA download of firmware from the preconfigured URL. The green LED lights up during the update.

All of the options in Table 1 should make controlling the device easy. During my initial experiments, it wasn't possible to regain control even by reflashing. In extreme situations, you might need to erase memory before reflashing with:

sudo esptool.py erase_flash

Erasing the memory will write a 0xFF to each memory location. After the memory has been erased, it needs to be reflashed with the firmware. Make sure when you do this that you use the correct memory size for your device. Once the Sonoff is up and running, you can finally control it.

Sending Commands

The Tasmota firmware has a great deal of functionality to support a myriad of ESP8266-powered devices and their associated timers and sensors. Table 2 is the tiniest subset of commands from the Tasmota command list [5].

Table 2

Subset of Tasmota Commands

Command

Payload

Description

Power<n>

 

Show the current power state of the relay.

Power<n>

0/off

Turn relay or Relay<n> to off.

Power<n>

1/on

Turn relay or Relay<n> to on.

Power<n>

2/toggle

Toggle relay or Relay<n> to opposite state.

The Tasmota can be controlled by commands sent either via an MQTT broker or by sending a command to the device directly as an HTTP request. In the future, I might set up an MQTT broker or a home automation package such as Domoticz or OpenHAB, but for now, I control the devices with HTTP commands:

http://sonoff/cm?cmnd=Power
http://sonoff/cm?cmnd=Power%20On
http://sonoff/cm?cmnd=Power%20Off
http://sonoff/cm?cmnd=Power%20Toggle

It is even possible to set a password for the Tasmota web interface, which requires you to provide the username and password as part of the command:

http://192.168.178.50/cm?user=admin&password=passwordgoeshere&cmnd=Power On

Adding a password might make your devices more secure to naive attackers on your network, but it is passed in clear text, undermining your total security, especially if you access your devices over the Internet.

Controlling the Sonoff from the browser is quite neat during development, but the most flexible way would be to call a script. Although quite a few different ways of doing this probably exist, one easy example uses cURL. A number of sites discuss the full powers of cURL, but I only need to perform a simple HTTP GET:

curl http://192.168.178.59/cm?cmnd=Power%20Toggle

The command to toggle the power switch also could be added to a shell script.

Conclusion

Tasmota not only supports all the same types of functionality that most other smart socket apps support, but much, much more. Besides MQTT, it supports Domoticz home automation out of the box; more importantly, Tasmota lets you keep all your data within your home network and control the device by script or in a browser. This isn't to say I didn't encounter any difficulties during the process (see the "Troubleshooting" box), but in general I would say flashing Tasmota to my Sonoff S20 has been wildly successful.

Troubleshooting

Most of the problems I experienced stemmed from badly soldered header pins and an old version of ESPTool. During my journey of discovery, I also noted issues others encountered.

Problem

Regardless of what commands were sent to ESPTool, the executable didn't seem to be working but didn't return any errors.

Solution

The ESPTool binary from the repository might be old – or really old. Download and use the Python script [4].

Problem

ESPTool was current, but it could not communicate with the device.

Solutions

  • Bad header pin connections. I prepared two identical devices with header pins but was initially only able to communicate with one of the devices because of bad connections.
  • Cheap serial adapters. Some people complained about low-quality serial adapters not working. These adapters contained FTDI chips from unverified sources. The FTDI chips might have been counterfeit. Window drivers from the providers FTDI and Prolific have been released that do not support these counterfeit chips, although this does not seem to be a problem on Linux [6] [7].
  • Power. The problem could be not enough power provided to the unit. Some users have complained that adding an extension cable caused enough power loss that the FTDI adapter could not power the transfer.
  • Voltage. The FTDI adapter is 5.0V, not 3.3V, and fried the device.

Problem

Don't know whether the FTDI adapter is recognized by Linux.

Solution

This problem can be verified with the lsusb command:

Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 1bcf:2883 Sunplus Innovation Technology Inc.
Bus 001 Device 003: ID 8087:07da Intel Corp.
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 008: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

The second line from the bottom shows that the device was recognized.

Problem

An HTTP call to the S20 device returned strange errors, and the Sonoff didn't obey the command:

File Not Found
 **
URI: /cm/
Method: GET
Arguments: 1
cmnd: Power 0

Solution

Perhaps the password was accidentally set. When passing in the command, also pass in the username and password.

The Author

Christopher Dock is a senior consultant at T-Systems On Site Services. When he is not working on integration projects, he likes to experiment with Raspberry Pi solutions and other electronics projects. You can read more of his work on his blog http://blog.paranoidprofessor.com.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Controlling a Smart Plug

    You could spend hundreds of dollars on specialized IoT appliances and fixtures, or you could just hack a smart plug and talk to it with your Linux system.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News