import machine
from machine import Pin, RTC
from machine import sleep
import json
rtc = RTC()
def get_boot_count():
"""
Read or set bootCount in RTC memory
"""
mem = rtc.memory()
try:
res = json.loads(mem)
except Exception:
# bootnumber not saved
res = {}
bootCount = res.get('bootCount', 1)
res['bootCount'] = bootCount + 1
rtc.memory(json.dumps(res))
return bootCount
def print_wakeup_reason():
"""
Print by console the wake up reason
"""
reason = machine.wake_reason()
print(dir(machine))
if reason == machine.EXT0_WAKE:
print("EXT0")
elif reason == machine.EXT1_WAKE:
print("EXT1")
elif reason == machine.TIMER_WAKE:
print("TIMER")
elif reason == machine.TOUCHPAD_WAKE:
print("TOUCHPAD")
elif reason == machine.ULP_WAKE:
print("TOUCHPAD")
else:
print("Wakeup was not caused by deep sleep: %s\n" % reason)
return
print("Going to sleep now")
print_wakeup_reason()
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
print(get_boot_count())
machine.deepsleep(1000)
while True:
pass