Longwood Weather Observatory
What Is LWO?
Longwood Weather Observatory
Longwood Weather Observatory (LWO) is a hand-built and coded Automatic Weather Station based on a Raspberry Pi 3A. It was built in early 2020 and lives in Rainham, Kent. It records air temperature, humidity, pressure, wind speed and rainfall. This information is then uploaded to the Wia internet-of-things service, and the MetOffice WOW service.
The station is currently out of commission due to the free-to-use services it relied on to connect to the internet being terminated and me no longer being based in Kent to maintain it.
Code For Collecting Data
from gpiozero import Button
import time
import math
import statistics
import bme280_sensor
from time import gmtime, strftime
wind_count = 0
radius_cm = 9.0
wind_interval = 5
wind_gust_max = 0
store_speeds = []
gust_speed_list = []
interval = 15
BUCKET_SIZE = 0.2794
rain_count = 0
rainfall_24 = 0
def bucket_tipped():
global rain_count
rain_count = rain_count + 1
def reset_rainfall():
global rain_count
rain_count = 0
def spin():
global wind_count
wind_count = wind_count + 1
def calculate_speed(time_sec):
global wind_count
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_cm = circumference_cm * rotations
speed = dist_cm / time_sec / 100 * 1.4
return speed
def reset_wind():
global wind_count
wind_count = 0
wind_speed_sensor = Button(5)
wind_speed_sensor.when_pressed = spin
rain_sensor = Button(6)
rain_sensor.when_pressed = bucket_tipped
while True:
start_time = time.time()
check_midnight = strftime(“%H:%M:%S”, gmtime())
if check_midnight == “00:00:00”:
rainfall_24 = 0
while time.time() – start_time <= wind_interval:
reset_wind()
time.sleep(wind_interval)
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_gust_current = max(store_speeds)
if wind_gust_current > wind_gust_max:
wind_gust_max = wind_gust_current
wind_speed = statistics.mean(store_speeds)
rainfall = rain_count * BUCKET_SIZE
rainfall_24 = rainfall_24 + rainfall
reset_rainfall()
humidity, pressure, ambient_temp = bme280_sensor.read_all()
print(“current wind speed: ” + str(wind_speed) + ” m/s”)
current_wind_speed_write = open(“current_wind_speed.txt.”, “w”)
current_wind_speed_write.write(str(wind_speed))
current_wind_speed_write.close()
print(“max wind gust: ” + str(wind_gust_max) + ” m/s”)
print(“accumulated rain in last 24 hr: ” + str(rainfall_24) + ” mm”)
current_rainfall_24_write = open(“current_rainfall_24.txt.”, “w”)
current_rainfall_24_write.write(str(rainfall_24))
current_rainfall_24_write.close()
print(“rainfall in last 30 seconds: ” +str(rainfall) + “mm”)
current_rainfall_write = open(“current_rainfall.txt.”, “w”)
current_rainfall_write.write(str(rainfall))
current_rainfall_write.close()
print(“humidity: ” + str(humidity) + “%”)
current_humidity_write = open(“current_humidity.txt.”, “w”)
current_humidity_write.write(str(humidity))
current_humidity_write.close()
print(“pressure: ” + str(pressure) + ” units”)
current_pressure_write = open(“current_pressure.txt.”, “w”)
current_pressure_write.write(str(pressure))
current_pressure_write.close()
print(“temperature: ” + str(ambient_temp) + “C”)
current_temp_write = open(“current_temp.txt.”, “w”)
current_temp_write.write(str(ambient_temp))
current_temp_write.close()
print(” “)
store_speeds.clear()
wind_gust_max = 0
time.sleep(30)
Code For Uploading Data
from wia import Wia
import time
import urllib
import urllib2
already_done = False
already_done_rain = False
wia = Wia()
wia.access_token = “d_sk_########################
def Timeformatting(theTime):
Timenow = time.strftime(“%Y-%m-%d %H:%M:%S”,theTime)
Timeformat = Timenow.replace(‘:’,’%3A’).replace(‘ ‘,’+’)
return Timeformat, Timenow
#WOW Details
SiteID = ####################################; AWSKey = ######; softwaretype = “my_software”
while True:
temp_file = open(“current_temp.txt.”, “r”)
ambient_temp = round(float(temp_file.read()), 1)
wia.Event.publish(name=”temperature”, data=ambient_temp)
pressure_file = open(“current_pressure.txt.”, “r”)
pressure = round(float(pressure_file.read()), 1)
wia.Event.publish(name=”pressure”, data=pressure)
humidity_file = open(“current_humidity.txt.”, “r”)
humidity = round(float(humidity_file.read()), 1)
wia.Event.publish(name =”humidity”, data=humidity)
wind_file = open(“current_wind_speed.txt.”, “r”)
wind_speed = round(float(wind_file.read()), 1)
wia.Event.publish(name=”windspeed”, data=wind_speed)
rainfall_file = open(“current_rainfall.txt.”, “r”)
rainfall = round(float(rainfall_file.read()), 1)
wia.Event.publish(name=”rainfall”, data=rainfall)
accum_rain_file = open(“current_rainfall_24.txt.”, “r”)
accum_rain = round(float(accum_rain_file.read()), 1)
wia.Event.publish(name=”accum_rain”, data=accum_rain)
temp_f = round((ambient_temp*1.8)+32, 1)
wia.Event.publish(name=”farenheit”, data=temp_f)
pressure_Hg = round(pressure / 33.863886666667, 1)
wia.Event.publish(name=”pressure Hg”, data=pressure_Hg)
wind_mph = round(wind_speed / 0.44704, 1)
wia.Event.publish(name=”wind mph”, data=wind_mph)
if ambient_temp < 0 and already_done == False:
wia.Event.publish(name=”low_temp”, data=””)
already_done = True
elif ambient_temp > 0 and already_done == True:
already_done = False
if rainfall < 0 and already_done_rain == False:
wia.Event.publish(name=”rain_start”, data=””)
already_done_rain = True
elif rainfall > 0 and already_done == True:
already_done_rain = False
daily_rain_in = round(accum_rain*0.254)
#beaufort conversion
if wind_speed < 1:
wia.Event.publish(name=”beaufort”, data=”0 – Calm”)
elif wind_speed >= 1 and wind_speed <= 2:
wia.Event.publish(name=”beaufort”, data=”1 – Light air”)
elif wind_speed >= 2 and wind_speed <= 3:
wia.Event.publish(name=”beaufort”, data=”2 – Light breeze”)
elif wind_speed >= 4 and wind_speed <= 5:
wia.Event.publish(name=”beaufort”, data =”3 – Gentle breeze”)
elif wind_speed >= 6 and wind_speed <= 8:
wia.Event.publish(name=”beaufort”, data =”4 – Moderate breeze”)
wia.Event.publish(name=”beaufort”, data =”4 – Moderate breeze”)
elif wind_speed >= 9 and wind_speed <= 11:
wia.Event.publish(name=”beaufort”, data=”5 – Fresh breeze”)
elif wind_speed >= 11 and wind_speed <= 14:
wia.Event.publish(name=”beaufort”, data=”6 – Strong breeze”)
elif wind_speed >= 15 and wind_speed <= 17:
wia.Event.publish(name=”beaufort”, data=”7 – Near gale”)
elif wind_speed >= 18 and wind_speed <= 21:
wia.Event.publish(name=”beaufort”, data=”8 – Gale”)
elif wind_speed >= 22 and wind_speed <= 24:
wia.Event.publish(name=”beaufort”, data=”9 – Strong gale”)
elif wind_speed >= 25 and wind_speed <= 28:
wia.Event.publish(name=”beaufort”, data=”10 – Storm”)
elif wind_speed >= 29 and wind_speed <= 32:
wia.Event.publish(name=”beaufort”, data=”11 – Violent storm”)
elif wind_speed > 32:
wia.Event.publish(name=”beaufort”, data=”12 – Hurricane”)
#feels like temp
if temp_f <= 50 and wind_mph > 3:
feels_like_f = 35.74 + (0.6215 * temp_f) – (35.75* (wind_mph**0.16)) + (0.4275 * T * (wind_mph ** 0.16))
feels_like_c = round((feels_like_f – 32) / 1.8, 1)
wia.Event.publish(name=”feels like”, data=feels_like_c)
elif ambient_temp >= 26.7:
feels_like_f = -42.379 +(2.04901523 * temp_f) + (10.13333127 * humidity) – (0.22475541 * temp_f * humidity) – (0.0068783 * temp_f * temp_f) – (0.05481717 * humidity * humidity) + (0.00122874 * temp_f * temp_f * humidity) + (0.00085282 * temp_f * humidity * hum$
feels_like_c = round((feels_like_f – 32)/1.8, 1)
wia.Event.publish(name=”feels like”, data=feels_like_c)
else:
wia.Event.publish(name=”feels like”, data=ambient_temp)
[Timeformat, Timenow] = Timeformatting(time.gmtime())
url = “http://wow.metoffice.gov.uk/automaticreading?siteid=%s&siteAuthenticationKey=%s&dateutc=%s&tempf=%s&baromin=%s&humidity=%s&windspeedmph=%s&dailyrainin=%s&softwaretype=%s” % (SiteID,AWSKey,Timeformat, temp_f, pressure_Hg, humidity, wind_mph, daiy_rain_in, softwaretype)
request = urllib2.Request(url)
response = urllib2.urlopen(request).getcode()
if float(response) == 200:
print “200 — WOW upload OK”
else:
print “WOW upload error”
time.sleep(300)