logo elektroda
logo elektroda
X
logo elektroda

TuyaMCU flashing, setup and configuration guide - configure dpIDs for Home Assistant

p.kaczmarek2 9048 18
ADVERTISEMENT
  • Block diagram of MCU and Wi-Fi module cooperation.
    Here I will show you step by step how to configure a TuyaMCU device, how to get dpID list, how to map dpIDs to channels and publish them to Home Assistant and how to create custom page for the device. This topic will cover most of TuyaMCU topics required to get your Tuya device running cloudless with HA.

    But first of all - do not flash device first, start with reading this guide. This is because one of dpID extraction methods requires an unflashed device

    0. Do generic research
    The first step is obviously to figure out what TuyaMCU is. This is why some basic reading is recommended first.
    - read our writeup: TuyaMCU protocol - communication between the microcontroller and the WiFi module
    - read Tuya docs: https://developer.tuya.com/en/docs/iot/tuya-c...-serial-port-access-protocol?id=K9hhi0xxtn9cb
    - open our devices list, tick "detailed" checkbox and search for "TuyaMCU", read the found teardowns
    - read our autoexec samples page, at least ones related to TuyaMCU https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    For generic OBK knowledge, please:
    - read our docs: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md
    - watch our flashing guides: https://www.youtube.com/@elektrodacom

    1. Check is your device using TuyaMCU
    Now you need to know is your device using TuyaMCU. So,
    tTry to answer the following questions:
    - when you open device case, is there an extra MCU in SOIC or TQFP case (or similiar) near WiFi module?
    - is WiFi module connected to MCU via RX1/TX1 (UART1) lines?
    - when you do a config extraction with our flasher, does it find a baud setting in the flash?
    - when you have flashed your device already, does your device has still functional buttons, even without configuring them in OBK?
    - does your device has a screen?
    If you answered YES to some of the questions, there is a chance your device is TuyaMCU.

    2. Determine baud setting
    The best way to find out baud setting is to use our flash tool: our flasher, just get a 2MB flash backup of device taken by UART or just download Tuya config partition via Tuya config extraction in OBK and drag and drop it on the flasher:
    BK7231 Easy UART Flasher interface with settings and TuyaMCU device information
    TuyaMCU baud rates are 9600 (default) and 115200 (high speed)

    3. Determine the meaning of dpIDs
    This is where having a not yet flashed device is handy, but there are mutliple ways. If you haven't flashed device yet:
    - you can just get them from Tuya
    - if you have an isolated USB to UART converter, and know how to connect it in a safe manner (because device power supply may not be isolated from mains), you can use our TuyaMCU analyzer to sniff the communication and observe which dpIDs are sent when you are doing a particular action in Tuya app, for example, change relay and observe which dpID is sent
    If you already flashed OBK:
    - you can start the driver and set the TuyaMCU baud in autoexec and then use tuyaMcu_sendQueryState command to get dpIDs from the MCU. Then, you have to observe their values and guess their meanings (for example, if thermometer is showing 21.5C, and one dpID has value 215, then you can suspect it's the temperature)
    - you can also search our devices list for similiar devices, maybe some dpID are matching
    - you can also use tuyaMcu_sendState command to send something to TuyaMCU, for example, relay value, and observe if it changes on the physical device, see command docs

    5. Start the driver and check is there a communication
    This is when you already have to flash the device. Earlier steps could be completed without flashing, but now it's the time to change the firmware.
    The main issue with changing firmware of TuyaMCU devices is that TuyaMCU uses the same port as the flashing.
    This means, that you will have to done something to sever the connection of MCU for the time of the flashing, at least in the most cases.
    There are multiple ways to do it:
    - cut the traces (RX and TX) leading to MCU and repair them after flashing
    - disconnect board with TuyaMCU (if possible, if boards are separate)
    - desolder either TuyaMCU or WiFi module (sometimes it's possible)
    - desolder resistors on RX/TX traces (if present, some devices don't have them)
    Alternatively, you could also try to look up the datasheet of the MCU and check whether it's possible to put it into the RESET state via pulling it low or high.
    Futhermote, there are also OTA flashing solutions, but they are not reliable and most of the new devices are patched.

    6. Start the driver and check is there a communication
    Now it's time to setup a basic autoexec.bat in OBK:



    Here is a starting config:
    
    // Start TuyaMCu driver
    startDriver TuyaMCU
    // set TuyaMCU baud rate
    //tuyaMcu_setBaudRate 115200
    // set TuyaMCU default wifi state 0x04, which means "paired",
    // because some TuyaMCU MCUs will not report all data
    // unless they think they are connected to cloud
    tuyaMcu_defWiFiState 4
    

    The tuyaMcu_setBaudRate is commented out, please remove comment if needed. This should give you basic TuyaMCU heartbeats and communication in the OpenBeken Web App Log. This should also make tuyaMcu_sendQueryState work.
    Do not forget the tuyaMcu_defWiFiState 4 line. By default, OBK sends "paired" state (0x04) only when MQTT is on, but some devices always require the WiFi state to be "paired" (0x04) before sending data. This will control the WiFi LED of the MCU and even may control buzzer. Some devices will send buzzer audio signals when not paired.


    7. Map the dpIDs
    In OBK, you can map dpIDs to channels.
    Channel is like a variable, they can store integers.
    You can adjust the way channels are displayed by setting their types, for example:
    
    setChannelType 1 Toggle
    

    This will create a toggle on GUI, and this:
    
    setChannelType 1 temperature
    

    this will create a temperature display.
    To see full list of channel types, check: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/channelTypes.md
    Still, to get data from MCU, you need to map dpID to channel, so:
    
    // Map given dpID to channel 1
    // dpID type value
    linkTuyaMCUOutputToChannel 24 val 1
    

    Code above maps dpID 24 to channel 1, with type value.
    There are multiple possible types.
    There are Tuya types:
    - 0-raw
    - 1-bool
    - 2-value
    - 3-string
    - 4-enum
    - 5-bitmap
    And there OBK-specific types. For example, if you want given dpID to always publish over MQTT in the hex form, you can do:
    
    linkTuyaMCUOutputToChannel 24 MQTT
    

    This will publish the value under tm/TYPE/dpID topic every time that TuyaMCU sends it.

    There are also special types for compound raw data packets, the sample below is for Voltage + Current + Power raw packet for TAC2121C device:
    
    linkTuyaMCUOutputToChannel 6 RAW_TAC2121C_VCP
    

    The code above will automatically set Voltage, Current and Power channels.
    To see complete config samples, check our autoexec samples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    You can also use our devices list:
    https://openbekeniot.github.io/webapp/devicesList.html
    Enter "TuyaMCU dimmer" in the search field (for example) and search.

    So, if you don't know the meaning of dpIDs, the workflow is the following:
    1. try to query state from the MCU
    2. write config to map received dpIDs to channels
    3. save config and reboot
    4. observe if the config works, if something is missing, do the adjustements
    Do a little steps, one at time, that way you can figure even a complicated device. Don't hesitate to ask on forum if there is a problem.

    8. Create a custom GUI
    This is purely optional, but if you want to have a custom gui on your device, you can create your own page with HTML and JS and host it on LittleFS:
    OpenBeken as a mini HTTP hosting - writing pages in Javascript, Tasmota REST
    There is a very useful feature that can be used for this purpose, it's called DP. It can be sent via HTTP from the Javascript on REST page:
    Screenshot of JSON with dpID data
    In order for it to work, you need to enable the following flag: flag 46.
    This will store last values of dpIDs in hex and allow you accessing them in JSON.
    The same command works via MQTT, it will send reply under stat/TuyaMCU/DP.


    9. Pair with Home Assistant
    There are multiple ways to do the pairing and they strongly depend on your personal preference and your particular device.
    The most simple way is to just use automatic HASS Discovery which is creating Hass entities that are based on OBK channel types and their respective name:



    The more advanced way is to write YAML config yourself and just put it in the configuration.yaml of your Home Assistant.
    There are few good tricks that may help with that, for example, you can use a special argument for linkTuyaMCUOutputToChannel to enable dpID publishing in raw hex form, so it can be processed by HA later:
    
    linkTuyaMCUOutputToChannel 6 MQTT
    

    Set it, save and reboot, and observe what happens in Home Assistant log. You can also use Home Assistant MQTT tool to watch for MQTT packets.

    Can you show sample teardown topic for TuyaMCU dimmer?
    Well, as I said, please use our devices list, but still, here is a one sample:
    OpenBeken and TuyaMCU dimmer - configuration guide/tutorial
    And here is a fan device configuration:
    BK7231T Treatlife DS03 Fan/Light Dimmer Switch: OTA Flash & Setup Guide (TuyaMCU dpIDs)
    And here is power meter teardown:
    [BK7231N ] Teardown and flashing of Tomzn TOMPD-63 WIFI (not to be confounded with TOMPD-63LW)

    Can you show a sample of custom REST page for device
    You can check out our BW-AF1 tutorial:
    OpenBeken on BW-AF1 fryer with WiFi - interior, TYWE3S/WB3S, configuration
    It has a custom REST page for the fryer control.


    Can you show a sample of custom script for device?
    Check out our TAC2121C-like setup with charging limit controller:
    Home Assistant Integration: Reflashing BK7231N in Tuya Smart DIN-rail Energy Meter TAC2121C
    The final script is at the end of the topic.

    What about battery powered devices?
    Battery powered TuyaMCU devices are special - the MCU controls the power of WiFi module there. MCU only enables WiFi module when it needs to report data. This requires a special driver called tmSensor.
    Please see a sample tmSensor guide for more information:
    Energy-saving (?) Battery-operated door / window sensor for WiFi DS06
    [CB3S/BK7231N] Temperature/Humidity Sensor with TuyaMCU - Diagram, Reverse Enginering



    Summary

    There are many ways to configure and flash TuyaMCU devices. TuyaMCU dpIDs can be extracted before flashing, either manually (by sniffing the protocol) or automatically from Tuya cloud side. TuyaMCU dpIDs can be also requested and investigated after flashing, with a query state command. TuyaMCU can be configured in a flexible way, either with stock OBK page, or with fully custom REST interface that can be hosted on any OBK device with LittleFS. TuyaMCU devices configured with OBK can be also paired with Home Assistant, both by automatic Home Discovery or by more advanced and flexible manual YAML config. There are also multiple ways to get TuyaMCU data out of OBK device - you can just use OBK channels, or you can use special MQTT dpID link, or you can use DP command to request all dpID values per request. The DP command works both via HTTP and MQTT. You can also script OBK devices to automate your setup, either with or without Home Assistant.
    Let us know if you have any TuyaMCU-related questions, we will do our best to help you configure your devices!

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11862 posts with rating 9949, helped 568 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21057937
    p.kaczmarek2
    Moderator Smart Home
    TuyaMCU update! Please see the following topic for the details of new OBK TuyaMCU feature:
    How to get dpID list of types and values for flashed TuyaMCU devices with OpenBeken
    Helpful post? Buy me a coffee.
  • #3 21159305
    morgan_flint
    Level 14  
    Hello @p.kaczmarek2 !

    Maybe there are two more possibilities, although I'm unsure about how to make them work, or if they're redundant with the other ones... perhaps the members referred below can give more information:

    - @crg1darkspr1te talked in this post about unpacking/decrypting the factory firmware, and obtaining a .json with info about the DpID's.

    - @divadiow talked in this post about the "Tuya API response for this device", and gave a .json similar to the previous one, but with a second part with more descriptive info about the DpIDs (looks very much like the one in the abbreviated method in my tutorial.

    Maybe they can give more information about their methods!
  • ADVERTISEMENT
  • #4 21228240
    io2345
    Level 7  
    Channel numbers greater than 99 seem not to be supported. I tried to use 101 and 102, as these were my corresponding dpID's, but the controls were not displayed in GUI until I changed channel number to 7 and 8.
  • #5 21229830
    p.kaczmarek2
    Moderator Smart Home
    There are 64 available channels in OBK:
    Code snippet in the new_pins.h file showing that 64 channels are available in the OpenBK7231T_App project.
    Helpful post? Buy me a coffee.
  • #6 21407813
    erdeidominik1999
    Level 8  
    Hy!
    Is tehre any way to create channel type like select menu? So I need to have for example 4 fixed selection options.
    Another question:
    I have a solar charge controller which works with tuyamcu, my problem is that some info is reported in a single dp, which are separate values, for example: the dp has the following value: 00 77 00 04 00 53, the 1st and 2nd byte is one variable, the 3rd, 4th is another, and the 5th, 6th is another. A found that RAW_TAC2121C_VCP does something like this, but there there are 8 bytes, can you @p.kaczmarek2 create a type like that but with 6 bytes?
  • #7 21441949
    doudouni100
    Level 4  
    >>21229830 Hi

    Is there any way to change it ?

    I have a Temperature and humidity sensor and the battery channel is 101 so it cannot be displayed.
  • ADVERTISEMENT
  • #8 21441971
    erdeidominik1999
    Level 8  
    >>21441949
    Do you have more than 64 channels? Why would you like to use ch101?
  • #9 21441973
    p.kaczmarek2
    Moderator Smart Home
    You don't need to change it. You can map any dpID to any channel. For example:
    
    
    startDriver TuyaMCU
    
    // cook on/off 
    setChannelType 1 Toggle
    setChannelLabel 1 "Cook"
    linkTuyaMCUOutputToChannel 111 bool 1 
    // power on/off
    setChannelLabel 2 "Power"
    setChannelType 2 Toggle
    linkTuyaMCUOutputToChannel 101 bool 2 
    
    // set temperature
    setChannelLabel 3 "New Temperature"
    setChannelType 3 TextField
    linkTuyaMCUOutputToChannel 103 val 3
    

    See samples here: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md

    Added after 54 [seconds]:

    erdeidominik1999 wrote:

    I have a solar charge controller which works with tuyamcu, my problem is that some info is reported in a single dp, which are separate values, for example: the dp has the following value: 00 77 00 04 00 53, the 1st and 2nd byte is one variable, the 3rd, 4th is another, and the 5th, 6th is another. A found that RAW_TAC2121C_VCP does something like this, but there there are 8 bytes, can you @p.kaczmarek2 create a type like that but with 6 bytes?

    RAW_TAC2121C_VCP with 6 bytes only and the same format?
    Helpful post? Buy me a coffee.
  • #10 21441988
    erdeidominik1999
    Level 8  
    >>21441973
    Yes the same with RAW_TAC2121C_VCP with 6 bytes. The types need to be Voltage_div10, Current_div10, Power_div10. But I think Current_div10 is not exists. Another type that is not exists but it needs is Percent_div10, because the battery percentage is reponter in percent with 10 multiplier.
  • #11 21442008
    doudouni100
    Level 4  
    >>21441973
    Thanks ! That did it.!
    I tried it before but I didn't change the number after "enum"

    So for anyone that has a sensor this is the autoexec:
    startDriver TuyaMCU
    startDriver tmSensor
    // dpID 27 is temperature div 10
    setChannelType 27 temperature_div10
    linkTuyaMCUOutputToChannel 27 val 27
    // dpID 46 is % humidity
    setChannelType 46 Humidity
    linkTuyaMCUOutputToChannel 46 val 46
    // dpID 101 is battery state - low(0), mid(1) and high(2)
    setChannelType 10 ReadOnly
    linkTuyaMCUOutputToChannel 101 enum 10
    setChannelLabel 10 Battery
    Close-up of a green PCB with a blue electronic module mounted on it.
  • #12 21442013
    erdeidominik1999
    Level 8  
    >>21442008
    You can also use channel 1,2,3, I think it is cleaner. It is not needed to have the same channel as tuya dp.
  • #13 21443231
    morgan_flint
    Level 14  
    doudouni100 wrote:
    startDriver tmSensor

    Do you really need to start tmSensor?
    As long as the Tuya MCU manages the interface with the sensor, Open Beken doesn't need to take care of that, I think
  • #14 21443445
    doudouni100
    Level 4  
    >>21443231

    Hey !

    Your probably right ! It makes sense.

    After trying everything I found on this forum and others with no success, I thought I'd give it a go myself and also learn something on the way.

    The "startDriver tmSensor" was there from my previous tries and I didn't question it because what do I know :)

    Once I found the proper channels with TuyaMCUAnalyzer for temp, humidity and battery I just plugged those numbers in the previous autoexec and It worked !

    I cant imagine it will do it any harm and since it is battery powered I'm not going to take it apart and try it .

    Or maybe my curiosity will win and I will take it apart now that you mentioned it :)
  • #15 21443483
    morgan_flint
    Level 14  
    doudouni100 wrote:
    I cant imagine it will do it any harm and since it is battery powered I'm not going to take it apart and try it .

    No, it won't make any harm, but you don't need to take it apart, you can edit the autoexec.bat from the web interface
  • #16 21443600
    doudouni100
    Level 4  
    >>21443483

    Like I said it is battery powered and it stays on for a few seconds to send whatever it sends and the Wi-Fi turns off to save battery,

    I will have to take it apart to force power to the Wi-Fi module so it will stay on long enough to make the changes, from the web interface.
  • #17 21443630
    erdeidominik1999
    Level 8  
    >>21443600 If you power it off and on 3 times it will goes to safe mode, and not run the autoexec, so the wifi won't turn off.
  • #18 21443655
    morgan_flint
    Level 14  
    In the case of some of my battery-powered Tuya devices (also temperature - humidity sensors) pushing the backlight button for a long period also forces WiFi to stay for a longer time. From the photos, I can't tell if yours has this kind of button
  • ADVERTISEMENT
  • #19 21474001
    erdeidominik1999
    Level 8  
    erdeidominik1999 wrote:
    >>21441973
    Yes the same with RAW_TAC2121C_VCP with 6 bytes. The types need to be Voltage_div10, Current_div10, Power_div10. But I think Current_div10 is not exists. Another type that is not exists but it needs is Percent_div10, because the battery percentage is reponter in percent with 10 multiplier.

    @p.kaczmarek2 Can you include this type?

Topic summary

This discussion provides a comprehensive guide on configuring TuyaMCU devices for integration with Home Assistant (HA). It emphasizes the importance of understanding the TuyaMCU protocol and obtaining the dpID list before flashing the device. Users are encouraged to conduct preliminary research, including reading the TuyaMCU protocol documentation and exploring device lists. Additional insights are shared regarding methods for extracting dpID information from factory firmware and the limitations of channel numbers in OpenBeken (OBK), with a note that channels above 99 are unsupported. The conversation also highlights the availability of 64 channels in OBK for device configuration.
Summary generated by the language model.
ADVERTISEMENT