From 4728d2736f28ec4492cbbbeafaeb5f410683d452 Mon Sep 17 00:00:00 2001 From: lowfatcode Date: Mon, 5 Sep 2022 08:16:51 +0100 Subject: [PATCH] fixed error when rain.txt not present --- enviro/boards/weather.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/enviro/boards/weather.py b/enviro/boards/weather.py index 4228222..bddb4c9 100644 --- a/enviro/boards/weather.py +++ b/enviro/boards/weather.py @@ -4,7 +4,7 @@ from machine import Pin, PWM from pimoroni import Analog from enviro import i2c, hold_vsys_en_pin -import enviro.helpers +import enviro.helpers as helpers from phew import logging RAIN_MM_PER_TICK = 0.2794 @@ -12,23 +12,25 @@ bme280 = BreakoutBME280(i2c, 0x77) ltr559 = BreakoutLTR559(i2c) -piezo_pwm = PWM(Pin(28)) - wind_direction_pin = Analog(26) wind_speed_pin = Pin(9, Pin.IN, Pin.PULL_UP) def startup(): import wakeup + # check if rain sensor triggered wake rain_sensor_trigger = wakeup.get_gpio_state() & (1 << 10) + if rain_sensor_trigger: # read the current rain entries - rain_entries = None - with open("rain.txt", "r") as rainfile: - rain_entries = rainfile.read().split("\n") + rain_entries = [] + if helpers.file_exists("rain.txt"): + with open("rain.txt", "r") as rainfile: + rain_entries = rainfile.read().split("\n") # add new entry - rain_entries.append(enviro.helpers.datetime_string()) + logging.info("> add new rain trigger at {helpers.datetime_string()}") + rain_entries.append(helpers.datetime_string()) # limit number of entries to 190 - each entry is 21 bytes including # newline so this keeps the total rain.txt filesize just under one @@ -119,10 +121,10 @@ def timestamp(dt): return time.mktime((year, month, day, hour, minute, second, 0, 0)) def rainfall(): - if not enviro.helpers.file_exists("rain.txt"): + if not helpers.file_exists("rain.txt"): return 0 - now = timestamp(enviro.helpers.datetime_string()) + now = timestamp(helpers.datetime_string()) with open("rain.txt", "r") as rainfile: rain_entries = rainfile.read().split("\n")