import machine
import utime
import esp32
uS_TO_S_FACTOR = 1000000
TIME_TO_SLEEP = 30
boot_count = machine.RTC().memory[0] # Store boot count in RTC memory
rtc_io_pin = machine.Pin(32, machine.Pin.IN, machine.Pin.PULL_UP) # Configure RTC_IO pin as input with pull-up resistor
def print_wakeup_reason():
global wakeup_reason
wakeup_reason = machine.reset_cause()
if wakeup_reason == machine.WAKE_REASON_EXT0:
print("Wakeup caused by external signal using RTC_IO")
elif wakeup_reason == machine.WAKE_REASON_EXT1:
print("Wakeup caused by external signal using RTC_CNTL")
elif wakeup_reason == machine.WAKE_REASON_TIMER:
print("Wakeup caused by timer")
elif wakeup_reason == machine.WAKE_REASON_TOUCHPAD:
print("Wakeup caused by touchpad")
elif wakeup_reason == machine.WAKE_REASON_ULP:
print("Wakeup caused by ULP program")
else:
print(f"Wakeup was not caused by deep sleep: {wakeup_reason}")
def wake_up_handler(pin):
global wakeup_reason
wakeup_reason = machine.WAKE_REASON_EXT0
def setup():
print("Initializing serial communication...")
utime.sleep(1) # Wait for 1 second
global boot_count
boot_count += 1
print(f"Boot number: {boot_count}")
print_wakeup_reason()
print(f"Setup ESP32 to sleep for every {TIME_TO_SLEEP} seconds")
machine.deepsleep(TIME_TO_SLEEP * uS_TO_S_FACTOR)
def loop():
pass
setup()