Add files via upload

This commit is contained in:
Richard Albritton 2020-03-31 14:49:27 -07:00 committed by GitHub
parent 6ea9c4484e
commit 7776f7f066
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 212 additions and 119 deletions

View file

@ -1,119 +1,212 @@
import time import time
import random import audioio
import board import random
import busio import board
from digitalio import DigitalInOut import busio
import neopixel from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi import digitalio
from adafruit_esp32spi import adafruit_esp32spi_wifimanager import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
print("ESP32 Open Weather API demo") import adafruit_fancyled.adafruit_fancyled as fancy
# Use cityname, country code where countrycode is ISO3166 format. print("ESP32 Open Weather API demo")
# E.g. "New York, US" or "London, GB"
LOCATION = "Manhattan, US" button = digitalio.DigitalInOut(board.A1)
button.switch_to_input(pull=digitalio.Pull.UP)
# Get wifi details and more from a secrets.py file
try: wave_file = open("sound/Rain.wav", "rb")
from secrets import secrets wave = audioio.WaveFile(wave_file)
except ImportError: audio = audioio.AudioOut(board.A0)
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Get wifi details and more from a secrets.py file
# Set up where we'll be fetching data from try:
DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION from secrets import secrets
DATA_SOURCE += "&appid="+secrets['openweather_token'] except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
# If you are using a board with pre-defined ESP32 Pins: raise
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY) # Use cityname, country code where countrycode is ISO3166 format.
esp32_reset = DigitalInOut(board.ESP_RESET) # E.g. "New York, US" or "London, GB"
LOCATION = secrets['timezone']
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) # Set up where we'll be fetching data from
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+secrets['timezone']
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) DATA_SOURCE += "&appid="+secrets['openweather_token']
pixels = neopixel.NeoPixel(board.D2, 16, brightness=1.0, auto_write=False)
pixels.fill(0x050505) # If you are using a board with pre-defined ESP32 Pins:
pixels.show() esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
# clouds palette esp32_reset = DigitalInOut(board.ESP_RESET)
cloudy_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
fancy.CRGB(0.5, 0.5, 0.5), # gray spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
# sunny palette status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
sunny_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
fancy.CRGB(1.0, 1.0, 0.0),# Yellow pixels = neopixel.NeoPixel(board.D2, 150, brightness=1.0, auto_write=False)
fancy.CRGB(1.0, 0.5, 0.0),] # Orange pixels.fill(0x050505)
# thunderstorm palette pixels.show()
thunder_palette = [fancy.CRGB(0.0, 0.0, 1.0), # blue
fancy.CRGB(0.5, 0.5, 0.5), # gray # clouds palette
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray cloudy_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
last_thunder_bolt = None fancy.CRGB(0.5, 0.5, 0.5), # gray
fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
palette = None # current palette # sunny palette
pal_offset = 0 # Positional offset into color palette to get it to 'spin' sunny_palette = [fancy.CRGB(1.0, 1.0, 1.0), # White
levels = (0.25, 0.3, 0.15) # Color balance / brightness for gamma function fancy.CRGB(1.0, 1.0, 0.0),# Yellow
raining = False fancy.CRGB(1.0, 0.5, 0.0),] # Orange
thundering = False # thunderstorm palette
thunder_palette = [fancy.CRGB(0.0, 0.0, 1.0), # blue
weather_refresh = None fancy.CRGB(0.5, 0.5, 0.5), # gray
weather_type = None fancy.CRGB(0.5, 0.5, 1.0)] # blue-gray
while True: last_thunder_bolt = None
# only query the weather every 10 minutes (and on first run)
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600: palette = None # current palette
try: pal_offset = 0 # Positional offset into color palette to get it to 'spin'
response = wifi.get(DATA_SOURCE).json() levels = (0.25, 0.3, 0.15) # Color balance / brightness for gamma function
print("Response is", response) raining = False
weather_type = response['weather'][0]['main'] snowing = False
weather_type = 'Thunderstorm' # manually adjust weather type for testing! thundering = False
has_sound = False
print(weather_type) # See https://openweathermap.org/weather-conditions
# default to no rain or thunder weather_refresh = None
raining = thundering = False weather_type = None
if weather_type == 'Clouds': button_mode = 4
palette = cloudy_palette button_select = False
if weather_type == 'Rain':
palette = cloudy_palette cloud_on = True
raining = True
if weather_type == 'Sunny': while True:
palette = sunny_palette if not button.value:
if weather_type == 'Thunderstorm': button_mode = button_mode + 1
palette = thunder_palette print("Button Pressed")
raining = thundering = True if button_mode > 4:
# pick next thunderbolt time now button_mode = 0
next_bolt_time = time.monotonic() + random.randint(1, 5) print("Mode is:", button_mode)
weather_refresh = time.monotonic() pressed_time = time.monotonic()
except RuntimeError as e: button_select = True
print("Some error occured, retrying! -", e) weather_refresh = None
time.sleep(5) while not button.value:
continue audio.stop()
#print("Debounce")
if palette: if (time.monotonic() - pressed_time) > 4:
for i in range(len(pixels)): print("Turning OFF")
color = fancy.palette_lookup(palette, pal_offset + i / len(pixels)) cloud_on = False
color = fancy.gamma_adjust(color, brightness=levels) pixels.fill(0x000000) # bright white!
pixels[i] = color.pack() pixels.show()
pixels.show() while not cloud_on:
pal_offset += 0.01 # Bigger number = faster spin while not button.value:
pass
if raining: #print("Debounce")
# don't have a droplet every time if not button.value:
if random.randint(0, 25) == 0: # 1 out of 25 times pressed_time = time.monotonic()
pixels[random.randint(0, len(pixels)-1)] = 0xFFFFFF # make a random pixel white print("Button Pressed")
pixels.show() cloud_on = True
button_select = False
# if its time for a thunderbolt weather_refresh = None
if thundering and (time.monotonic() > next_bolt_time):
print("Ka Bam!") if button_mode == 0:
# fill pixels white, delay, a few times weather_type = 'Sunny'
for i in range(random.randint(1, 3)): # up to 3 times... if button_mode == 1:
pixels.fill(0xFFFFFF) # bright white! weather_type = 'Clouds'
pixels.show() if button_mode == 2:
time.sleep(random.uniform(0.01, 0.2)) # pause weather_type = 'Rain'
pixels.fill(0x0F0F0F) # gray if button_mode == 3:
pixels.show() weather_type = 'Thunderstorm'
time.sleep(random.uniform(0.01, 0.3)) # pause if button_mode == 4:
# pick next thunderbolt time now weather_type = 'Snow'
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s
# only query the weather every 10 minutes (and on first run)
if (not weather_refresh) or (time.monotonic() - weather_refresh) > 600:
try:
if not button_select:
response = wifi.get(DATA_SOURCE).json()
print("Response is", response)
weather_type = response['weather'][0]['main']
#weather_type = 'Rain' # manually adjust weather type for testing!
if weather_type == 'Clear':
weather_type = 'Sunny'
print(weather_type) # See https://openweathermap.org/weather-conditions
# default to no rain or thunder
raining = snowing = thundering = has_sound = False
if weather_type == 'Sunny':
palette = sunny_palette
wave_file = open("sound/Clear.wav", "rb")
wave = audioio.WaveFile(wave_file)
has_sound = True
if weather_type == 'Clouds':
palette = cloudy_palette
wave_file = open("sound/Clouds.wav", "rb")
wave = audioio.WaveFile(wave_file)
has_sound = True
if weather_type == 'Rain':
palette = cloudy_palette
wave_file = open("sound/Rain.wav", "rb")
wave = audioio.WaveFile(wave_file)
raining = True
has_sound = True
if weather_type == 'Thunderstorm':
palette = thunder_palette
raining = thundering = True
has_sound = True
# pick next thunderbolt time now
next_bolt_time = time.monotonic() + random.randint(1, 5)
if weather_type == 'Snow':
palette = cloudy_palette
wave_file = open("sound/Snow.wav", "rb")
wave = audioio.WaveFile(wave_file)
snowing = True
has_sound = True
weather_refresh = time.monotonic()
except RuntimeError as e:
print("Some error occured, retrying! -", e)
time.sleep(5)
continue
if not audio.playing and has_sound:
if not thundering:
audio.play(wave)
if palette:
for i in range(len(pixels)):
color = fancy.palette_lookup(palette, pal_offset + i / len(pixels))
color = fancy.gamma_adjust(color, brightness=levels)
pixels[i] = color.pack()
pixels.show()
pal_offset += 0.01 # Bigger number = faster spin
if raining:
# don't have a droplet every time
for i in range(random.randint(1, 5)): # up to 3 times...
pixels[random.randint(0, len(pixels)-1)] = 0x0000FF # make a random pixel Blue
pixels.show()
if snowing:
# don't have a droplet every time
for i in range(random.randint(1, 5)): # up to 3 times...
pixels[random.randint(0, len(pixels)-1)] = 0xFFFFFF # make a random pixel white
pixels.show()
# if its time for a thunderbolt
if thundering and (time.monotonic() > next_bolt_time):
print("Ka Bam!")
# fill pixels white, delay, a few times
for i in range(random.randint(1, 3)): # up to 3 times...
pixels.fill(0xFFFFFF) # bright white!
pixels.show()
time.sleep(random.uniform(0.01, 0.2)) # pause
pixels.fill(0x0F0F0F) # gray
pixels.show()
time.sleep(random.uniform(0.01, 0.3)) # pause
# pick next thunderbolt time now
Thunder = random.randint(0, 2)
if Thunder == 0:
wave_file = open("sound/Thunderstorm0.wav", "rb")
elif Thunder == 1:
wave_file = open("sound/Thunderstorm1.wav", "rb")
elif Thunder == 2:
wave_file = open("sound/Thunderstorm2.wav", "rb")
wave = audioio.WaveFile(wave_file)
audio.play(wave)
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.