|
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
|
|
20 | 21 | button = digitalio.DigitalInOut(board.A1)
|
21 | 22 | button.switch_to_input(pull=digitalio.Pull.UP)
|
22 | 23 |
|
23 |
| -wave_file = open("sound/Rain.wav", "rb") |
24 |
| -wave = audiocore.WaveFile(wave_file) |
| 24 | +with open("sound/Rain.wav", "rb") as wave_file: |
| 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()
|
|
134 | 139 | raining = snowing = thundering = has_sound = False
|
135 | 140 | if weather_type == 'Sunny':
|
136 | 141 | palette = sunny_palette
|
137 |
| - wave_file = open("sound/Clear.wav", "rb") |
138 |
| - wave = audiocore.WaveFile(wave_file) |
| 142 | + with open("sound/Clear.wav", "rb") as wave_file: |
| 143 | + wave = audiocore.WaveFile(wave_file) |
139 | 144 | has_sound = True
|
140 | 145 | if weather_type == 'Clouds':
|
141 | 146 | palette = cloudy_palette
|
142 |
| - wave_file = open("sound/Clouds.wav", "rb") |
143 |
| - wave = audiocore.WaveFile(wave_file) |
| 147 | + with open("sound/Clouds.wav", "rb") as wave_file: |
| 148 | + wave = audiocore.WaveFile(wave_file) |
144 | 149 | has_sound = True
|
145 | 150 | if weather_type == 'Rain':
|
146 | 151 | palette = cloudy_palette
|
147 |
| - wave_file = open("sound/Rain.wav", "rb") |
148 |
| - wave = audiocore.WaveFile(wave_file) |
| 152 | + with open("sound/Rain.wav", "rb") as wave_file: |
| 153 | + wave = audiocore.WaveFile(wave_file) |
149 | 154 | raining = True
|
150 | 155 | has_sound = True
|
151 | 156 | if weather_type == 'Thunderstorm':
|
|
156 | 161 | next_bolt_time = time.monotonic() + random.randint(1, 5)
|
157 | 162 | if weather_type == 'Snow':
|
158 | 163 | palette = cloudy_palette
|
159 |
| - wave_file = open("sound/Snow.wav", "rb") |
160 |
| - wave = audiocore.WaveFile(wave_file) |
| 164 | + with open("sound/Snow.wav", "rb") as wave_file: |
| 165 | + wave = audiocore.WaveFile(wave_file) |
161 | 166 | snowing = True
|
162 | 167 | has_sound = True
|
163 | 168 | weather_refresh = time.monotonic()
|
|
204 | 209 | # pick next thunderbolt time now
|
205 | 210 | Thunder = random.randint(0, 2)
|
206 | 211 | if Thunder == 0:
|
207 |
| - wave_file = open("sound/Thunderstorm0.wav", "rb") |
| 212 | + wave_filename = "sound/Thunderstorm0.wav" |
208 | 213 | elif Thunder == 1:
|
209 |
| - wave_file = open("sound/Thunderstorm1.wav", "rb") |
| 214 | + wave_filename = "sound/Thunderstorm1.wav" |
210 | 215 | elif Thunder == 2:
|
211 |
| - wave_file = open("sound/Thunderstorm2.wav", "rb") |
212 |
| - wave = audiocore.WaveFile(wave_file) |
213 |
| - audio.play(wave) |
| 216 | + wave_filename = "sound/Thunderstorm2.wav" |
| 217 | + if wave_filename: |
| 218 | + with open(wave_filename, "rb") as wave_file: |
| 219 | + wave = audiocore.WaveFile(wave_file) |
| 220 | + audio.play(wave) |
214 | 221 | next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s
|
0 commit comments