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