From 52e257b66e73480fe1fb322413ef8300487c865a Mon Sep 17 00:00:00 2001 From: lowfatcode Date: Fri, 2 Sep 2022 12:48:26 +0100 Subject: [PATCH 1/2] merge phew --- phew | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phew b/phew index c399309..367cfcc 160000 --- a/phew +++ b/phew @@ -1 +1 @@ -Subproject commit c39930981de5615907976452201f5b41fd716cfc +Subproject commit 367cfccc398086b4958e887ee3e3447e71dfb8c9 From f8616058408be221a04db8b6c138ea2f15d2bae8 Mon Sep 17 00:00:00 2001 From: lowfatcode Date: Fri, 2 Sep 2022 14:56:20 +0100 Subject: [PATCH 2/2] readings now synched to clock time instead of relative to last boot --- enviro/__init__.py | 35 +++++++++++++++++++++++++---------- main.py | 6 +++--- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/enviro/__init__.py b/enviro/__init__.py index 58ccabc..fa9f9b1 100644 --- a/enviro/__init__.py +++ b/enviro/__init__.py @@ -107,6 +107,8 @@ def stop_activity_led(): # intialise the pcf85063a real time clock chip rtc = PCF85063A(i2c) i2c.writeto_mem(0x51, 0x00, b'\x00') # ensure rtc is running (this should be default?) +rtc.enable_timer_interrupt(False) + t = rtc.datetime() RTC().datetime((t[0], t[1], t[2], t[6], t[3], t[4], t[5], 0)) # synch PR2040 rtc too @@ -156,7 +158,7 @@ def wake_reason(): def halt(message): logging.error(message) warn_led(WARN_LED_BLINK) - sleep(config.reading_frequency) + sleep() # returns True if we've used up 90% of the internal filesystem def low_disk_space(): @@ -310,19 +312,32 @@ def startup(): helpers.mkdir_safe("readings") helpers.mkdir_safe("uploads") -def sleep(minutes = -1): +def sleep(): logging.info("> going to sleep") # make sure the rtc flags are cleared before going back to sleep - logging.debug(" - clearing rtc alarm flags") - rtc.clear_timer_flag() + logging.debug(" - clearing and disabling timer and alarm") rtc.clear_alarm_flag() - # if wake time supplied then set rtc timer - if minutes != -1: - logging.info(f" - setting timer to wake in {minutes} minutes") - rtc.enable_timer_interrupt(True) - rtc.set_timer(minutes, PCF85063A.TIMER_TICK_1_OVER_60HZ) + # set alarm to wake us up for next reading + dt = rtc.datetime() + hour, minute = dt[3:5] + + # calculate how many minutes into the day we are + minute = math.floor(minute / config.reading_frequency) * config.reading_frequency + minute += config.reading_frequency + while minute >= 60: + minute -= 60 + hour += 1 + if hour >= 24: + hour -= 24 + ampm = "am" if hour < 12 else "pm" + + logging.info(f" - setting alarm to wake at {hour:02}:{minute:02}{ampm}") + + # sleep until next scheduled reading + rtc.set_alarm(0, minute, hour) + rtc.enable_alarm_interrupt(True) # disable the vsys hold, causing us to turn off logging.info(" - shutting down") @@ -339,7 +354,7 @@ def sleep(minutes = -1): # we'll wait here until the rtc timer triggers and then reset the board logging.debug(" - on usb power (so can't shutdown) halt and reset instead") - while not rtc.read_timer_flag(): + while not rtc.read_alarm_flag(): time.sleep(0.25) if button_pin.value(): # allow button to force reset diff --git a/main.py b/main.py index dacd2bc..31f0858 100644 --- a/main.py +++ b/main.py @@ -19,10 +19,10 @@ # # - the Pimoroni pirate crew -# import enviro firmware +# import enviro firmware, this will trigger provisioning if needed import enviro -# initialise enviro, this will trigger provisioning if needed +# initialise enviro enviro.startup() # now that we know the device is provisioned import the config @@ -65,4 +65,4 @@ enviro.halt("! reading upload failed") # go to sleep until our next scheduled reading -enviro.sleep(config.reading_frequency) \ No newline at end of file +enviro.sleep() \ No newline at end of file