forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed as not planned
Milestone
Description
CircuitPython version
8.2.0Code/REPL
# weather station for disabling automatic sprinklers
# https://learn.adafruit.com/weather-wise-wifi-umbrella-stand/code-install-walkthrough
import os
import ssl
import microcontroller
import board
import wifi
import socketpool
import adafruit_requests
if __name__ == '__main__':
def connect_to_network():
# connect to SSID
wifi.radio.enabled = True
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
#pool = socketpool.SocketPool(wifi.radio)
return requests
def disconnect_from_network():
wifi.radio.enabled = False
# function for making http requests with try/except
def get_request(tries, ping):
for i in range(tries):
try:
n = ping
except Exception as error:
print(error)
time.sleep(10)
if i < tries - 1:
continue
raise
break
return n
def check_weather(weather_url):
requests = connect_to_network()
response = get_request(5, requests.get(weather_url))
response_as_json = response.json()
disconnect_from_network()
return response_as_json
def calc_weighted_precip_history(response):
data = response['daily']['precipitation_sum']
#weather_url requests 3 historical days, don't care about future
weights = [ 0.1, 0.1, 0.4, 0.4 ]
return mult_lists(data,weights)
def get_precip_prob(response):
data = response['daily']['precipitation_probability_max']
#don't care about 3 historical days but only today and tomorrow
weights = [ 0.0, 0.0, 0.0, 0.7, 0.3]
return mult_lists(data,weights)
def mult_lists(data,weights):
result = 0.0
for i in range(len(weights)):
result += weights[i] * data[i]
#print(str(round(weights[i],2)) + " x " + str(round(data[i],2)) + " = " + str(round(result,2)))
return result
def get_current_datetime(response):
return response['current_weather']['time']
def interrupt_sprinklers():
lat = os.getenv("MY_LAT")
long = os.getenv("MY_LONG")
# API request to open-meteo
weather_url = "https://api.open-meteo.com/v1/forecast?"
# pass latitude and longitude
weather_url += "latitude=" + lat + "&longitude=" + long + "&daily=precipitation_sum,precipitation_probability_max¤t_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timezone=auto&past_days=3"
# https://api.open-meteo.com/v1/forecast?latitude=47.718&longitude=-116.9516&hourly=rain&daily=precipitation_sum&timezone=auto&start_date=2023-05-01&end_date=2023-07-05
# this one is better
# https://api.open-meteo.com/v1/forecast?latitude=47.718&longitude=-116.9516&daily=precipitation_sum&timezone=auto&past_days=3&forecast_days=3
# even better
# https://api.open-meteo.com/v1/forecast?latitude=47.718&longitude=-116.9516&daily=precipitation_sum,precipitation_probability_max¤t_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timezone=auto&past_days=3
response = check_weather(weather_url)
#response = {'longitude': -116.956, 'timezone_abbreviation': 'PDT', 'latitude': 47.7152, 'generationtime_ms': 0.645995, 'daily_units': {'time': 'iso8601', 'precipitation_sum': 'inch', 'precipitation_probability_max': '%'}, 'current_weather': {'windspeed': 9.7, 'weathercode': 1, 'is_day': 1, 'time': '2023-07-07T15:00', 'winddirection': 85.0, 'temperature': 90.7}, 'elevation': 665.0, 'daily': {'time': ['2023-07-04', '2023-07-05', '2023-07-06', '2023-07-07', '2023-07-08', '2023-07-09', '2023-07-10', '2023-07-11', '2023-07-12', '2023-07-13'], 'precipitation_sum': [0.1, 0.2, 0.3, 0.0, 0.0, 0.0, 0.048, 0.0, 0.0, 0.0], 'precipitation_probability_max': [0, 0, 13, 0, 0, 3, 48, 42, 0, 3]}, 'timezone': 'America/Los_Angeles', 'utc_offset_seconds': -25200}
weighted_precip_history = calc_weighted_precip_history(response)
print("\nPrecipitation history: " + str(round(weighted_precip_history,2)))
precip_prob = get_precip_prob(response)
print("Future probability: " + str(round(precip_prob,2)))
interrupt_sprinklers = False
if(precip_prob > 95 or weighted_precip_history > 0.1):
interrupt_sprinklers = True
else:
interrupt_sprinklers = False
return interrupt_sprinklers
print("Interrupt sprinklers? " + str(interrupt_sprinklers()))Behavior
Code runs successfully then stops and reports "Fault detected by hardware."
Description
It worked yesterday but continues to fault after reinstalling from here https://circuitpython.org/board/adafruit_feather_esp32s2_tft/.
Additional information
none