Skip to content

Commit ae510db

Browse files
author
ladyada
committed
init sketch
1 parent 2952284 commit ae510db

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

CircuitPython_WeatherCloud/code.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import time
2+
import board
3+
import busio
4+
import random
5+
from digitalio import DigitalInOut
6+
import neopixel
7+
from adafruit_esp32spi import adafruit_esp32spi
8+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
9+
import adafruit_requests as requests
10+
import adafruit_fancyled.adafruit_fancyled as fancy
11+
12+
print("ESP32 Open Weather API demo")
13+
14+
# Use cityname, country code where countrycode is ISO3166 format.
15+
# E.g. "New York, US" or "London, GB"
16+
LOCATION = "Manhattan, US"
17+
18+
# Get wifi details and more from a secrets.py file
19+
try:
20+
from secrets import secrets
21+
except ImportError:
22+
print("WiFi secrets are kept in secrets.py, please add them there!")
23+
raise
24+
25+
# Set up where we'll be fetching data from
26+
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
27+
DATA_SOURCE += "&appid="+secrets['openweather_token']
28+
29+
# If you are using a board with pre-defined ESP32 Pins:
30+
esp32_cs = DigitalInOut(board.ESP_CS)
31+
esp32_ready = DigitalInOut(board.ESP_BUSY)
32+
esp32_reset = DigitalInOut(board.ESP_RESET)
33+
34+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
35+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
36+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
37+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
38+
pixels = neopixel.NeoPixel(board.D2, 16, brightness=1.0, auto_write=False)
39+
pixels.fill(0x050505)
40+
pixels.show()
41+
42+
# clouds palette
43+
cloudy_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
44+
fancy.CRGB(0.5, 0.5, 0.5), # gray
45+
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
46+
# sunny palette
47+
sunny_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
48+
fancy.CRGB(1.0, 1.0, 0.0),# Yellow
49+
fancy.CRGB(1.0, 0.5, 0.0),] # Orange
50+
# thunderstorm palette
51+
thunder_palette = [fancy.CRGB(0.0, 0.0, 1.0), # blue
52+
fancy.CRGB(0.5, 0.5, 0.5), # gray
53+
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
54+
last_thunder_bolt = None
55+
56+
palette = None # current palette
57+
pal_offset = 0 # Positional offset into color palette to get it to 'spin'
58+
levels = (0.25, 0.3, 0.15) # Color balance / brightness for gamma function
59+
raining = False
60+
thundering = False
61+
62+
weather_refresh = None
63+
weather_type = None
64+
while True:
65+
# only query the weather every 10 minutes (and on first run)
66+
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
67+
try:
68+
response = wifi.get(DATA_SOURCE).json()
69+
print("Response is", response)
70+
weather_type = response['weather'][0]['main']
71+
weather_type = 'Thunderstorm' # manually adjust weather type for testing!
72+
73+
print(weather_type) # See https://openweathermap.org/weather-conditions
74+
# default to no rain or thunder
75+
raining = thundering = False
76+
if weather_type == 'Clouds':
77+
palette = cloudy_palette
78+
if weather_type == 'Rain':
79+
palette = cloudy_palette
80+
raining = True
81+
if weather_type == 'Sunny':
82+
palette = sunny_palette
83+
if weather_type == 'Thunderstorm':
84+
palette = thunder_palette
85+
raining = thundering = True
86+
# pick next thunderbolt time now
87+
next_bolt_time = time.monotonic() + random.randint(1, 5)
88+
weather_refresh = time.monotonic()
89+
except RuntimeError as e:
90+
print("Some error occured, retrying! -", e)
91+
time.sleep(5)
92+
continue
93+
94+
if palette:
95+
for i in range(len(pixels)):
96+
color = fancy.palette_lookup(palette, pal_offset + i / len(pixels))
97+
color = fancy.gamma_adjust(color, brightness=levels)
98+
pixels[i] = color.pack()
99+
pixels.show()
100+
pal_offset += 0.01 # Bigger number = faster spin
101+
102+
if raining:
103+
# don't have a droplet every time
104+
if (random.randint(0, 25) == 0): # 1 out of 25 times
105+
pixels[random.randint(0, len(pixels)-1)] = 0xFFFFFF # make a random pixel white
106+
pixels.show()
107+
108+
# if its time for a thunderbolt
109+
if thundering and (time.monotonic() > next_bolt_time):
110+
print("Ka Bam!")
111+
# fill pixels white, delay, a few times
112+
for i in range(random.randint(1, 3)): # up to 3 times...
113+
pixels.fill(0xFFFFFF) # bright white!
114+
pixels.show()
115+
time.sleep(random.uniform(0.01, 0.2)) # pause
116+
pixels.fill(0x0F0F0F) # gray
117+
pixels.show()
118+
time.sleep(random.uniform(0.01, 0.3)) # pause
119+
# pick next thunderbolt time now
120+
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s
121+

CircuitPython_WeatherCloud/secrets.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is where you keep secret settings, passwords, and tokens!
2+
# If you put them in the code you risk committing that info or sharing it
3+
4+
secrets = {
5+
'ssid' : 'my_ssid',
6+
'password' : 'my_pass',
7+
'timezone' : "America/New_York", # http://worldtimeapi.org/timezones
8+
'openweather_token' : 'putYourOpenWeatherTokenHere',
9+
}

0 commit comments

Comments
 (0)