Skip to content

Commit 25db1ba

Browse files
committed
Secrets Cleanup: C and E
1 parent c322fbd commit 25db1ba

File tree

11 files changed

+130
-100
lines changed

11 files changed

+130
-100
lines changed

CLUE/CLUE_Rock_Paper_Scissors/advanced/code.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,10 @@
6262
from rps_display import RPSDisplay, blankScreen
6363

6464

65-
# Look for our name in secrets.py file if present
66-
ble_name = None
67-
try:
68-
from secrets import secrets
69-
ble_name = secrets.get("rps_name")
70-
if ble_name is None:
71-
ble_name = secrets.get("ble_name")
72-
if ble_name is None:
73-
print("INFO: No rps_name or ble_name entry found in secrets dict")
74-
except ImportError:
75-
pass # File is optional, reaching here is not a program error
76-
65+
# Look for our name in settings.toml file if present
66+
ble_name = os.getenv("rps_name", os.getenv("ble_name"))
67+
if ble_name is None:
68+
print("INFO: No rps_name or ble_name entry found in settings.toml")
7769

7870
debug = 1
7971

@@ -228,7 +220,7 @@ def button_right():
228220
# Intro screen with audio
229221
rps_display.introductionScreen()
230222

231-
# Enable the Bluetooth LE radio and set player's name (from secrets.py)
223+
# Enable the Bluetooth LE radio and set player's name (from settings.toml)
232224
ble = BLERadio()
233225
if ble_name is not None:
234226
ble.name = ble_name

Cheekmate/CircuitPython/code.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
CHEEKMATE: secret message receiver using WiFi, Adafruit IO and a haptic
77
buzzer. Periodically polls an Adafruit IO dashboard, converting new messages
88
to Morse code.
9-
10-
secrets.py file must be present and contain WiFi & Adafruit IO credentials.
119
"""
1210

11+
from os import getenv
1312
import gc
1413
import time
1514
import ssl
@@ -23,11 +22,20 @@
2322
import wifi
2423
from adafruit_io.adafruit_io import IO_HTTP
2524

26-
try:
27-
from secrets import secrets
28-
except ImportError:
29-
print("WiFi secrets are kept in secrets.py, please add them there!")
30-
raise
25+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
26+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
27+
ssid = getenv("CIRCUITPY_WIFI_SSID")
28+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
29+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
30+
aio_key = getenv("ADAFRUIT_AIO_KEY")
31+
32+
if None in [ssid, password, aio_username, aio_key]:
33+
raise RuntimeError(
34+
"WiFi and Adafruit IO settings are kept in settings.toml, "
35+
"please add them there. The settings file must contain "
36+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
37+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
38+
)
3139

3240
# CONFIGURABLE GLOBALS -----------------------------------------------------
3341

@@ -154,10 +162,10 @@ def play(string):
154162
# WIFI CONNECT -------------------------------------------------------------
155163

156164
try:
157-
print("Connecting to {}...".format(secrets["ssid"]), end="")
158-
wifi.radio.connect(secrets["ssid"], secrets["password"])
165+
print(f"Connecting to {ssid}...")
166+
wifi.radio.connect(ssid, password)
159167
print("OK")
160-
print("IP:", wifi.radio.ipv4_address)
168+
print(f"IP: {wifi.radio.ipv4_address}")
161169

162170
pool = socketpool.SocketPool(wifi.radio)
163171
requests = adafruit_requests.Session(pool, ssl.create_default_context())
@@ -169,8 +177,6 @@ def play(string):
169177

170178
# ADAFRUIT IO INITIALIZATION -----------------------------------------------
171179

172-
aio_username = secrets["aio_username"]
173-
aio_key = secrets["aio_key"]
174180
io = IO_HTTP(aio_username, aio_key, requests)
175181

176182
# SUCCESSFUL STARTUP, PROCEED INTO MAIN LOOP -------------------------------

CircuitPython_WeatherCloud/code.py

+33-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from os import getenv
56
import time
67
import random
78
import audioio
@@ -20,25 +21,29 @@
2021
button = digitalio.DigitalInOut(board.A1)
2122
button.switch_to_input(pull=digitalio.Pull.UP)
2223

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)
2526
audio = audioio.AudioOut(board.A0)
2627

28+
# Get WiFi details, ensure these are setup in settings.toml
29+
ssid = getenv("CIRCUITPY_WIFI_SSID")
30+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2731

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+
)
3439

3540
# Use cityname, country code where countrycode is ISO3166 format.
3641
# E.g. "New York, US" or "London, GB"
37-
LOCATION = secrets['timezone']
42+
LOCATION = getenv('timezone')
3843

3944
# 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')
4247

4348
# If you are using a board with pre-defined ESP32 Pins:
4449
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -47,8 +52,8 @@
4752

4853
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4954
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)
5257
pixels = neopixel.NeoPixel(board.D2, 150, brightness=1.0, auto_write=False)
5358
pixels.fill(0x050505)
5459
pixels.show()
@@ -134,18 +139,18 @@
134139
raining = snowing = thundering = has_sound = False
135140
if weather_type == 'Sunny':
136141
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)
139144
has_sound = True
140145
if weather_type == 'Clouds':
141146
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)
144149
has_sound = True
145150
if weather_type == 'Rain':
146151
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)
149154
raining = True
150155
has_sound = True
151156
if weather_type == 'Thunderstorm':
@@ -156,8 +161,8 @@
156161
next_bolt_time = time.monotonic() + random.randint(1, 5)
157162
if weather_type == 'Snow':
158163
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)
161166
snowing = True
162167
has_sound = True
163168
weather_refresh = time.monotonic()
@@ -204,11 +209,13 @@
204209
# pick next thunderbolt time now
205210
Thunder = random.randint(0, 2)
206211
if Thunder == 0:
207-
wave_file = open("sound/Thunderstorm0.wav", "rb")
212+
wave_filename = "sound/Thunderstorm0.wav"
208213
elif Thunder == 1:
209-
wave_file = open("sound/Thunderstorm1.wav", "rb")
214+
wave_filename = "sound/Thunderstorm1.wav"
210215
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)
214221
next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s

CircuitPython_WeatherCloud/secrets.py

-13
This file was deleted.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This file is where you keep private settings, passwords, and tokens!
6+
# If you put them in the code you risk committing that info or sharing it
7+
8+
9+
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
10+
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
11+
timezone="America/New_York" # http://worldtimeapi.org/timezones
12+
openweather_token="putYourOpenWeatherTokenHere"

CircuitPython_qrio/adafruit_io/code.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
"""
66
This demo is designed for the Kaluga development kit version 1.3 with the
7-
ILI9341 display. Your secrets.py must be populated with your wifi credentials
7+
ILI9341 display. Your settings.toml must be populated with your wifi credentials
88
and your Adafruit IO credentials.
99
"""
1010

11+
from os import getenv
1112
import ssl
12-
from secrets import secrets
1313
from ulab import numpy as np
1414
from terminalio import FONT
1515
import board
@@ -25,6 +25,21 @@
2525
from adafruit_io.adafruit_io import IO_MQTT
2626
import adafruit_minimqtt.adafruit_minimqtt as MQTT
2727

28+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
29+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
30+
ssid = getenv("CIRCUITPY_WIFI_SSID")
31+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
32+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
33+
aio_key = getenv("ADAFRUIT_AIO_KEY")
34+
35+
if None in [ssid, password, aio_username, aio_key]:
36+
raise RuntimeError(
37+
"WiFi and Adafruit IO settings are kept in settings.toml, "
38+
"please add them there. The settings file must contain "
39+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
40+
"'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum."
41+
)
42+
2843
# To change the name of the feed on adafruit_io, just modify this string:
2944
feed_name = "qrstring"
3045

@@ -53,14 +68,14 @@
5368
cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV
5469

5570
print("Connecting to WIFI")
56-
wifi.radio.connect(secrets["ssid"], secrets["password"])
71+
wifi.radio.connect(ssid, password)
5772
pool = socketpool.SocketPool(wifi.radio)
5873

5974
print("Connecting to Adafruit IO")
6075
mqtt_client = MQTT.MQTT(
6176
broker="io.adafruit.com",
62-
username=secrets["aio_username"],
63-
password=secrets["aio_key"],
77+
username=aio_username,
78+
password=aio_key,
6479
socket_pool=pool,
6580
ssl_context=ssl.create_default_context(),
6681
)

CircuitPython_qrio/usb_hid/code.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
"""
66
This demo is designed for the Kaluga development kit version 1.3 with the
7-
ILI9341 display. Your secrets.py must be populated with your wifi credentials
8-
and your Adafruit IO credentials.
7+
ILI9341 display.
98
"""
109

1110
from ulab import numpy as np

CircuitStonks/code.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
from os import getenv
56
import time
67
import board
78
from digitalio import DigitalInOut
@@ -14,24 +15,29 @@
1415

1516
minitft = minitft_featherwing.MiniTFTFeatherWing()
1617

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
18+
# Get WiFi details, ensure these are setup in settings.toml
19+
ssid = getenv("CIRCUITPY_WIFI_SSID")
20+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
21+
22+
if None in [ssid, password]:
23+
raise RuntimeError(
24+
"WiFi settings are kept in settings.toml, "
25+
"please add them there. The settings file must contain "
26+
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
27+
"at a minimum."
28+
)
2329

2430
# If you are using a board with pre-defined ESP32 Pins:
2531
esp32_cs = DigitalInOut(board.D13)
2632
esp32_ready = DigitalInOut(board.D11)
2733
esp32_reset = DigitalInOut(board.D12)
2834
spi = board.SPI()
2935
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
30-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)
36+
wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password)
3137

3238
# Symbol "INX" for S&P500, "DJIA" for Dow
3339
DATA_SOURCE = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&apikey="
34-
DATA_SOURCE += secrets['alphavantage_key']
40+
DATA_SOURCE += getenv('alphavantage_key')
3541
symbols = ["DJIA", "INX", "AAPL", "TSLA", "MSFT"]
3642

3743
# Set text, font, and color

CircuitStonks/secrets.py

-11
This file was deleted.

CircuitStonks/settings.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This file is where you keep private settings, passwords, and tokens!
6+
# If you put them in the code you risk committing that info or sharing it
7+
8+
CIRCUITPY_WIFI_SSID="your-wifi-ssid"
9+
CIRCUITPY_WIFI_PASSWORD="your-wifi-password"
10+
timezone="America/New_York" # http://worldtimeapi.org/timezones
11+
alphavantage_key="GRABAFREEKEYONLINE"

0 commit comments

Comments
 (0)