logo elektroda
logo elektroda
X
logo elektroda

Tuya Wifi Temp/Humidity Sensor BK7231N / S8310 (CTH8310) / 2xAAA Battery / No Display / PCB_576P1-v1

lktrdng 1161 9
ADVERTISEMENT
  • Helpful post
    #1 21387221
    lktrdng
    Level 4  
    I got this sensor from Aliexpress and installed openbeken on it using some soldering via serial.

    Images
    Chips
    Image of a PCB with highlighted BK7231N and SHT30 chips.

    PCB side A
    View of the backside of PCB 576P1-V1.0 with wires attached.

    PCB Side B
    PCB with electronic components and circuitry.

    Case
    Temperature and humidity sensor disassembled into parts.

    Manual
    User manual for a temperature and humidity sensor. User manual for ZigBee TH sensor on a table.

    Output of BF7231 Flasher
    [syntax=json]
    {
       "tmp_min":"-100",
       "hum_sen":"5",
       "alt_pin_pin":"16",
       "tran_t":"15",
       "net_led_pin":"26",
       "hum_min":"0",
       "jv":"2.0.1",
       "bt_lv":"0",
       "alt_pin_lv":"1",
       "i2c_sda_lv":"0",
       "samp_sw_pin":"17",
       "max_V":"3000",
       "tmp_max":"600",
       "min_V":"2350",
       "samp_sw_lv":"1",
       "cfg_en":"0",
       "tmp_sen":"5",
       "i2c_sda_pin":"9",
       "bt_pin":"20",
       "rep_t":"1",
       "rstcnt":"3",
       "module":"CBU",
       "s_type":"1",
       "i2c_scl_pin":"6",
       "rep_t2":"60",
       "net_t":"180",
       "samp_type":"1",
       "net_led_lv":"1",
       "i2c_scl_lv":"0",
       "samp_pin":"23",
       "alarm_st":"1",
       "hum_max":"100",
       "crc":"87"
    }
    [/syntax]
    


    [syntax=text]
    Device configuration, as extracted from Tuya: 
    - ALT pin on P16
    - Battery Relay on P17
    - Battery Max Voltage: 3000
    - Battery Min Voltage: 2350
    - I2C SDA on P9
    - Button (channel 0) on P20
    - I2C SCL on P6
    - Battery ADC on P23
    Device seems to use Battery Driver. See more details here: https://www.elektroda.com/rtvforum/topic3959103.html
    Device seems to be using CBU module, which is using BK7231N.
    And the Tuya section starts, as usual, at 2023424
    [/syntax]


    Related posts
    Similar devices
    https://www.elektroda.com/rtvforum/topic4058406.html
    Calibration command
    https://www.elektroda.com/rtvforum/topic3971252.html#20539841

    Where to buy
    https://es.aliexpress.com/item/1005007861419682.html

    After playing a little bit i come up with this config:
    [syntax=json]
    {
      "vendor": "Tuya",
      "bDetailed": "0",
      "name": "Tuya TH Sensor",
      "model": "enter short model name here",
      "chip": "BK7231N",
      "board": "PCB_576P1-V1.0",
      "flags": "0",
      "keywords": [
        "Tuya",
        "CHT8310",
        "TODO"
      ],
      "pins": {
        "6": "CHT83XX_SCK;1;0",
        "9": "CHT83XX_SDA;1;2",
        "17": "BAT_Relay;5",
        "20": "dInput;43",
        "23": "BAT_ADC;40",
        "26": "WifiLED_n;37"
      },
      "command": "backlog PowerSave 1; startDriver battery; startDriver CHT83XX; CHT_calibrate -5 19",
      "image": "https://obrazki.elektroda.pl/5076675700_1736544409.jpg",
      "wiki": "https://www.elektroda.com/rtvforum/topic4098816.html"
    }
    [/syntax]


    Calibrations:
    Battery, as per BK7231 flasher text extracted from tuya
    Battery_Setup 2350 3000


    Temperature
    Comparing with a bunch of other chinese sensors and calculating the average :P
    CHT_calibrate -5 19


    Im posting this to have it for future reference and to update later with changes.

    EDIT 2025-02-07: Autoexec script
    Operation modes:
    Loop mode: The device will turn on with the wifi led also on, publish mqtt, go to sleepp 600seg and start again.
    Maintenance mode: The device will stay on for 600s with the led blinking, then reboot to normal mode. This 600s must be enough to do any required change in the configuration while avoiding keep the device powered on too much to kill the batteries.

    To enter maintenance mode, you press the internal button while the led is on. If the device is sleeping, when you press the internal button it will turn on, an then, while led is on you can enter maintenance mode.
    To exit maintenance mode, just wait 600s or press the internal button while led is blinking.

    
    // Pins and Channel definitions
    SetPinRole 6 CHT83XX_SCK
    SetPinChannel 6 1
    
    SetPinRole 9 CHT83XX_SDA
    SetPinChannel 9 1 2
    
    SetPinRole 17 BAT_Relay
    SetPinChannel 17 5
    
    SetPinRole 20 dInput
    SetPinChannel 20 43
    SetChannelLabel 43 intButton 1
    
    SetPinRole 23 BAT_ADC
    SetPinChannel 23 40
    
    SetPinRole 26 WifiLED_n
    SetPinChannel 26 37
    
    // Internal Channels definition
    setChannelType 44 ReadOnly
    //SetChannelVisible 43 0
    setChannelLabel 44 LoopMode 1
    // LoopMode=1 for start
    setChannel 44 1 
    
    //Always save power
    PowerSave 1
    
    // hold button to get into safe mode
    addEventHandler OnHold 20 SafeMode
    
    // Battery
    startDriver battery
    Battery_Setup 2350 3000
    
    // Temp and Hum sensor 
    startDriver CHT83XX
    CHT_calibrate -2 12
    
    // 30s timeout to deep sleep 2mins if waits never complete
    addRepeatingEventID 30 -1 5 DeepSleep 120
    
    //set wifi light on to indicate that deepsleep still can be cancelled
    SetPinRole 26 Rel
    setChannel 37 1 
    
    //to cancell the deepsleep by pressing the internal button
    //TODO: make led blinking to indicate the "operating mode"
    addChangeHandler Channel43 == 0 backlog echo "exit loop mode"; setChannel 44 0; setChannel 37 0
    
    // wait until wifi is connected
    //waitFor WiFiState 4
    //echo "WiFiState"
    // wait for MQTT to connect
    waitFor MQTTState 1
    // wait for NTP to sync
    //waitFor NTPState 1
    
    cancelRepeatingEvent 5
    echo "cancel event 5"
    
    publishChannels
    echo "!!!!!!!!!!!!!!channels published !!!!!!!!!!!!!!!!!!"
    delay_s 5
    // turn led OFF, going to sleep
    setChannel 37 0
    
    // Deep sleep (shut down everything) and reboot automatically when in loop  mode
    if $CH44==1 then PinDeepSleep 600
    
    //////// if not in loop mode
    clearAllHandlers
    
    // blink the led
    addRepeatingEvent 1 -1 setChannel 37 !$CH37
    
    // stay in mantenance mode 5 mins and then go back to loop mode
    addRepeatingEvent 600 -1 restart
    
    //reboot if button pressed
    addChangeHandler Channel43 == 0 backlog echo "exit maintenance mode"; restart
    
    
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #3 21394945
    cerbero85
    Level 3  
    Hi lktrdng,
    thanks for your post.

    I bought the same device and I used your configuration.
    I only changed the calibration command:
    CHT_calibrate -1 4


    Did you use a MQTT broker for sending the data?
    Did you notice a high battery drain?
  • #4 21395271
    lktrdng
    Level 4  
    >>21394945
    You listed all my pendings :P
    Once I set up my homeassistant (every time I turn it on, I lose dns resolution on my network) I will work on this.
    The idea is to create an autoexec.bat which waits for wifi, waits for mqtt, sends the data, and goes to sleep for a couple of minutes.
    And yes, batteries are dying like flies if I keep it always on and connected to wifi.
  • #5 21396514
    cerbero85
    Level 3  
    Thanks for your tricks.

    I looked for the "autoexec.bat" and I tried this:

    
    // start some drivers
    startDriver CHT83XX
    startDriver battery
    
    // hold button to get into safe mode
    addEventHandler OnHold 20 SafeMode
    
    // battery
    Battery_Setup 2350 3000
    Battery_Measure
    
    // wait until wifi is connected
    waitFor WiFiState 4
    
    // go to deep sleep if mqtt can't connect
    addRepeatingEventID 30 -1 1337 DeepSleep 600
    
    // wait for MQTT to connect
    waitFor MQTTState 1
    
    cancelRepeatingEvent 1337
    
    // wait 5 seconds
    delay_s 5
    
    publishChannels
    delay_s 5
    
    // Deep sleep (shut down everything) and reboot automatically
    DeepSleep 120
    


    Tomorrow I will post here a feedback with the results of Home Assistant

    Added after 8 [hours] 43 [minutes]:

    This is a chart from home assistant (7 hours of logged with the autoexec.bat above):

    Graphs showing voltage and temperature of Tuya TH sensor recorded by Home Assistant over 7 hours.
  • #6 21428957
    lktrdng
    Level 4  
    >>21396514 I finally had time and made my autoexec.bat. I'm sharing in case someone finds it useful.

    The code and philosophy of operation is included in the initial post
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #8 21429146
    insmod
    Level 22  
    >>21428957 Since this is a CHT8310 model, you can try to configure it to use ALERT pin for waking up.
    Looking at tuya config, it seems to be wired to P16.
    Configure P16 as dInput/dInput_n
    Replace DeepSleep 120 with this:
    
    CHT_Alert 0.2 0 60 2
    PinDeepSleep 7200
    

    Look for CHT_Alert explanation here: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    You can tweak some values for longer battery life.

    Suggestion: move addRepeatingEventID before "waitFor WiFiState 4", so it would still go to sleep if failed to connect to wifi.
  • #9 21429169
    lktrdng
    Level 4  
    >>21429146
    I was exploring exactly that cht_alert feature!! Are you looking at my screen now?? :P I'm still trying to figure out what's the queue for...
    As for wifistate, I have that line commented out because it's implicit in mqttstate 1 as per my use case :D

    Now I'm fighting to figure out how to calibrate it, since I'm getting lower measurements when looping than when keeping the device on. I guess because of the hot of others stuff on it...
    Any advice will be welcomed
  • #10 21429187
    insmod
    Level 22  
    >>21429169 Queue is for like when you configured alert at 25.5 and 0.1 difference and queue number is 2. So now queue value is 0. If sensor measured that now is 25.6, value is incremented by 1. Still, since configured number is 2 alert won't be triggered. If next measure is 25.5, then value is reset to 0, and if it's 25.6, then value is incremented again, and since value is now equals configured number - alert is triggered.

Topic summary

The discussion revolves around the Tuya WiFi temperature and humidity sensor (model CHT8310) that was modified with OpenBeken firmware. Users shared their experiences with the device, including the installation process via soldering and serial connection. Key topics included battery life concerns, MQTT integration for data transmission, and the creation of an autoexec.bat script to manage device operations efficiently. Suggestions for optimizing battery usage included configuring the ALERT pin for waking the device and adjusting sleep settings. Users also discussed calibration issues and the impact of device temperature on measurements.
Summary generated by the language model.
ADVERTISEMENT