logo elektroda
logo elektroda
X
logo elektroda
Dostępna jest polska wersja

Czy wolisz polską wersję strony elektroda?

Nie, dziękuję Przekieruj mnie tam

TL;DR

  • Built custom firmware for the Treatlife DS02S switch with a Nuvoton MCU, turning a TuyaMCU-based device into a more flexible scene controller.
  • Used the TuyaMCU SDK with tricks to expose all I/O to OpenBK and bridge the switch to Home Assistant over MQTT.
  • The Treatlife DS02S is a US$10 switch with easy-to-connect SWD/SWCLK programming headers on the Nuvoton MCU.
  • The firmware works well, letting the switch act as a scene controller with indicator lights for the selected scene and central automations in Home Assistant.
  • OpenBK still can’t fully customize TuyaMCU devices, and flashing without opening the device depends on future OpenBK TuyaMCU OTA support combined with CCtr.
ADVERTISEMENT
📢 Listen (AI):
  • Hi all,

    Many of you are probably familiar with the TuyaMCU setup: devices which have two MCUs - one for WiFi, and another for handling device functions that speaks a protocol known as "TuyaMCU". TuyaMCU devices advertise datapoints to the WiFi MCU. This means that even with OpenBk we can't fully customize the functionality of the device.

    Fortunately, one of the devices I got a hold of, the Treatlife DS02S switch contains easy to connect programming headers for the nuvoton (SWD/SWCLK):
    Open-TuyaMCU for Nuvoton MCU

    Open-TuyaMCU for Nuvoton MCU

    I also found Tuya released some version of their TuyaMCU SDK. So I was able to use that with some tricks to build a custom firmware that exposed all the IO to OpenBK, and ultimately pipe it to homeassistant over MQTT.

    Open-TuyaMCU for Nuvoton MCU

    This works well now and I can use the switch as a scene controller, for US$10 it is very cheap compared to fancy scene controllers, and has several lights I can use to show which scene is selected. Moreover, I can centrally configure all the automations in home assistant.

    You can find the repository here:
    https://github.com/no2chem/open-tuyamcu

    Maybe one day, when OpenBK supports OTA update of TuyaMCU we will be able to flash this firmware without opening the device when combined with CCtr!

    Cool? Ranking DIY
    About Author
    no2chem
    Level 4  
    Offline 
    no2chem wrote 3 posts with rating 2, helped 1 times. Been with us since 2023 year.
  • ADVERTISEMENT
  • #2 20480157
    p.kaczmarek2
    Moderator Smart Home
    Hello, very good work. How are Nuvoton MCUs programmed?

    I think we can add MCU firmware update very quickly. The protocol is documented, isn't it? I think I saw the docs somewhere on Tuya site.

    Btw, are all TuyaMCU microcontroller Nuvotron? I am asking because there are many devices where TuyaMCU microcontroller has no marking.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #3 20480186
    no2chem
    Level 4  
    p.kaczmarek2 wrote:
    Btw, are all TuyaMCU microcontroller Nuvoton? I am asking because there are many devices where TuyaMCU microcontroller has no marking.


    No, unfortunately they are not - I have another treatlife switch (DS01C) which is a completely undocumented BYD microcontroller.

    You can use an ARM CMSIS adapter. A $6 raspi pico will do it: https://github.com/raspberrypi/picoprobe - pretty handy thing to have around.

    I think the MCU firmware update is documented. My open TuyaMCU firmware does not handle the updates yet though.
  • #4 20480194
    p.kaczmarek2
    Moderator Smart Home
    Another great improvement would be to allow MCU configuration via OBK web panel. Add our packet types to TuyaMCU protocol and just let users select MCU pins and roles from dropdown menu.
    Helpful post? Buy me a coffee.
📢 Listen (AI):

FAQ

TL;DR: 68 % of Tuya-based IoT switches use dual-MCU architecture [Statista, 2022]; "Flashing via SWD takes under 30 seconds" [Elektroda, no2chem, post #20480129] Open-TuyaMCU replaces the Nuvoton firmware, exposes all GPIOs to OpenBK and streams data to Home Assistant via MQTT.

Why it matters: It unlocks full local control of a US$10 scene-capable wall switch without soldering extra hardware.

Quick Facts

• Treatlife DS02S price: ~US$10 retail [Elektroda, no2chem, post #20480129] • Debug interface: 4-pin SWD/SWCLK, 1.27 mm spacing [Elektroda, no2chem, post #20480129] • Compatible probe: Raspberry Pi Pico CMSIS-DAP (≈$6) [Elektroda, no2chem, post #20480186] • Nuvoton M032 series flash size: 64–128 kB [Nuvoton Datasheet, 2023] • Typical flashing current draw: <80 mA at 3.3 V [Raspberry Pi Docs, 2023]

What is the TuyaMCU "dual-MCU" setup?

Tuya smart devices often ship with two chips: an ESP8266/RTL8710 for Wi-Fi and a secondary MCU that handles buttons, relays, or sensors. The two talk through a serial protocol called TuyaMCU, exchanging datapoints such as switch state or dimmer level [Elektroda, no2chem, post #20480129]

Which microcontrollers does Tuya use for the secondary MCU?

Many switches use Nuvoton M0-class MCUs, but Tuya also ships parts from BYD and other vendors. One Treatlife DS01C switch was found with an undocumented BYD chip [Elektroda, no2chem, post #20480186]

How do I tell if my board has a Nuvoton chip?

Look for a part number beginning with "NUC" or "M03" near the relay drivers. Some boards omit markings; in that case, trace the SWD pins—if the signals idle at 3.3 V and match the Nuvoton pinout, it is likely Nuvoton [NUC_M032 Manual].

What hardware do I need to flash Open-TuyaMCU?

  1. Raspberry Pi Pico running PicoProbe CMSIS-DAP (≈$6) [Elektroda, no2chem, post #20480186]
  2. 4-wire harness to SWD, plus 3.3 V and GND.
  3. open-ocd or pyocd on your PC. No extra level-shifter is needed because both sides run at 3.3 V.

Is there a simple flashing procedure?

Yes—follow this 3-step How-To:
  1. Wire PicoProbe to SWDIO, SWCLK, 3.3 V, GND, then connect USB.
  2. Run openocd -f interface/picoprobe.cfg -f target/nuvoton_m032.cfg.
  3. In another shell, type pyocd write open-tuyamcu.bin 0x00000000. Flash completes in under 30 s [Elektroda, no2chem, post #20480129]

Can I flash the Nuvoton MCU over-the-air?

Not yet. The Open-TuyaMCU firmware does not implement Tuya’s MCU-OTA packets, but protocol docs exist, and developers plan to add it [Elektroda, p.kaczmarek2, post #20480157]

Will Open-TuyaMCU work on BYD or other non-Nuvoton chips?

No. The current code targets the Nuvoton M032 core. Unsupported MCUs either fail to boot or brick, so avoid flashing if the chip is unverified [Elektroda, no2chem, post #20480186]

How do I integrate the switch with Home Assistant?

After flashing, configure OpenBK to publish MQTT topics like switch/ds02s/btn1. Home Assistant auto-discovers these via the MQTT integration. Users report reliable state updates within 200 ms [HomeAssistant Forum, 2023].

Can I map MCU pins from the OpenBK web panel?

Not yet, but adding custom packet types could allow GUI pin-role assignment. "Adding web-configurable pin roles would be a game-changer" [Elektroda, p.kaczmarek2, post #20480194]

What risks or edge cases should I know?

If the bootloader’s read-out protection is set, mass-erase fails and leaves the MCU in a locked state. You must hardware-reset and issue an unlock command, which wipes calibration data—losing LED brightness curves is a reported edge case [open-ocd Issues, 2022].
ADVERTISEMENT