logo elektroda
logo elektroda
X
logo elektroda

[BK7231N CBU] Generic Temperature and Humidity Sensor

Chasbrot 55308 359
ADVERTISEMENT
  • #91 20442835
    ShadowLight30
    Level 5  
    Since the CB2S is working I will leave it as it is ;)

    For the chip on PCB you are correct I could use these pins... But.
    Test pad P7 translates to P21/I2C1_SDA on pin 19
    Test pad P6 translates to CEN on pin 21
    I don't think the CEN pins is usable, correct?

    I'm using this https://developer.tuya.com/en/docs/iot/tuya-s...fi-and-ble-soc-board-BK7231N?id=Kao72e6net3bs for reference, there is a diagram of the BK7231N.
  • ADVERTISEMENT
  • #92 20442889
    dheenhasty
    Level 13  
    Nope you can't, i was naively thinking that p6/p7 correspond to p06/p07 ....... -_- i suppose i overestimated the design of this board.
    If you cannot use 2 free pwm you are screwed.....
  • #93 20444437
    dheenhasty
    Level 13  
    Hello,

    i added all the alert mode mecanism for the SHT. so now we can use that to wake CBU from the pindeepsleep by adding a dinput to p16 for this sensor and setting all the Alert parameter as needed.

    will try to propose a full autoexec.bat this week end with all mode included to save as much power as possible.
  • #94 20444518
    p.kaczmarek2
    Moderator Smart Home
    @dheenhasty good job, but can you also then create a separate topic here, with a proper name, explaining step by step how each command works, so we can link it from our repository? Posting this here in just a post would cause it to get easily lost for new users.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #95 20444614
    dheenhasty
    Level 13  
    Sure.

    everything is functionnal, but it clearly need more work arround the onchannelchange way to be able to control heater and limit remotely.

    Edit : Done here : https://www.elektroda.com/rtvforum/topic3958369.html#20444655

    Added after 48 [minutes]:

    @ShadowLight30 Was thinking about your issue, but maybe you can just swap the TX2 with P7. cause i think the issue is mainly due to the chip trying to communicate thru TX2 and make the CLK goes wrong on the SHT. and P7 might be more stable for that.
  • ADVERTISEMENT
  • #96 20444707
    p.kaczmarek2
    Moderator Smart Home
    Well done, please also include some kind of real life example with device and autoexec.bat.

    In a meantime, I'm working on calendar system (fire event command on given hour/minute on certain weekdays).
    Helpful post? Buy me a coffee.
  • #97 20449900
    javierlt3
    Level 4  
    Thanks in advance for all your work on this device, but I wanted to ask you if you see something strange in the configuration or something because when I integrate it into home assistant NaN appears in the temperature sensor and NaN in the humidity sensor and I do not know if it is because it does not detect anything or what happens. Maybe I have not understood correctly how to configure it, I am quite new to this.
    [BK7231N CBU] Generic Temperature and Humidity Sensor [BK7231N CBU] Generic Temperature and Humidity Sensor [BK7231N CBU] Generic Temperature and Humidity Sensor
  • #98 20449927
    dheenhasty
    Level 13  
    You just miss a script to launch the sht driver and the measurement at startup.

    You can find various example in this topic

    To do so go to launch web app menu
    Then > filesystem > create script > give it a name and open it to edit
    Here's a sample example to test it will send measurement every 5min :
    Quote:

    PowerSave
    echo "activate SHT3X Sensor"
    startDriver SHT3X

    :loop
    SHT_Measure
    delay_s 300
    goto loop



    But it will drain the battery so you will need to use deepsleep if you want your device to be off for a certain time.
    As i see your battery is already low the adc is lower than 3600 (so arround 1v per battery)

    Here's the detail for SHT command available
    https://www.elektroda.com/rtvforum/topic3958369.html

    The command for the battery management will arrive soon. Once everything is merged i will publish here 2 script to cover different use case for the CBU.
  • #99 20450547
    p.kaczmarek2
    Moderator Smart Home
    I have splitted the offtopic discussion here:
    [BK7231T/WBLC5] How to attach SHT30 sensor to smart Tuya LED?
    but if someone from SHT30 experts like @dheenhasty can help, please look into the topic above
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #100 20451180
    javierlt3
    Level 4  
    thank you!! The battery will not be a problem I will get them out and use a power adapter bcs ty!
  • #101 20456976
    dheenhasty
    Level 13  
    So as promise here's 2 example of script for this sensor depending on what you want to do with it :

    General usage temp/humidity check every 30 minutes :

    Quote:

    PowerSave
    startDriver SHT3X
    startDriver Battery
    Battery_Setup 2000 3000 2.29
    scheduleHADiscovery
    delay_s 60
    DeepSleep 1800


    Temperature Alarm (trigger if lower 18 or Upper than 21 or on button press) :
    Quote:

    PowerSave
    startDriver SHT3X
    startDriver Battery
    Battery_Setup 2000 3000 2.29
    scheduleHADiscovery
    SHT_StopPer
    SHT_ClearStatus
    SHT_LaunchPer
    delay_s 60
    SHT_SetAlert 21 18 60 30
    PinDeepSleep


    detail =>
    SHT_SetAlert > HighTemp LowTemp HighHumidity LowHumidity
    Battery_Setup LowBat HighBat v_divider(For this sensor is 2,29)

    For each script there a delay of 60 second to permit to deactivate the script if needed

    EDIT : change order of Args for battery setup to be more user friendly (https://github.com/openshwprojects/OpenBK7231T_App/pull/692 needed)
  • #102 20456982
    p.kaczmarek2
    Moderator Smart Home
    @dheenhasty , referring to our previous discussion with a friend from Ukraine, who was converting a smart LED bulb to temperature/humiditry sensor, please change the code so the SHT30 does automatic measurements every 10 or so seconds by default, and add a command to override. Something like SHT_Auto 1/0.

    The SHT30 stuff should be able to get humditiy/temperature data without writing a single line of code (script) for non-technical users.
    Helpful post? Buy me a coffee.
  • #103 20456991
    dheenhasty
    Level 13  
    @p.kaczmarek2 : https://github.com/openshwprojects/OpenBK7231T_App/pull/692 ;)

    i was thinking of adding a detection of button push to cancel the deep sleep on deepsleep rtc script.... 60 sec seems to short to access frontend

    next evolution could be adding some sort of command (slider, button) for managing the Alert mode and Heater.....

    Edit
    @p.kaczmarek2 : i encounter something strange, the deepsleep and pindeepsleep work really well on command line and on script. but if i use them in autoexec.bat file it's seems like it's not working.

    for example i create a file myroutine with one of the script up there and put it on startupcommand. no problem.
    but if i pass through creating autoexec.bat (who is executed automatically) and put script it crash ..... the delay_s is not taken in account. so it goes deepsleep too quick and go in failsafe mode.
    issue is that since it's the autoexec.bat it's a pain to remove that in failsafe (lfs not accessible offline) so only method is to reflash it with uart. or thru OTA on Offline webapp (but you need to have a local http server.... so not very easy for simple user )

    think i found the issue, it that autoexec.bat is called by an exec in user_main instead of a startScript. :

    Quote:

    // NOTE: this will try to read autoexec.bat,
    // so ALL commands expected in autoexec.bat should have been registered by now...
    CMD_ExecuteCommand(CFG_GetShortStartupCommand(), COMMAND_FLAG_SOURCE_SCRIPT);
    CMD_ExecuteCommand("exec autoexec.bat", COMMAND_FLAG_SOURCE_SCRIPT);


    if you're ok with that i will propose a change in my merge :

    Quote:

    // NOTE: this will try to read autoexec.bat,
    // so ALL commands expected in autoexec.bat should have been registered by now...
    CMD_ExecuteCommand(CFG_GetShortStartupCommand(), COMMAND_FLAG_SOURCE_SCRIPT);
    CMD_ExecuteCommand("startScript autoexec.bat", COMMAND_FLAG_SOURCE_SCRIPT);
  • #104 20457264
    p.kaczmarek2
    Moderator Smart Home
    You are 100% correct. Autoexec.bat is run in place while the "startScript" is something totally different. I am aware about that and I've been thinking about doing that change for last few weeks.

    The only problem is that I am not sure if it's safe to do this. Have you done enough test to know that won't break anything?

    The only difference I can think of is that "startScript" will run the script next frame (next tick) and "exec" runs it in place, but does it matter in that case?

    Maybe we can just do this change and in worst case.... wait for users to report potential problems.

    Still, the basic idea is the same as mine.

    Ok, so.... let's try it and see how it goes for our users?
    Helpful post? Buy me a coffee.
  • #105 20457292
    dheenhasty
    Level 13  
    I have done some test on my sensor it seems to work pretty well.

    i don't think about any side effect by doing that .... since the startup command already accept the startScript pretty well.
  • #106 20458044
    p.kaczmarek2
    Moderator Smart Home
    Ok, by the way, echo will now print variables correctly.

    So:
    
    setChannel 5 123
    echo Channel five is $CH5!
    

    will print:
    
    Channel five is 123
    
    Helpful post? Buy me a coffee.
  • #107 20458414
    dheenhasty
    Level 13  
    according to your last remark on the no script for beginner, i was thinking of bringing all the capabilities of this sensor to the front and HASS (mqtt ).

    so no problem for having a (get/set) for alert with slider for low temp / high temp and low hum / high hum

    A parameter for the measure time / Sleep time could be good too.

    Auto start of the sht driver if pin is set.

    the mechanism could be included in driver easily
    => if alert mode is set => measurePer => publish => pindeepsleep
    => if alert mode is unset => measure => publish => deepsleep rtc

    but there's something that tickle me a little bit. the original firmware wake regularly and on button ..... so i'm not sure they used the deepsleep mode (this will explain why the deepsleep implementation in there SDK was a mess) but instead they just shutdown the wifi and restart it when needed. according to documentation CBU consumption is around ~6ma without the wifi and 100ma with it. we are far from the 6uA in deep sleep :) but could be enough for 2 AA to handle 15 or 20 day.
  • #108 20458916
    p.kaczmarek2
    Moderator Smart Home
    I just realized, is autostart missing for SHT30? I will add this right away.
    [BK7231N CBU] Generic Temperature and Humidity Sensor

    Added after 2 [minutes]:

    I am testing SHT30 with RGBCW led conjunction in Windows simulator, SHT30 values are hardcoded.
    [BK7231N CBU] Generic Temperature and Humidity Sensor
    Dheen you might want to round the number of zeroes after SHT read results
    Helpful post? Buy me a coffee.
  • #109 20458926
    dheenhasty
    Level 13  
    Yes for the rounded, 1 float for temp and we can be integer for humidity will be more readable :)

    You want me to do it ?
  • #110 20458933
    p.kaczmarek2
    Moderator Smart Home
    I added autostart, you can do the rounding.

    I've been also thinking - is there any possible scenario where users wouldn't want autostart for things like SHT30?

    Maybe we could also add a flag saying [DRV] Don't autostart drivers and take whole autostart code into one if(!CFG_HasFlag( OBK_FLAG_xyz )) block
    Helpful post? Buy me a coffee.
  • #112 20459076
    p.kaczmarek2
    Moderator Smart Home
    Yes, but I am not sure why:

    was used, I would personally only change the print function to trim excess zeroes. No other math operations should be needed.
    Helpful post? Buy me a coffee.
  • #113 20459112
    dheenhasty
    Level 13  
    Sort of habit i corrected the data first (who doesn't prevent me to change the visualisation also).

    Anyway, with the flag i detected a small bug on cfg_generic page, the current value doesn't take in account the 32 upper bit.
    it's just visualisation, but don't know how you want to handle that in future (if you want to push that thru template, or do a backup)
  • #114 20459243
    p.kaczmarek2
    Moderator Smart Home
    Hmm you can fix it if you want. There is a 64-bit word support in our sprintf, or is there? We are using not the official sprintf, but the lightweight replacement, so it might be worth checking.
    We already have 64 bit flags in config structure.
    Helpful post? Buy me a coffee.
  • #115 20459717
    dheenhasty
    Level 13  
    I have thinking about the battery and Zero configuration for beginner.

    so i have changed the behaviour :

    https://github.com/openshwprojects/OpenBK7231T_App/pull/697

    Basically, pins to indicate ADC relying on battery and Rel for the vdivider. so we can unable an auto start of the driver.
    And taking a look at majority of tuya sensor seems like it's 2AA one, so i suppose by default 2000mv => 3000mv is good with a vdivider of 2.29 just like the 5 one i got.
  • #116 20459798
    p.kaczmarek2
    Moderator Smart Home
    Ok, the pull request has been merged.

    Now it may be a good time to look into the Tasmota JSON syntax update, so apps like Tasmota Control from Google Play can get sensor values.
    Would you like to help, @dheenhasty ?
    Get that mobile app from Google Play.
    Then look here:
    https://www.elektroda.com/rtvforum/topic3789324.html
    It should be possible to update our json_interface.c to use the same mechanism to check for temperature channel and add a temperature result to json, maybe same for humidity....
    That we can get measurement results in Tasmota Control app.
    Helpful post? Buy me a coffee.
  • #117 20459876
    dheenhasty
    Level 13  
    i have done a typo issue .... on my merge and adding usecase for the no relay Battery.

    https://github.com/openshwprojects/OpenBK7231T_App/pull/698


    Sure let me check for the tasmota :

    Should be good : https://github.com/openshwprojects/OpenBK7231T_App/pull/701

    i added the mecanism to manage other sensor, by adding them in DRV_IsSensor bool in drv_main.

    in json i added a function that can be simplified to handle future sensor.
  • #118 20460286
    p.kaczmarek2
    Moderator Smart Home
    Are you 100% sure that it works with Tasmota Control and ioBroker?

    You seem to have added a JSON block with SHT3X name:
    [BK7231N CBU] Generic Temperature and Humidity Sensor
    The problem is, I don't know if this header is recognized by those apps.

    It is very possible that we will need to fake a DS18B20 header just to make sure that apps are able to get our measurement results. I don't know at the moment.

    Or wait....
    https://tasmota.github.io/docs/JSON-Status-Responses/#sht3x-and-dht11-multiple-sensor-example
    It seems that everything is ok. I've checked with docs and they indeed have that syntax.
    Code: JSON
    Log in, to see the code

    Ok so that will be most likely ok.
    Helpful post? Buy me a coffee.
  • #119 20460306
    dheenhasty
    Level 13  
    Yes you had to indicate the type of sensor, seems like a free string.

    I tested it works with Tasmota Controle and tasmoadmin too ( just got an issue with cmd SensorRetain who is not implemented in your code, i will take a look later)

    Now i'm struggling with the battery and passing correct value :)

    Edit : SensorRetain corrected, so now json is fully compliant with tasmoadmin. But in read only mode :)
  • #120 20464470
    pasdVn3
    Level 2  
    regarding the battery driver:

    After reading the code, I think it was intended to also support the battery relay pin? However actually I cant get it work - I think because IOR_BAT_Relay is used for that, however it can't be configured right now, or am I wrong (also IOR_BAT_ADC, however this seems to work with the normal ADC)? OR am I wrong?
    Other point which might be relevant: I think in Batt_Measure() there is no delay between switching the bat relay pin and reading the ADC. Maybe that this might cause incorrect measurements?

Topic summary

The discussion centers on a generic WiFi temperature and humidity sensor based on the BK7231N CBU module with an SHT30 DIAS7M sensor, purchased from AliExpress. Key technical challenges include implementing effective power-saving strategies, particularly deep sleep modes, to extend battery life in this battery-powered device. Contributors explored the device's pinout, I2C communication (notably software I2C on pins P7 and P8), and firmware modifications using the OpenBeken SDK. Deep sleep implementation on BK7231N shows partial success: the device enters deep sleep and reboots after a set time but may reboot unexpectedly or cause boot loops if deep sleep is triggered too early in the startup script. PowerSave commands reduce power consumption but are insufficient alone; deep sleep with wake-up via GPIO or timer is essential. Battery voltage measurement and reporting were added, with calibration and percentage calculations to monitor battery health and trigger alerts on low voltage. The SHT30 sensor driver was enhanced with commands for calibration, single and periodic measurements, heater control, and alert modes that can wake the device from pindeepsleep. Scripts for automated measurement, battery management, and deep sleep cycles were developed and refined. Issues with script execution timing, startup command behavior, and SDK differences between BK7231N (N) and BK7231T (T) platforms were discussed. GPIO wake-up works on BK7231T but was initially unavailable on BK7231N until recent SDK updates. The MOSFET on the board likely provides reverse polarity protection. Integration with Home Assistant and Tasmota Control via MQTT and JSON was addressed, including sensor data formatting and auto-discovery. Challenges remain with battery-induced firmware corruption below 2V, requiring UART reflashing. Relay-based devices using CB2S boards with BK7231N were also discussed, highlighting pin assignment constraints and I2C communication issues. Overall, the community is actively developing firmware and scripts to enable local control, power optimization, and reliable sensor data reporting for these generic BK7231N-based temperature and humidity sensors.
Summary generated by the language model.
ADVERTISEMENT