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.
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).
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.
Infos
- Tasmota project: https://github.com/arendst/Sonoff-Tasmota
- Sonoff S20 schematic: https://www.itead.cc/wiki/S20_Smart_Socket
- Tasmota on GitHub: https://github.com/arendst/Sonoff-Tasmota/releases
- ESPTool on GitHub: https://github.com/espressif/esptool/releases
- Tasmota commands: https://github.com/arendst/Sonoff-Tasmota/wiki/Commands
- Counterfeit FTDI chips: https://vilimpoc.org/blog/2016/05/04/esptool-usb-serial-adapter-shootout/
- Changing the FTDI PID: http://www.rei-labs.net/changing-ftdi-pid/
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.