Skip to content

Commit 97f59f4

Browse files
authored
Merge branch 'master' into activity_generator
2 parents f972333 + 430dfe3 commit 97f59f4

File tree

9 files changed

+569
-31
lines changed

9 files changed

+569
-31
lines changed

CircuitPython_WeatherCloud/code.py

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import time
22
import random
3+
import audioio
34
import board
45
import busio
56
from digitalio import DigitalInOut
7+
import digitalio
68
import neopixel
79
from adafruit_esp32spi import adafruit_esp32spi
810
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
911
import adafruit_fancyled.adafruit_fancyled as fancy
1012

1113
print("ESP32 Open Weather API demo")
1214

13-
# Use cityname, country code where countrycode is ISO3166 format.
14-
# E.g. "New York, US" or "London, GB"
15-
LOCATION = "Manhattan, US"
15+
button = digitalio.DigitalInOut(board.A1)
16+
button.switch_to_input(pull=digitalio.Pull.UP)
17+
18+
wave_file = open("sound/Rain.wav", "rb")
19+
wave = audioio.WaveFile(wave_file)
20+
audio = audioio.AudioOut(board.A0)
21+
1622

1723
# Get wifi details and more from a secrets.py file
1824
try:
@@ -21,8 +27,12 @@
2127
print("WiFi secrets are kept in secrets.py, please add them there!")
2228
raise
2329

30+
# Use cityname, country code where countrycode is ISO3166 format.
31+
# E.g. "New York, US" or "London, GB"
32+
LOCATION = secrets['timezone']
33+
2434
# Set up where we'll be fetching data from
25-
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION
35+
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+secrets['timezone']
2636
DATA_SOURCE += "&appid="+secrets['openweather_token']
2737

2838
# If you are using a board with pre-defined ESP32 Pins:
@@ -32,64 +42,129 @@
3242

3343
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3444
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
35-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
45+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
3646
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
37-
pixels = neopixel.NeoPixel(board.D2, 16, brightness=1.0, auto_write=False)
47+
pixels = neopixel.NeoPixel(board.D2, 150, brightness=1.0, auto_write=False)
3848
pixels.fill(0x050505)
3949
pixels.show()
4050

4151
# clouds palette
42-
cloudy_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
43-
fancy.CRGB(0.5, 0.5, 0.5), # gray
44-
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
52+
cloudy_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
53+
fancy.CRGB(0.5, 0.5, 0.5), # gray
54+
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
4555
# sunny palette
46-
sunny_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
47-
fancy.CRGB(1.0, 1.0, 0.0),# Yellow
48-
fancy.CRGB(1.0, 0.5, 0.0),] # Orange
56+
sunny_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
57+
fancy.CRGB(1.0, 1.0, 0.0), # Yellow
58+
fancy.CRGB(1.0, 0.5, 0.0), ] # Orange
4959
# thunderstorm palette
50-
thunder_palette = [fancy.CRGB(0.0, 0.0, 1.0), # blue
51-
fancy.CRGB(0.5, 0.5, 0.5), # gray
52-
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
60+
thunder_palette = [fancy.CRGB(0.0, 0.0, 1.0), # blue
61+
fancy.CRGB(0.5, 0.5, 0.5), # gray
62+
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
5363
last_thunder_bolt = None
5464

5565
palette = None # current palette
5666
pal_offset = 0 # Positional offset into color palette to get it to 'spin'
5767
levels = (0.25, 0.3, 0.15) # Color balance / brightness for gamma function
5868
raining = False
69+
snowing = False
5970
thundering = False
71+
has_sound = False
6072

6173
weather_refresh = None
6274
weather_type = None
75+
button_mode = 4
76+
button_select = False
77+
78+
cloud_on = True
79+
6380
while True:
81+
if not button.value:
82+
button_mode = button_mode + 1
83+
print("Button Pressed")
84+
if button_mode > 4:
85+
button_mode = 0
86+
print("Mode is:", button_mode)
87+
pressed_time = time.monotonic()
88+
button_select = True
89+
weather_refresh = None
90+
while not button.value: # Debounce
91+
audio.stop()
92+
if (time.monotonic() - pressed_time) > 4:
93+
print("Turning OFF")
94+
cloud_on = False
95+
pixels.fill(0x000000) # bright white!
96+
pixels.show()
97+
while not cloud_on:
98+
while not button.value: # Debounce
99+
pass
100+
if not button.value:
101+
pressed_time = time.monotonic()
102+
print("Button Pressed")
103+
cloud_on = True
104+
button_select = False
105+
weather_refresh = None
106+
107+
if button_mode == 0:
108+
weather_type = 'Sunny'
109+
if button_mode == 1:
110+
weather_type = 'Clouds'
111+
if button_mode == 2:
112+
weather_type = 'Rain'
113+
if button_mode == 3:
114+
weather_type = 'Thunderstorm'
115+
if button_mode == 4:
116+
weather_type = 'Snow'
117+
64118
# only query the weather every 10 minutes (and on first run)
65119
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
66120
try:
67-
response = wifi.get(DATA_SOURCE).json()
68-
print("Response is", response)
69-
weather_type = response['weather'][0]['main']
70-
weather_type = 'Thunderstorm' # manually adjust weather type for testing!
71-
72-
print(weather_type) # See https://openweathermap.org/weather-conditions
121+
if not button_select:
122+
response = wifi.get(DATA_SOURCE).json()
123+
print("Response is", response)
124+
weather_type = response['weather'][0]['main']
125+
if weather_type == 'Clear':
126+
weather_type = 'Sunny'
127+
print(weather_type) # See https://openweathermap.org/weather-conditions
73128
# default to no rain or thunder
74-
raining = thundering = False
129+
raining = snowing = thundering = has_sound = False
130+
if weather_type == 'Sunny':
131+
palette = sunny_palette
132+
wave_file = open("sound/Clear.wav", "rb")
133+
wave = audioio.WaveFile(wave_file)
134+
has_sound = True
75135
if weather_type == 'Clouds':
76136
palette = cloudy_palette
137+
wave_file = open("sound/Clouds.wav", "rb")
138+
wave = audioio.WaveFile(wave_file)
139+
has_sound = True
77140
if weather_type == 'Rain':
78141
palette = cloudy_palette
142+
wave_file = open("sound/Rain.wav", "rb")
143+
wave = audioio.WaveFile(wave_file)
79144
raining = True
80-
if weather_type == 'Sunny':
81-
palette = sunny_palette
145+
has_sound = True
82146
if weather_type == 'Thunderstorm':
83147
palette = thunder_palette
84148
raining = thundering = True
149+
has_sound = True
85150
# pick next thunderbolt time now
86151
next_bolt_time = time.monotonic() + random.randint(1, 5)
152+
if weather_type == 'Snow':
153+
palette = cloudy_palette
154+
wave_file = open("sound/Snow.wav", "rb")
155+
wave = audioio.WaveFile(wave_file)
156+
snowing = True
157+
has_sound = True
87158
weather_refresh = time.monotonic()
88159
except RuntimeError as e:
89160
print("Some error occured, retrying! -", e)
90161
time.sleep(5)
91162
continue
92163

164+
if not audio.playing and has_sound:
165+
if not thundering:
166+
audio.play(wave)
167+
93168
if palette:
94169
for i in range(len(pixels)):
95170
color = fancy.palette_lookup(palette, pal_offset + i / len(pixels))
@@ -100,20 +175,35 @@
100175

101176
if raining:
102177
# don't have a droplet every time
103-
if random.randint(0, 25) == 0: # 1 out of 25 times
178+
for i in range(random.randint(1, 5)): # up to 3 times...
179+
pixels[random.randint(0, len(pixels)-1)] = 0x0000FF # make a random pixel Blue
180+
pixels.show()
181+
182+
if snowing:
183+
# don't have a droplet every time
184+
for i in range(random.randint(1, 5)): # up to 3 times...
104185
pixels[random.randint(0, len(pixels)-1)] = 0xFFFFFF # make a random pixel white
105-
pixels.show()
186+
pixels.show()
106187

107188
# if its time for a thunderbolt
108189
if thundering and (time.monotonic() > next_bolt_time):
109190
print("Ka Bam!")
110191
# fill pixels white, delay, a few times
111192
for i in range(random.randint(1, 3)): # up to 3 times...
112-
pixels.fill(0xFFFFFF) # bright white!
193+
pixels.fill(0xFFFFFF) # bright white!
113194
pixels.show()
114-
time.sleep(random.uniform(0.01, 0.2)) # pause
115-
pixels.fill(0x0F0F0F) # gray
195+
time.sleep(random.uniform(0.01, 0.2)) # pause
196+
pixels.fill(0x0F0F0F) # gray
116197
pixels.show()
117-
time.sleep(random.uniform(0.01, 0.3)) # pause
198+
time.sleep(random.uniform(0.01, 0.3)) # pause
118199
# pick next thunderbolt time now
119-
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s
200+
Thunder = random.randint(0, 2)
201+
if Thunder == 0:
202+
wave_file = open("sound/Thunderstorm0.wav", "rb")
203+
elif Thunder == 1:
204+
wave_file = open("sound/Thunderstorm1.wav", "rb")
205+
elif Thunder == 2:
206+
wave_file = open("sound/Thunderstorm2.wav", "rb")
207+
wave = audioio.WaveFile(wave_file)
208+
audio.play(wave)
209+
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s
431 KB
Binary file not shown.
455 KB
Binary file not shown.
220 KB
Binary file not shown.
71.8 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)