Skip to content

Commit cbb0b20

Browse files
authored
Merge pull request #2477 from adafruit/umbrella_stand
Adding code for IoT umbrella stand
2 parents 7df93e4 + 1876c00 commit cbb0b20

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Weather_Wise_Umbrella_Stand/code.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import os
5+
import ssl
6+
import time
7+
import microcontroller
8+
import board
9+
import wifi
10+
import socketpool
11+
import adafruit_requests
12+
import neopixel
13+
from adafruit_ticks import ticks_ms, ticks_add, ticks_diff
14+
from adafruit_io.adafruit_io import IO_HTTP
15+
16+
# latitude
17+
lat = 38.58
18+
# longitude
19+
long = -121.49
20+
# hours in 24 hour time that the pixels should be off
21+
hours_off = (0, 6)
22+
# color of the pixels when rain is expected
23+
PIXELS_COLOR = (0, 0, 255)
24+
25+
# neopixel setup
26+
NUMPIXELS = 30 # number of neopixels
27+
BRIGHTNESS = 0.5 # A number between 0.0 and 1.0, where 0.0 is off, and 1.0 is max.
28+
PIN = board.GP0
29+
30+
pixels = neopixel.NeoPixel(PIN, NUMPIXELS, brightness=BRIGHTNESS, auto_write=False)
31+
32+
# turn on NeoPixels on boot to check wiring
33+
pixels.fill(PIXELS_COLOR)
34+
pixels.show()
35+
36+
# API request to open-meteo
37+
weather_url = "https://api.open-meteo.com/v1/forecast?"
38+
# pass latitude and longitude
39+
# will return sunrise and sunset times
40+
weather_url += "latitude=%d&longitude=%d&timezone=auto&hourly=rain&forecast_days=1" % (lat, long)
41+
42+
# connect to SSID
43+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
44+
45+
pool = socketpool.SocketPool(wifi.radio)
46+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
47+
48+
pool = socketpool.SocketPool(wifi.radio)
49+
50+
# adafruit IO info
51+
aio_username = os.getenv('aio_username')
52+
aio_key = os.getenv('aio_key')
53+
location = "America/Los Angeles"
54+
55+
# io HTTP for getting the time from the internet
56+
io = IO_HTTP(aio_username, aio_key, requests)
57+
58+
def reset_on_error(delay, error):
59+
print("Error:\n", str(error))
60+
print("Resetting microcontroller in %d seconds" % delay)
61+
time.sleep(delay)
62+
microcontroller.reset()
63+
64+
# function for making http requests with try/except
65+
def get_request(tries, ping):
66+
for i in range(tries):
67+
try:
68+
n = ping
69+
except Exception as error:
70+
print(error)
71+
time.sleep(10)
72+
if i < tries - 1:
73+
continue
74+
raise
75+
break
76+
return n
77+
78+
# pylint: disable=broad-except
79+
# function to make a request to open-meteo & time from IO
80+
def rain_check():
81+
# gets current time
82+
now = get_request(5, io.receive_time())
83+
h = now.tm_hour
84+
time.sleep(1)
85+
# make the API request
86+
weather_call = get_request(5, requests.get(weather_url))
87+
# packs the response into a JSON
88+
response_as_json = weather_call.json()
89+
# gets rain forecast
90+
_chance = response_as_json['hourly']['rain']
91+
return h, _chance
92+
93+
# ticks time tracker
94+
clock = ticks_ms()
95+
96+
# tracker for initial start-up state
97+
first_run = True
98+
99+
# 15 minutes in milliseconds
100+
time_check = 900000
101+
102+
while True:
103+
try:
104+
# every 15 minutes...
105+
if first_run or ticks_diff(ticks_ms(), clock) > time_check:
106+
print("pinging Open-Meteo")
107+
hour, rain_chance = rain_check()
108+
print("Rain expected: %.2f mm" % rain_chance[hour])
109+
if hour in hours_off:
110+
print("sleeping, please don't tell me it's raining")
111+
color = (0, 0, 0)
112+
else:
113+
# if rain is expected. turn pixels blue
114+
if rain_chance[hour] > 0:
115+
print("tut tut, looks like rain")
116+
color = PIXELS_COLOR
117+
# otherwise turn pixels off
118+
else:
119+
print("no rain expected")
120+
color = (0, 0, 0)
121+
if first_run:
122+
first_run = False
123+
else:
124+
pixels.fill(color)
125+
pixels.show()
126+
# reset clock
127+
clock = ticks_add(clock, time_check)
128+
except Exception as e:
129+
reset_on_error(10, e)

0 commit comments

Comments
 (0)