Converting a Tuya power plug to ESPHome device

I wanted to add some power consumption measures to my Openhab installation. But as for now, the majority of my installation relies on 433 Mhz devices and I did not found RFlink compatible ones that could send power consumption. By seing “Tuya Wifi” power plug on Aliexpress, I wanted to try one. Of course, these kind of device is “vendor locked”… unless you open it and flash an alternative firmware.

In fact, there are a lot of similar models but they may house different microcontrollers : esp (32 / 8266), beken (bkxxx, blxxx), Realtec (rtlxxx), etc. The exact chip can usualy be identified with a quick search on the internet, and I discovered several opensource alternative firmwares. Each one support a limited set of microcontrollers (OpenBK7231T, Tasmota, libretiny, etc.).

Opening the device

Note that the model I ordered had no specific brand, and may not be available anymore. In general, they are pretty easy to open with a pair of pliers by just pulling a bit the plastic.

There is only one skrew that maintains the PCB (at the botton of the photo). I just removed it and the board was then easily removable.

The important mention was “T102_V1.1”. A google search brung me to the documentation of this microcontroller which is a Realtek RTL8710BN:

Preparing for ESPHome

According to the documentation, here is the required pins :

I soldered some dupond cables directly to the chip as it can be flashed in place:

To be able to flash the microcrontroller, it has to be put in “download mode”. This is triggered by putting TX2 to ground, powering on then releasing TX2 from ground.

This mode allows to get informations, dump the firmware or upload a firmware via the DEBUG UART port (TX2 / RX2).

Like the documentation advices, I used a FT232RL usb to serial adapter because the other ones I have did not pass the initial handshake (timeout).

Before trying to flash ESPHome, which is the only opensource available alternative, I wanted to test the connection and backup the original firmware. To do so I installed libchiptool via python’s PIP tool.

With this tool installed, I checked if the communication was working correctly:

Then I dumped the original firmware:

Flashing ESPHome

Despite ESPhome recommendation is to install from git, I prefered to use python’s PIP to do so.

Then I created a configuration file for the device and followed the instructions.
Board type is rtl87xx and board id is t102-v1.1

With the Yaml file, I compiled ESPhome firmware for the device :

And finaly uploaded this firmware to the device, using the same trick as for dumping (TX2 to ground, power on, then release)

Here is the output I got :

At this time a reset (unplug / replug) and voila ! The smartplug was running ESPHome.
However, it just connected to the Wifi and was not reporting nor doing anything because the ESPHome Yaml file was just containing the bare minimum. EspHome need to know how to read sensors and how to drive the relay with GPIOs.

Chance is that someone actually documented a very similar device from another brand. In fact they are the same, so I used a similar YAML file to compile a new firmware and uploaded it.

Unfortunately, the boot process stop complaining that “Change interrupts not supported”. This is a known issue that have been reported, but there is a workaround : https://github.com/libretiny-eu/libretiny/issues/155

After the wiring_irq.c file modified as stated in the issue’s comments, the device booted correctly. Note that MQTT is not yet implemented for RTL87XX.

Integration with OpenHab

This is the easiest part as an EspHome binding already exists. So In order to use this Esphome driven smartplug I just did the following in Openhab:

  • Installl the “ESPHome Binding for the native API”
  • Add a thing and choose the ESPHome Binding
  • Choose the “Add manually” option by selecting “ESPHome Device”
  • Enter the hostname eg. “smartplug.local.lan” and the API password from the YAML file

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.