Skip to content

Commit 1a2b160

Browse files
committed
Merge branch 'rain-per-hour-and-day' into weather-underground
2 parents 4db1962 + 3420d20 commit 1a2b160

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

documentation/boards/enviro-weather.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Enviro Weather is a super slimline all in one board for keeping a (weather) eye
2222
|Wind Speed|`wind_speed`|metres per second|m/s|`0.45`|
2323
|Voltage|`voltage`|volts|V|`4.035`|
2424

25-
The rain today value is adjusted for local time (and/or DST) by modifying the utc_offset value in config.py
25+
The rain today value is adjusted for DST in the UK by setting uk_bst = True in config.py
26+
For static time zone offsets (not taking account of DST), modify the utc_offset value in config.py
27+
The time zone offset value is ignored if uk_bst = True
2628

2729
## On-board devices
2830

enviro/boards/weather.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,56 @@ def wind_direction():
148148

149149
def rainfall(seconds_since_last):
150150
new_rain_entries = []
151-
amount = 0
151+
amount = 0 # rain since last reading
152152
per_hour = 0
153153
today = 0
154+
offset = 0 # UTC offset hours
155+
156+
# configure offset variable for UK BST or timezone offset from config file
157+
# and BST lookup function
158+
if config.uk_bst == True:
159+
if helpers.uk_bst():
160+
offset = 1
161+
elif config.utc_offset != 0:
162+
offset += config.utc_offset
163+
164+
# determine current day number and timestamp
154165
now = helpers.timestamp(helpers.datetime_string())
155-
now_day = helpers.timestamp_day(helpers.datetime_string(), config.utc_offset)
166+
now_day = helpers.timestamp_day(helpers.datetime_string(), offset)
156167
logging.info(f"> current day number is {now_day}")
168+
169+
# process the rain file data
157170
if helpers.file_exists("rain.txt"):
158171
with open("rain.txt", "r") as rainfile:
159172
rain_entries = rainfile.read().split("\n")
160173

161-
# process the rain file data
174+
# populate latest, per second, today and last hour readings from rain log
175+
# file, write new rain log file dropping any yesterday readings
162176
for entry in rain_entries:
163177
if entry:
164178
ts = helpers.timestamp(entry)
165179
tsday = helpers.timestamp_day(entry, config.utc_offset)
166180
logging.info(f"> rain reading day number is {tsday}")
167-
# count how many rain ticks since the last reading
181+
# populate amount with rain since the last reading
168182
if now - ts < seconds_since_last:
169183
amount += RAIN_MM_PER_TICK
170-
# Pick up any untracked yesterday data if current reading is a new day
171-
# Techincally this should be yesterday, but capturing in today is much
172-
# less complex than backdating in the readings file from here
184+
# add any rain ticks from yesterday since the previous reading
185+
# this will misallocate day totals, but will ensure the hourly total
186+
# is correct without introducing complexity backdating yesterday and
187+
# the error will be minimised with frequent readings
188+
# TODO sum yesterday rain and generate a rain_today reading with
189+
# 23:59:59 timestamp of yesterday
173190
if tsday != now_day:
174191
today += RAIN_MM_PER_TICK
175192
# count how many rain ticks in the last hour
176193
if now - ts < 3600:
177194
per_hour += RAIN_MM_PER_TICK
178-
# count how many rain ticks today and delete older entries
195+
# count how many rain ticks today and drop older entries for new file
179196
if tsday == now_day:
180197
today += RAIN_MM_PER_TICK
181198
new_rain_entries.append(entry)
182199

183-
# write out adjusted rain log
200+
# write out new adjusted rain log
184201
with open("rain.txt", "w") as newrainfile:
185202
newrainfile.write("\n".join(new_rain_entries))
186203

enviro/config_defaults.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from phew import logging
33

44
DEFAULT_USB_POWER_TEMPERATURE_OFFSET = 4.5
5+
DEFAULT_UTC_OFFSET = 0
6+
DEFAULT_UK_BST = True
57

68

79
def add_missing_config_settings():
@@ -42,11 +44,17 @@ def add_missing_config_settings():
4244
warn_missing_config_setting("height_above_sea_level")
4345
config.height_above_sea_level = 0
4446

47+
try:
48+
config.uk_bst
49+
except AttributeError:
50+
warn_missing_config_setting("uk_bst")
51+
config.uk_bst = DEFAULT_UK_BST
52+
4553
try:
4654
config.utc_offset
4755
except AttributeError:
4856
warn_missing_config_setting("utc_offset")
49-
config.utc_offset = 0
57+
config.utc_offset = DEFAULT_UTC_OFFSET
5058

5159
def warn_missing_config_setting(setting):
5260
logging.warn(f"> config setting '{setting}' missing, please add it to config.py")

enviro/config_template.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
wifi_ssid = None
1313
wifi_password = None
1414

15-
# For local time corrections to daily rain logging (include DST)
16-
utc_offset = 1
15+
# Adjust daily rain day for UK BST
16+
uk_bst = True
17+
18+
# For local time corrections to daily rain logging other than BST
19+
# Ignored if uk_bst = True
20+
utc_offset = 0
1721

1822
# how often to wake up and take a reading (in minutes)
1923
reading_frequency = 15

enviro/helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from enviro.constants import *
22
import machine, math, os, time, utime
3+
from phew import logging
34

45
# miscellany
56
# ===========================================================================
@@ -24,6 +25,32 @@ def timestamp(dt):
2425
second = int(dt[17:19])
2526
return time.mktime((year, month, day, hour, minute, second, 0, 0))
2627

28+
def uk_bst():
29+
# Return True if in UK BST - manually update bst_timestamps {} as needed
30+
dt = datetime_string()
31+
year = int(dt[0:4])
32+
ts = timestamp(dt)
33+
bst = False
34+
35+
bst_timestamps = {
36+
2023: {"start": 1679792400, "end": 1698541200},
37+
2024: {"start": 1711846800, "end": 1729990800},
38+
2025: {"start": 1743296400, "end": 1761440400},
39+
2026: {"start": 1774746000, "end": 1792890000},
40+
2027: {"start": 1806195600, "end": 1824944400},
41+
2028: {"start": 1837645200, "end": 1856394000},
42+
2029: {"start": 1869094800, "end": 1887843600},
43+
2030: {"start": 1901149200, "end": 1919293200}
44+
}
45+
46+
if year in bst_timestamps:
47+
if bst_timestamps[year]["start"] < ts and bst_timestamps[year]["end"] > ts:
48+
bst = True
49+
else:
50+
logging.warn(f"> Current year is not in BST lookup dictionary: {year}")
51+
return bst
52+
53+
2754
# Return the day number of your timestamp string accommodating UTC offsets
2855
def timestamp_day(dt, offset_hours):
2956
# Bounce via timestamp to properly calculate hours change

0 commit comments

Comments
 (0)