Retry on error, solve #10

This commit is contained in:
Gabriel Augendre 2023-04-23 11:40:06 +02:00
parent 600257d19a
commit 614ee88c83
3 changed files with 29 additions and 6 deletions

View file

@ -9,7 +9,7 @@ from badger2040 import (
UPDATE_FAST, UPDATE_FAST,
) )
from badger_with_clock import Badger2040 from badger_with_clock import Badger2040
from badger_os import get_battery_level from badger_os import get_battery_level, warning
import secrets import secrets
from dst import fix_dst from dst import fix_dst
@ -201,10 +201,7 @@ def main():
while True: while True:
display.rtc.clear_timer_flag() display.rtc.clear_timer_flag()
fetch_and_display() fetch_and_display()
display.rtc.set_timer( display.set_timer_minutes_with_jitter(secrets.REFRESH_INTERVAL_MINUTES)
secrets.REFRESH_INTERVAL_MINUTES, ttp=PCF85063A.TIMER_TICK_1_OVER_60HZ
)
display.rtc.enable_timer_interrupt(True)
print("Halting") print("Halting")
display.halt() display.halt()
@ -249,4 +246,11 @@ def get_time():
return fix_dst(*display.rtc.datetime()) return fix_dst(*display.rtc.datetime())
while True:
try:
main() main()
except Exception as e:
print(e)
warning(display, str(e))
display.set_timer_minutes_with_jitter(secrets.ERROR_REFRESH_INTERVAL_MINUTES)
display.halt()

View file

@ -3,6 +3,7 @@ import machine
from pimoroni_i2c import PimoroniI2C from pimoroni_i2c import PimoroniI2C
from pcf85063a import PCF85063A from pcf85063a import PCF85063A
import ntptime import ntptime
import random
import badger2040 import badger2040
@ -24,3 +25,20 @@ class Badger2040(badger2040.Badger2040):
ntptime.settime() ntptime.settime()
now = time.localtime() now = time.localtime()
self.rtc.datetime(now[:7]) self.rtc.datetime(now[:7])
def set_timer_minutes(self, minutes: int):
"""Wake the board in X minutes. Doesn't halt the board."""
self.rtc.clear_timer_flag()
self.rtc.set_timer(minutes, ttp=PCF85063A.TIMER_TICK_1_OVER_60HZ)
self.rtc.enable_timer_interrupt(True)
def set_timer_minutes_with_jitter(
self, minutes: int, jitter_percentage: float = 0.2
):
"""
Wake the board in X minutes with some jitter. Doesn't halt the board.
jitter_percentage should be a float between 0.0 and 1.0.
"""
delta = int(minutes * abs(jitter_percentage))
minutes += random.randint(-delta, delta)
self.set_timer_minutes(minutes)

View file

@ -14,3 +14,4 @@ HA_PLANT_CONDUCTIVITY_SENSOR = "sensor.plant_name_conductivity"
HA_PLANT_ILLUMINANCE_SENSOR = "sensor.plant_name_illuminance" HA_PLANT_ILLUMINANCE_SENSOR = "sensor.plant_name_illuminance"
REFRESH_INTERVAL_MINUTES = 60 # Max 255 REFRESH_INTERVAL_MINUTES = 60 # Max 255
ERROR_REFRESH_INTERVAL_MINUTES = 30