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
- Download Debian/Raspbian Buster Lite
- Use Etcher.io to load image on to SD Card
- Place a file called ssh on the boot partition (ensure there is no file extension)
- Open
config.txt
- Go to the bottom and add
dtoverlay=dwc2
as the last line in the file
- Go to the bottom and add
- Open
cmdline.txt
- After
rootwait
, add a space and then addmodules-load=dwc2,g_serial
- 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"
}
- Put SD Card into RPi and boot
- SSH into it using
[email protected]
, password israspberry
- Run
sudo raspi-config
- Set Locale/Timezone
- Change password
- Change hostname (which will become the new mDNS name instead of
raspberrypi
) - Go to Advanced settings and resize partition
Set up the Raspberry Pi ZeroW Serial Gadget to accept logins
- Run
sudo dmesg
to verify that it bound theg_serial
driver - Enable the tty service
sudo systemctl enable [email protected]
- Reboot
sudo reboot
- After the Raspberry Pi reboots, connect to it via SSH again and verify that the service is running
sudo systemctl is-active [email protected]
- 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.
- Open PuTTy
- Select the port and configure it for 115200 baud, 8N1 (8-bit No-parity 1-stop)
- 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)
sudo apt-get update && sudo apt-get upgrade
- Install git to clone the GitHub repository
sudo apt-get install git
- 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)
- 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
- Download
trunc.exe
- Run
trunc bootloader.bin 32768
trunc partition-table.bin 24576
ota_data_initial.bin
has already been padded to 8K.
- Now glue them all together;
copy /b bootloader.bin+partition-table.bin+ota_data_initial.bin+your_program.bin thirdparty.bin
- Place
thirdparty.bin
in yourfiles
directory within thetuya-convert
folder from the previous section. Note - thethirdparty.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;
- Power up the Tuya device.
- Connect to the Raspberry Pi via the USB serial connection and PuTTy.
- Start the Tuya-Convert program
cd tuya-convert
./start_flash.sh
After it has started up, you will need to;
- Connect your smart phone to the
vtrust-flash
network (it will tell you when to do this) - Put the Tuya device into configuration mode – usually by holding down the primary button for about 8 seconds; the LED should flash fast.