logo elektroda
logo elektroda
X
logo elektroda

[CB3S/BK7231N] Temperature/Humidity Sensor with TuyaMCU - Diagram, Reverse Engin

p.kaczmarek2 18753 55
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #31 20746949
    dotkach
    Level 4  
    is a good example of how to receive all data on request, and I noticed that in every successful example, our device sends the entire packet every time after waking up, even if there are no changes in the settings, which in turn leads to less autonomy from the accumulator, if you take away from the code "linkTuyaMCUOutputToChannel 17 val 5 1" and "linkTuyaMCUOutputToChannel 18 val 6 1" then the device only sends temperature, humidity, and when battery status changes and alarm states.

    I am trying to execute the following logic:
    if "CH7" is set to a high state through mqtt, then insert the following code "linkTuyaMCUOutputToChannel 18 val 6 1", otherwise - do not execute
    if - works improperly regardless of whether the condition is met or not, it still performs the function

    Spoiler:

    waitFor MQTTState 1
    // extra delay, to be sure
    delay_s 1

    startDriver TuyaMCU
    startDriver tmSensor

    // dpID 1 is tempererature div 10
    setChannelType 1 temperature_div10
    linkTuyaMCUOutputToChannel 1 val 1

    // dpID 2 is % humidity
    setChannelType 2 Humidity
    linkTuyaMCUOutputToChannel 2 val 2

    // dpID 3 is battery state - low(0), mid(1) and high(2)
    linkTuyaMCUOutputToChannel 3 enum 3
    setChannelType 3 ReadOnlyLowMidHigh
    setChannelLabel 3 Battery

    setChannelType 7 Toggle
    setChannelLabel 7 seting

    //
    // setup dpCache - temperature interval
    //
    // Show textfield for that
    setChannelType 5 TextField
    // setup display name
    setChannelLabel 5 Temperature Interval
    // Make value persistant (stored between reboots),
    // start value -1 means "remember last"
    SetStartValue 5 -1
    // set default value if not set
    if $CH5=0 then "setChannel 5 1"
    // link dpID 17 to channel 5, the type is val, extra '1' means that its dpCache variable
    if $CH7!=0 then "linkTuyaMCUOutputToChannel 17 val 5 1"

    setChannelType 6 TextField
    setChannelLabel 6 Humidity Interval
    SetStartValue 6 -1
    if $CH6=0 then "setChannel 6 1"
    if $CH7!=0 then "linkTuyaMCUOutputToChannel 18 val 6 1"
  • ADVERTISEMENT
  • #32 20747501
    p.kaczmarek2
    Moderator Smart Home
    Following syntax seems correct. I tested in self-test and it passes correctly:
    Screenshot of Visual Studio code editor showing a C code snippet in the openBeken_win32_msvc2017 project.
    How do you know that CH7 is received from MQTT by the time you execute the code?

    TuyaMCU might don't want to wait for MQTT to go online....

    Maybe keep ch7 in flash and receive it in one session and use in next session?
    Helpful post? Buy me a coffee.
  • #33 20750304
    dotkach
    Level 4  
    you are probably right, I tested it in different modes and found that the MCU turns off before the module has time to process everything

    how can you save the state of the channel in flash memory?
    I think that setting up such a sensor happens quite rarely, so there should be no problems with erasing the flash memory
  • ADVERTISEMENT
  • #34 20900532
    sevastianovv
    Level 5  
    >>20564338
    Good afternoon. please tell me how to prescribe it correctly.

    backlog startDriver tuyaMCU; startDriver tmSensor; linkTuyaMCUOutputToChannel 1 val 1; setChannelType 1 temperature_div10; linkTuyaMCUOutputToChannel 2 val 2; setChannelType 2 Humidity; linkTuyaMCUOutputToChannel 3 val 3; setChannelType 3 ReadOnly; linkTuyaMCUOutputToChannel 18 val 6 60; setChannelType 6 TextField; linkTuyaMCUOutputToChannel 17 val 5 60; setChannelType 5 TextField;

    Or is it like that?

    backlog startDriver tuyaMCU; startDriver tmSensor; linkTuyaMCUOutputToChannel 1 val 1; setChannelType 1 temperature_div10; linkTuyaMCUOutputToChannel 2 val 2; setChannelType 2 Humidity; linkTuyaMCUOutputToChannel 3 val 3; setChannelType 3 ReadOnly; linkTuyaMCUOutputToChannel 18 val 6 60; linkTuyaMCUOutputToChannel 17 val 5 60;
  • #35 20900536
    p.kaczmarek2
    Moderator Smart Home
    Hello, why don't you just put it in the autoexec.bat in LittleFS?
    [youtube]
    https://www.youtube.com/watch?v=kXi8S12tmC8
    [/youtube]
    Helpful post? Buy me a coffee.
  • #36 20900584
    sevastianovv
    Level 5  
    >>20564338
    Good afternoon. please tell me how to prescribe it correctly.

    backlog startDriver tuyaMCU; startDriver tmSensor; linkTuyaMCUOutputToChannel 1 val 1; setChannelType 1 temperature_div10; linkTuyaMCUOutputToChannel 2 val 2; setChannelType 2 Humidity; linkTuyaMCUOutputToChannel 3 val 3; setChannelType 3 ReadOnly; linkTuyaMCUOutputToChannel 18 val 6 60; setChannelType 6 TextField; linkTuyaMCUOutputToChannel 17 val 5 60; setChannelType 5 TextField;

    Or is it like that?

    backlog startDriver tuyaMCU; startDriver tmSensor; linkTuyaMCUOutputToChannel 1 val 1; setChannelType 1 temperature_div10; linkTuyaMCUOutputToChannel 2 val 2; setChannelType 2 Humidity; linkTuyaMCUOutputToChannel 3 val 3; setChannelType 3 ReadOnly; linkTuyaMCUOutputToChannel 18 val 6 60; linkTuyaMCUOutputToChannel 17 val 5 60;
  • #37 20900615
    p.kaczmarek2
    Moderator Smart Home
    setChannelType 6 TextField
    is optional, it's not required for core functionality. And again, why don't you use LittleFS? Please watch the video I linked.
    Helpful post? Buy me a coffee.
  • #38 20917074
    sevastianovv
    Level 5  
    Good afternoon. How do I make the date and time sent to mqtt?

    Added after 9 [minutes]:

    >>20900615
    Good afternoon. I've done everything, it's working. Thanks!
  • ADVERTISEMENT
  • #39 20921490
    Wen2024
    Level 7  
    Hello p.kaczmarek2. I have flashed my TH01 with your "tuyaMCU thermostat backup" from your post #1. I want to learn from your good running settings because i don't get any usefull data from my TH01 on any channel. All are set to 0!!! I tried the SSID and the Pass from your "Short wake up and reporting test" to get the WIFI logging. But they don't seem to be right. Can you provide me the data? Best regards!
  • #40 20922287
    p.kaczmarek2
    Moderator Smart Home
    Wen2024 wrote:
    Hello p.kaczmarek2. I have flashed my TH01 with your "tuyaMCU thermostat backup" from your post #1.

    You are not supposed to flash this file, you should rather flash latest binary from here:
    https://github.com/openshwprojects/OpenBK7231T_App/releases
    Futhermore, you need to configure your device, and first of all, check:
    - whether your device is TuyaMCU like in this topic or a non-TuyaMCU one (with deep sleep)
    - has the TuyaMCU dpIDs changed or not
    - has the TuyaMCU baud rate changed
    Are you getting any TuyaMCU transactions going on UART1 port?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #42 20941536
    Wen2024
    Level 7  
    Hello p.kaczmarek2 and @DeDaMrAz, can someone of your both sent the link of the aliexpress product from post 1? Background: I have already ordered 8 different TH01 / TH08 sensors. In spite of the description, none of them was able to determine the the change and adjust frequency of reporting temperatures (fnID17). Thank you and best regards, Wen2024
  • #43 20973055
    Wen2024
    Level 7  
    I finally got the right ones. The product description said:"The data update frequency is 1-120 minutes, and the update frequency can be selected as required". However, as the suppliers partly write off from each other, this is also no guarantee...

    Added after 16 [minutes]:

    Hello dotkach from post #33,
    you had asked the question: "How can you save the state of the channel in flash memory?" Have you made any progress with it?
    Background: The battery status is only reported by the MCU when it changes. I would like to cache the current status and report it to the server on each cycle.
    I did not find an example in the forum. What i found at the command.md is SPITestFlash_WriteStr, SPITestFlash_ReadData and SPITestFlash_Erase. But without an example, I see no chance.
    Does anyone have an example? Can anyone help?
    Best regards,
    Wen2024
  • #44 20973103
    p.kaczmarek2
    Moderator Smart Home
    dotkach wrote:

    how can you save the state of the channel in flash memory?

    Wen2024 wrote:

    you had asked the question: "How can you save the state of the channel in flash memory?

    If you set start value for given channel, for example, set start value of channel 2 to -1, it will remember the previous value.
    Screenshot of a table showing the SetStartValue function and its description.
    So, if you set channel 2 to 123 and reboot, it will be still 123.

    See also:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    Code snippet with settings for initial channel values. Screenshot of code setting initial channel values in a script.
    Helpful post? Buy me a coffee.
  • #45 20973159
    Wen2024
    Level 7  
    Great, that works straight away!!! I just have to test whether the switch from full battery to half full and then empty battery works...
    Many thanks, Wen2024
  • #46 20973232
    p.kaczmarek2
    Moderator Smart Home
    I'm glad to hear it's working. I apologize for the delay in the reply, I must have missed that. Next time feel free to ping me if I don't respond... or send a link to the topic via PM (but ask questions on forum, not on PM)
    Helpful post? Buy me a coffee.
  • #47 21233509
    xPawelski
    Level 1  
    Hello Lads,

    I've found a new way to flash this device (2.1.8 ver from tuya app) without cutting uart traces.

    I've had serious troubles with flashing it over cloudcutter. It was very slow and leaving it overnight also failed.

    After dozens of unsuccessful tries with BK7231 GUI Flash Tool and with hid_download_py I've finally learned the procedure:

    1. Used BigTreeTech BTT Writer 1.0 and BK7231 GUI Flash Tool for that.
    2. Connected green PCB's BAT+ to 3.3v, BAT- to GND, TX to TX and RX to RX (yes, that's not a mistake)
    3. Turned on Flag 26 - [UART] Use alternate UART for BL0942, CSE, TuyaMCU, etc in order to automatically enter flashing mode.
    4. Downloaded latest OpenBeken QIO
    5. Baud rate set to 1500000 (the faster the better, it increases chances of successful flashing during that short window)
    You have to repeatedly press reset button during the flashing in order to prevent device from sleeping.

    Sadly, it's still impossible to configure device after that. In battery mode AP is up for maybe 2 minutes and goes down immediately after connecting to it.
    In order to configure it, you need to power CB3S (blue PCB) directly with VCC (3.3V) and GND pins. That way AP stays forever.

    Now my problem is that even after using correct autoexec.bat (that's with tuyamcu and tmsensor drivers, dpId's 1/2/3) my sensors are not working. They are showing 0.0000 values, even over the MQTT.
    There are no TuyaMCU logs whatsoever.

    After that many attemps I've flashed device without any backup so I don't have factory params anymore.

    CB3S electronic module on a green PCB with BAT+ and BAT- markings.
  • #48 21310541
    liomio93
    Level 1  
    >>21233509 did you manage to solve this?
    I have the exact same problem with this board, in the logs i only see something the first time that i long press the button but tuyaMCU only sends the battery status and nothing else
  • #49 21330668
    przemocmax
    Level 11  
    Witam, który element w tych czujnikach temperatury

    View of a temperature sensor circuit board with component markings. wifi TUYA odpowiada za pomiar temperatury?
  • #50 21330698
    p.kaczmarek2
    Moderator Smart Home
    U4 is temperature/humidity measuring chip:
    Close-up of a circuit board with electronic components, highlighted by a large yellow arrow.
    Helpful post? Buy me a coffee.
  • #51 21330744
    przemocmax
    Level 11  
    Super dzięki, a czy jakiś namiar na pdf tego elementu?
  • #52 21332035
    przemocmax
    Level 11  
    Hello, is it possible to connect an external temperature sensor to this device, if so, which one would work appropriately?
  • #53 21333979
    p.kaczmarek2
    Moderator Smart Home
    Is your device using chip supported by OpenBeken? What is the marking on the MCU?
    Helpful post? Buy me a coffee.
  • #55 21498222
    slinucs
    Level 1  
    >>20564338 Hi, thanks for the guide. I tried to flash my sensor but I forgot to cut the tuyaMCU tracks. I flashed the firmware with ltchiptool. The firmware starts correctly, connects to mqtt and sends data, but they are always all 0. I checked the logs but could not find any entries about tuyaMCU. What could have happened? Can it be fixed or should I just throw out the sensor?
  • #56 21502290
    rodak69
    Level 11  
    Hi, I have a question from a different angle. Is it possible to add an external temperature and humidity probe to the system? If so, which pins would need to be plugged in?
    Regards

Topic summary

The discussion centers on the reverse engineering and firmware modification of a Tuya WiFi temperature and humidity sensor based on the TuyaMCU protocol, featuring a separate always-on microcontroller and a WiFi module that activates the MCU for status reporting. Key topics include the analysis of the sensor's communication protocol, schematic extraction, and adapting firmware to operate independently from the Tuya cloud, enabling integration with Home Assistant. Participants share detailed UART packet captures, discuss command 10 (ObtainDPCache) for adjusting data update frequency, and address challenges in serial port data capture and synchronization. Firmware scripting examples for autoexec.bat are provided to optimize sensor reporting intervals and power consumption, including channel linking and persistent storage of channel states using start values or LittleFS. The thread also covers flashing procedures using tools like BK7231 GUI Flash Tool, BigTreeTech BTT Writer, and ltchiptool, with emphasis on correct wiring, baud rates, and overcoming device sleep modes during flashing. Hardware identification points to the U4 chip as the temperature/humidity sensor component. Users inquire about adding external sensors and saving MQTT-received states in flash memory. The development of a bidirectional UART sniffer with TuyaMCU message decoding is also discussed, including software design considerations for real-time data capture. Overall, the thread provides comprehensive technical insights into customizing TuyaMCU-based battery-powered temperature/humidity sensors for enhanced local control and integration.
Summary generated by the language model.
ADVERTISEMENT