
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:



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:


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.