Software apps and online services
Hand tools and fabrication machines
In my bathroom, I had used a Sonoff Touch T1 2CH light switch with Tasmota on it, but I really didn't get along well with the touch. The surfaces were just too small for it to be used in a hazzle-free manner. So I decided to replace it with something that has actual buttons again.
Sadly, Sonoff didn't offer anything that would fit my needs, so I ended up with a Smart Life/Tuya wall switch which, according to specs is using an ESP8266 and that should be compatible with Tasmota.
Ok, but how would I get Tasmota on it? Some research on the net revealed that there is a project called tuya-convert. While it didn't seem to list the actual piece of hardware it is said to flash Tuya devices with Tasmota, over the air, no soldering required, so why not giving it a shot. As you can probably guess by the parts list and the picture, that didn't work out so well for me. Not to blame the tuya-convert project, I messed up all by myself in that I installed the official app for this device and connected it to the switch. It immediately did a firmware upgrade of the switch and that would change the OTA mechanism in a way that tuya-convert can't deal with any more, at least at the time of this writing.
So, here's the big fat warning:
Do not connect the switch to the internet or the official SmartLife app or it will upgrade the firmware and can't be flashed to Tasmota over the air any more.
Here we are, with a Tuya light switch ready to be flashed to Tasmota using tuya-convert.
Follow the instrucations on https://github.com/ct-Open-Source/tuya-convert. I have tried it on the Raspberry pi and it seemed to have worked like a charm - well, up to the point where it would fail because of the new firmware which prevents the Tasmota image from being injected, but I managed to connect the Tuya device to the tuya-convert WiFi and actually talk to the switch.
Now, if you're lucky that's it for you... No soldering, not even opening the case. But I suppose that isn't why you ended up here, You're probably stuck with the new firmware and all you're going to see is something similar as I have reported in this issue: https://github.com/ct-Open-Source/tuya-convert/issues/185
However, no need to panic, read on instead...
OK, if you ended here like me and it is too late, you already got the latest firmware, or if your newly bought device comes with the latest firmware already preinstalled, stay calm, no need for getting it replaced.
IMPORTANT: Before you do anything here, unplug the switch from the main power supply. It is not required to have it plugged to the main power supply for flashing and in fact, it will likely destroy the device doing so, putting your hardware and your health at risk. So, this entire chapter goes without mains connected, just using the 3.3V power from the Raspberry Pi, promised?
Let's have a look at the schematics from Tyua as provided on https://docs.tuya.com/en/hardware/WiFi-module/wifi-e3s-module.html
So, it is the TYWE3S, which is an ESP8266 as far as I understand and other gurus at Tasmota have flashed similar devices already. for example the Tuya Dimmer as documented on https://github.com/arendst/Sonoff-Tasmota/wiki/Tuya-Dimmer:
This Tuya dimmer has the same chip on it, but a different layout. It works nevertheless, If you have a flasher, solder on the wires like in that picture. Note that GPIO-0 must be connected to GND in order to enable flash mode, but not connected for normal operation. So I made sort of a bridge with the cables where I could plug/unplug the GPIO-0 wire on demand (The red short cable in the picture).
Now, let's use the Raspberry pi to build the flasher. Install a standard raspbian (or Ubuntu - anything should work really - I have used the nymea image, given that I had that one already installed for nymea) on the RPi.
The one thing we have to do is to enable UART on the RPi. For that, edit configtxt with
sudo nano /boot/config.txt
and add the following line:
We also need to disable some services to not block the UART port:
sudo systemctl stop serial-getty@ttyAMA0.service sudo systemctl disable serial-getty@ttyAMA0.service
When done, reboot the RPi and verify that the serial port is available.
NOTE: Depending on whether you are using a RPi 2 or RPi 3, the serial port might be found at /dev/ttyAMA0 or /dev/ttyS0. Replace this accordingly throughout the rest of this howto. I had used a RPi2 and hence my log outputs will show /dev/ttyAMA0 all the time.
sudo pip install esptool
And download the latest Tasmota firmware from https://github.com/arendst/Sonoff-Tasmota/releases. Just use the latest firmware and the language of your choice. For example:
Now, let's look at the UART setup on the RPi:
This above layout displays the generic RPI pin layout, UART RX and TX being at pins 8 and 10. For GND and 3.3V we have multiple options but make sure to not use the 5V pins as the chip requires 3.3V only and will get damaged by 5V. Let's map it to the TYWE3S:
|----------------------------------------------------| | | RPi | TYWE3S | Remark TYWE3S | |----------------------------------------------------| | GND | GND (25) | GND | Bottom right | | TX | TXD (8) | RXD0 | Second from top, right | | RX | RXD (10) | TXD0 | Top right | | 3.3V | 3.3V (1) | VCC | Bottom left | |----------------------------------------------------|
Note that TX and RX are swapped between the RPi and the TYWE3S! This makes sense as one device needs to read what the other writes and vice versa.
NOTE: There's one caveat with this: Given we're using the 3.3V line, the flowing current seems to be more than the RPi can actually deal with and if you connect the 3.3V line while the RPi is on, it will reboot. There are a few ways around here, the recommended one seems to use an external 3.3V power supply, however, I don't have one available so that's not an option for me. We can still make it work with the RPi though. Just power down your RPi unplugging the micro USB, then connect all the wires and power it on. Do not forget to close the GPIO-0 <-> GND bridge to enable flash mode. The TYWE3S will stay in flash mode long enough for the RPi to finish booting and you typing all the commands.
When the RPi has completed booting, log in, and fire up esptool. First erasing the flash completely. Needless to say, after this step the original firmware is gone and the switch will not work any more until Tasmota has been flashed and configured.
nymea@nymea ~# esptool.py --port /dev/ttyAMA0 erase_flash esptool.py v2.5.1 Serial port /dev/ttyAMA0 Connecting.... Detecting chip type... ESP8266 Chip is ESP8266 Features: WiFi MAC: a4:cf:12:a3:5f:a8 Uploading stub... Running stub... Stub running... Erasing flash (this may take a while)... Chip erase completed successfully in 4.7s Hard resetting via RTS pin...
Ok, next step is to flash Tasmota which is very similar to erasing the chip. As we cannot just power cycle the switch because it would reboot the RPi too, we need to shut down the RPI and power cycle the entire thing. For some reason I had some issues trying to power cycle too fast... Give capacitors a chance to free up by waiting some seconds before plugging it back in again.
nymea@nymea ~# esptool.py --port /dev/ttyAMA0 write_flash -fm dout 0x0 sonoff-basic.bin esptool.py v2.5.1 Serial port /dev/ttyAMA0 Connecting..... Detecting chip type... ESP8266 Chip is ESP8266 Features: WiFi MAC: a4:cf:12:a3:5f:a8 Uploading stub... Running stub... Stub running... Configuring flash size... Auto-detected Flash size: 1MB Compressed 483664 bytes to 330622... Wrote 483664 bytes (330622 compressed) at 0x00000000 in 30.2 seconds (effective 128.0 kbit/s)... Hash of data verified. Leaving... Hard resetting via RTS pin...
The final result should look something like this.
If all went well, power down everything again. Now, unconnect GPIO-0 from GND to put the switch into normal boot mode and power everything up again.
Now you should be able to connect to the switch via miniterm:
nymea@nymea ~# miniterm.py /dev/ttyAMA0 115200 -e
If you get an answer, we're good. You can now proceed with connecting it to your WiFi. First let it know about your WiFi SSID. Note, even if your WiFi name has spaces in it, do not quote it.
ssid This is my WiFi
Then set the password. Same goes here, do not quote it, even if it contains spaces.
password This is my WiFi password
If all goes well, it should print that it is connected and let you know about the IP it received in your network. Note it down.
Next thing we have to do is to configure Tasmota for this chip, given it is not officially supported upstream (yet?). For that, open a web browser in the same WiFi and enter the IP address of the device. You should see the Tasmota web interface. However, it won't have the ON/OFF buttons yet. Also, don't be surprised if your switch makes a lot of clicking noises... The default configuration doesn't work properly and afaict it switches the switch all the time while it would just want to flash a LED. We'll sort that out now. In the web interface, navigate to "Settings". From there, open "Device settings". Now pick the "Generic (18)" device and configure things as listed:
GPIO0 -> None (0) GPIO1 -> Led2i (57) GPIO2 -> None (0) GPIO3 -> Button1 (17) GPIO4 -> Relay2 (22) GPIO5 -> Button2 (18) GPIO12 -> None(0) GPIO13 -> Relay1 (21) GPIO14 -> Led1i (56) GPIO15 -> None (0) GPIO16 -> None (0)
Hit save and wait for the device to reboot. Once it's back, it should be functional. The only thing that doesn't seem to do exactly what I'd expect is the LEDs... I haven't managed to find a properly working configuration for that. If you do so, please let me know in the comments.
That's nearly it. Assuming you have a nymea setup up and running (If not, check it out at wiki.nymea.io - it's pretty awesome), all you have to do now is to go to "Manage things", add a Tasmota Device, enter it's IP and click OK. Nymea will take care of all the required MQTT setup and whatnot.
And that's it! The final result, fully integrated in my home automation setup.
In order to improve how the switch behaves, I'd recommend to go to the Tasmota web interface, to "Console" and execute this command:
This will cause the switch to turn lights already when pressed, instead of the default, which only triggers the actual switching when releasing the button.