logo elektroda
logo elektroda
X
logo elektroda

BK7231 deep sleep - how to enable wake up both on timer and on GPIO interrupt in OpenBeken?

p.kaczmarek2 3657 3
ADVERTISEMENT
  • BEKEN BK7231TQN32 chip viewed from above.
    OpenBeken features a deep sleep system which allows you to basically turn off the BK7231 until a certain amount of time passes (DeepSleep) or until a GPIO event occurs (like button press - PinDeepSleep). The following feature is necessary for the battery powered devices, otherwise the batteries would drain in a day or two. Here I will show how you can configure OBK to be waken up by both GPIO event and timer.
    Please note that there are also TuyaMCU-based battery powered devices. This article does not cover them, as they don't use deep sleep on the BK7231. For more info about TuyaMCU battery devices, read Energy-saving (?) Battery-operated door / window sensor for WiFi DS06

    Some basic topics related to the BK7231 deep sleeps were already covered on forum. Please see:
    Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken
    [OpenBeken] Battery measurement driver based ADC with voltage divider
    You can also check out autoexec.bat examples:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md

    So, there are two sleep commands:
    
    DeepSleep 123
    

    The command above will sleep BK7231 for 123 seconds. After 123 seconds, it will reboot from scratch.
    
    PinDeepSleep
    

    The command above will sleep BK7231 until a GPIO event occurs. So, you need to have configured some inputs for that, like a Button. Set a Btn role for one of GPIOs first and then when you press the button (short it to ground) the BK will wake up from scratch.

    The PinDeepSleep command can also take an emergency timer:
    
    PinDeepSleep 123
    

    The following will wake up by either GPIO event or when the 123 second timer passes.

    The following can be used for devices like smoke detectors so they both wake up on GPIO event and periodically just to report the battery status.
    It can be also used to make a configuration mode for temperature sensor.

    In order to check the reboot reason (whether it was a timer wake up or GPIO interrupt wakeup), you can use our variable:
    
    $rebootReason
    

    Following variable can be used in OBK scripts. For example, you can report the reboot reason via MQTT in the following way:
    
    // For best results, you need to
    // have at least one pin role
    // set to Button (with a button 
    // between pin and the ground)
    
    // now wait for MQTT
    waitFor MQTTState 1
    // publish int
    publishInt RebootReasonIndex $rebootReason
    // publish str
    if $rebootReason==0 then publish RebootReason Power
    if $rebootReason==1 then publish RebootReason Reboot
    if $rebootReason==2 then publish RebootReason Watchdog
    if $rebootReason==3 then publish RebootReason PinInterrupt
    if $rebootReason==4 then publish RebootReason SleepTimer
    
    // give user time to play
    delay_s 60
    // sleep either until pin wakeup or until 60 second timer
    PinDeepSleep 60
    

    After a brief testing, you can see that the published value changes depending on the way you've booted your device:
    Screenshot of an MQTT messages window showing the state information of an OBK device.
    Screenshot of MQTT results showing reboot details.
    Screenshot showing MQTT data including connection status and the reason for the reboot of an OpenBK7231 device.

    The following can be extended to make a configuration mode for temperature sensor. Let's say that temperature sensor wakes up every 10 minutes to do a measurement and you also want to be able to wake it up by a button so it stays online so you can reconfigure it. Here is a sample script that offers the said feature:
    
    
    // detect reboot by a button
    if $rebootReason==3 then goto config_mode
    // do emergency sleep if no MQTT in 30 seconds
    // addRepeatingEvent	[IntervalSeconds][RepeatsOr-1][CommandToRun]
    addRepeatingEvent 30 1 PinDeepSleep 60
    // now wait for MQTT
    waitFor MQTTState 1
    // publish data
    publish MySensorMode Normal
    publishFloat MySomeTemperature 12.34
    publishFloat MySomeHumidity 56.78
    // wait 3 seconds more
    delay_s 3
    // sleep for 60 seconds or until next button press
    PinDeepSleep 60
    
    config_mode:
    
    // now wait for MQTT
    waitFor MQTTState 1
    // publish data
    publish MySensorMode ConfigMode
    // do nothing, do not sleep
    

    The script shown above determines the reboot reason and then splits the execution depending on that.
    As you can see, the script works well:
    Screenshot of the MQTT interface with OpenBeken sensor information.
    Screenshot of the OpenBeken application showing the status of the BK7231N controller with various configuration options.
    To learn more about our scripts, visit our autoexec samples page:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/autoexecExamples.md
    And commands doc:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md
    You can also see our generic docs page:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/README.md
    And here is the most basic autoexec.bat creation guide:



    Remember to also just check out our Elektroda YT channel:
    https://www.youtube.com/@elektrodacom
    And don't forget about the devices list:
    https://openbekeniot.github.io/webapp/devicesList.html

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11920 posts with rating 9984, helped 572 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21100132
    JardaLCZ
    Level 5  
    Thanks for the article. Is there a defined maximum value of PinDeepSleep (and DeepSleep) ? I want to use a door sensor for my application but I need to get time to time a "live signal" that batteries are in a good condition and the connection to WiFi and MQTT is possible. I tried 1 day (86 400 secs) , it works but 2 days or more are not reliable. The time diference (time shift every day) is not a problem, I think that the counter in Deep is not precise. Thank you.
  • ADVERTISEMENT
  • #3 21100781
    p.kaczmarek2
    Moderator Smart Home
    This is a very good question, but we weren't exactly testing for that yet. The limitation must be within BK SDK. This is the function we're using:
    Code: C / C++
    Log in, to see the code

    And it calls:
    Code: C / C++
    Log in, to see the code

    They seem to be using a 32.786 oscillator for the wake up and they bound the time to:
    Code: C / C++
    Log in, to see the code

    which, in decimal, is 131071. 131071 seconds is about 36.4086111 hours, so 1.5 days.
    Helpful post? Buy me a coffee.
  • #4 21260233
    julioviegas
    Level 1  
    Hi all,

    I'm running OpenBeken v1.17.745 on a wifi water pump WD-01ADE chipset BK7231N. Everything is working fantastically great, except for the DeepSleep feature. It sends the device into deep sleep after I issue "DeepSleep 10" on the console, but the device fails to recover from it i.e. reboot after 10 seconds. I'm only able to recover from DeepSleep after unplugging the power from the device for some time (around a minute or so?) and then plug it back for a regular boot, which is not the best in terms of automation, of course.

    Is it due to something missing hardware-wise, such as a jumper between gpio and power/ground?

    Thanks in advance!
ADVERTISEMENT