|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
| 5 | +from os import getenv |
5 | 6 | import time |
6 | 7 | import random |
7 | 8 | import audioio |
|
24 | 25 | wave = audiocore.WaveFile(wave_file) |
25 | 26 | audio = audioio.AudioOut(board.A0) |
26 | 27 |
|
| 28 | +# Get WiFi details, ensure these are setup in settings.toml |
| 29 | +ssid = getenv("CIRCUITPY_WIFI_SSID") |
| 30 | +password = getenv("CIRCUITPY_WIFI_PASSWORD") |
27 | 31 |
|
28 | | -# Get wifi details and more from a secrets.py file |
29 | | -try: |
30 | | - from secrets import secrets |
31 | | -except ImportError: |
32 | | - print("WiFi secrets are kept in secrets.py, please add them there!") |
33 | | - raise |
| 32 | +if None in [ssid, password]: |
| 33 | + raise RuntimeError( |
| 34 | + "WiFi settings are kept in settings.toml, " |
| 35 | + "please add them there. The settings file must contain " |
| 36 | + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " |
| 37 | + "at a minimum." |
| 38 | + ) |
34 | 39 |
|
35 | 40 | # Use cityname, country code where countrycode is ISO3166 format. |
36 | 41 | # E.g. "New York, US" or "London, GB" |
37 | | -LOCATION = secrets['timezone'] |
| 42 | +LOCATION = getenv('timezone') |
38 | 43 |
|
39 | 44 | # Set up where we'll be fetching data from |
40 | | -DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+secrets['timezone'] |
41 | | -DATA_SOURCE += "&appid="+secrets['openweather_token'] |
| 45 | +DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION |
| 46 | +DATA_SOURCE += "&appid="+getenv('openweather_token') |
42 | 47 |
|
43 | 48 | # If you are using a board with pre-defined ESP32 Pins: |
44 | 49 | esp32_cs = DigitalInOut(board.ESP_CS) |
|
47 | 52 |
|
48 | 53 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
49 | 54 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
50 | | -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
51 | | -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 55 | +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
| 56 | +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) |
52 | 57 | pixels = neopixel.NeoPixel(board.D2, 150, brightness=1.0, auto_write=False) |
53 | 58 | pixels.fill(0x050505) |
54 | 59 | pixels.show() |
|
132 | 137 | print(weather_type) # See https://openweathermap.org/weather-conditions |
133 | 138 | # default to no rain or thunder |
134 | 139 | raining = snowing = thundering = has_sound = False |
| 140 | + wave_filename = None |
135 | 141 | if weather_type == 'Sunny': |
136 | 142 | palette = sunny_palette |
137 | | - wave_file = open("sound/Clear.wav", "rb") |
138 | | - wave = audiocore.WaveFile(wave_file) |
139 | | - has_sound = True |
| 143 | + wave_filename = "sound/Clear.wav" |
140 | 144 | if weather_type == 'Clouds': |
141 | 145 | palette = cloudy_palette |
142 | | - wave_file = open("sound/Clouds.wav", "rb") |
143 | | - wave = audiocore.WaveFile(wave_file) |
144 | | - has_sound = True |
| 146 | + wave_filename = "sound/Clouds.wav" |
145 | 147 | if weather_type == 'Rain': |
146 | 148 | palette = cloudy_palette |
147 | | - wave_file = open("sound/Rain.wav", "rb") |
148 | | - wave = audiocore.WaveFile(wave_file) |
| 149 | + wave_filename = "sound/Rain.wav" |
149 | 150 | raining = True |
150 | | - has_sound = True |
151 | 151 | if weather_type == 'Thunderstorm': |
152 | 152 | palette = thunder_palette |
153 | 153 | raining = thundering = True |
|
156 | 156 | next_bolt_time = time.monotonic() + random.randint(1, 5) |
157 | 157 | if weather_type == 'Snow': |
158 | 158 | palette = cloudy_palette |
159 | | - wave_file = open("sound/Snow.wav", "rb") |
160 | | - wave = audiocore.WaveFile(wave_file) |
| 159 | + wave_filename = "sound/Snow.wav" |
161 | 160 | snowing = True |
| 161 | + if wave_filename: |
| 162 | + wave_file = open(wave_filename, "rb") |
| 163 | + wave = audiocore.WaveFile(wave_file) |
162 | 164 | has_sound = True |
163 | 165 | weather_refresh = time.monotonic() |
164 | 166 | except RuntimeError as e: |
|
204 | 206 | # pick next thunderbolt time now |
205 | 207 | Thunder = random.randint(0, 2) |
206 | 208 | if Thunder == 0: |
207 | | - wave_file = open("sound/Thunderstorm0.wav", "rb") |
| 209 | + wave_filename = "sound/Thunderstorm0.wav" |
208 | 210 | elif Thunder == 1: |
209 | | - wave_file = open("sound/Thunderstorm1.wav", "rb") |
| 211 | + wave_filename = "sound/Thunderstorm1.wav" |
210 | 212 | elif Thunder == 2: |
211 | | - wave_file = open("sound/Thunderstorm2.wav", "rb") |
212 | | - wave = audiocore.WaveFile(wave_file) |
213 | | - audio.play(wave) |
| 213 | + wave_filename = "sound/Thunderstorm2.wav" |
| 214 | + if wave_filename: |
| 215 | + wave_file = open(wave_filename, "rb") |
| 216 | + wave = audiocore.WaveFile(wave_file) |
| 217 | + audio.play(wave) |
214 | 218 | next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s |
0 commit comments