Tuya Convert - mriksman/esp-idf-homekit GitHub Wiki

If you buy a light switch from AliExpress and it supports Tuya (or 'Smart Life'), then you can flash it with your own ESP-IDF based program.

Prepare a Raspberry Pi Zero W

Raspberry Pi Zero W is a good choice for this, as it has a USB port which allows it to be configured as a ‘Serial Gadget’, thus allowing you to PuTTy via a COM port connection. This frees up the Wi-Fi to be used as the access point for the flashing process.

Install Raspberry Pi

  1. Download Debian/Raspbian Buster Lite
  2. Use Etcher.io to load image on to SD Card
  3. Place a file called ssh on the boot partition (ensure there is no file extension)
  4. Open config.txt
    1. Go to the bottom and add dtoverlay=dwc2 as the last line in the file
  5. Open cmdline.txt
  6. After rootwait, add a space and then add modules-load=dwc2,g_serial
  7. Create a file called wpa_supplicant.conf, and put the following in it
country=AU
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
p2p_disabled=1

network={
    ssid="NETWORK-NAME"
    psk="NETWORK-PASSWORD"
}
  1. Put SD Card into RPi and boot
  2. SSH into it using [email protected], password is raspberry
  3. Run sudo raspi-config
    1. Set Locale/Timezone
    2. Change password
    3. Change hostname (which will become the new mDNS name instead of raspberrypi)
    4. Go to Advanced settings and resize partition  

Set up the Raspberry Pi ZeroW Serial Gadget to accept logins

  1. Run sudo dmesg to verify that it bound the g_serial driver
  2. Enable the tty service sudo systemctl enable [email protected]
  3. Reboot sudo reboot
  4. After the Raspberry Pi reboots, connect to it via SSH again and verify that the service is running sudo systemctl is-active [email protected]
  5. Reboot sudo shutdown now

Connect to the Raspberry Pi using the Serial Gadget

Ensure that your USB cable is data cable and not just a USB power cable. Plug in the USB cable from your computer to the Raspberry Pi microUSB connector labeled USB, not the PWR connector. On your computer you'll see a new Serial port is created.

  1. Open PuTTy
  2. Select the port and configure it for 115200 baud, 8N1 (8-bit No-parity 1-stop)
  3. Connect to the Raspberry Pi and log in. You may have to hit return a few times to get it to come up with the login prompt.

You've now confirmed that you can log in via serial.

Prepare Tuya-Convert Software

Log on using either method (via IP or serial)

  1. sudo apt-get update && sudo apt-get upgrade
  2. Install git to clone the GitHub repository sudo apt-get install git
  3. Install and configure Tuya-Convert
git clone https://github.com/ct-Open-Source/tuya-convert
cd tuya-convert
./install_prereq.sh

Preparing Firmware

The Tuya-Convert comes with a copy of Tasmota and ESPurna bin files. You can also use a bin file created in Arduino or any other third party.

For any ESP-IDF Project (these assume Windows OS and VSCode)

  1. Copy the following from the build directory, and place it in a separate folder;
bootloader\bootloader.bin
partiton_table\partition-table.bin
ota_data_initial.bin
your_program.bin
  1. Download trunc.exe
  2. Run
trunc bootloader.bin 32768
trunc partition-table.bin 24576

ota_data_initial.bin has already been padded to 8K.

  1. Now glue them all together; copy /b bootloader.bin+partition-table.bin+ota_data_initial.bin+your_program.bin thirdparty.bin
  2. Place thirdparty.bin in your files directory within the tuya-convert folder from the previous section.   Note - the thirdparty.bin must be less than 512KB.

Flashing Tuya Device

The Wi-Fi interface (wlan0) needs to be freed up for Tuya Convert to use to flash your Tuya smart devices. You will connect to the Raspberry Pi with the wired USB Serial Gadget and will no longer want it to connect to your Wi-Fi network automatically. Disable your Wi-Fi network configuration by renaming the Wi-Fi configuration file:

mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf-saved
sudo reboot

You can now proceed with the flashing process;

  1. Power up the Tuya device.
  2. Connect to the Raspberry Pi via the USB serial connection and PuTTy.
  3. Start the Tuya-Convert program
cd tuya-convert
./start_flash.sh

After it has started up, you will need to;

  1. Connect your smart phone to the vtrust-flash network (it will tell you when to do this)
  2. Put the Tuya device into configuration mode – usually by holding down the primary button for about 8 seconds; the LED should flash fast.