Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed error when rain.txt not present #66

Merged
merged 1 commit into from
Sep 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions enviro/boards/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@
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

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
Expand Down Expand Up @@ -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")

Expand Down