diff --git a/CLUE/CLUE_Rock_Paper_Scissors/advanced/code.py b/CLUE/CLUE_Rock_Paper_Scissors/advanced/code.py index 9da01840d..a9bbf2169 100644 --- a/CLUE/CLUE_Rock_Paper_Scissors/advanced/code.py +++ b/CLUE/CLUE_Rock_Paper_Scissors/advanced/code.py @@ -62,18 +62,10 @@ from rps_display import RPSDisplay, blankScreen -# Look for our name in secrets.py file if present -ble_name = None -try: - from secrets import secrets - ble_name = secrets.get("rps_name") - if ble_name is None: - ble_name = secrets.get("ble_name") - if ble_name is None: - print("INFO: No rps_name or ble_name entry found in secrets dict") -except ImportError: - pass # File is optional, reaching here is not a program error - +# Look for our name in settings.toml file if present +ble_name = os.getenv("rps_name", os.getenv("ble_name")) +if ble_name is None: + print("INFO: No rps_name or ble_name entry found in settings.toml") debug = 1 @@ -228,7 +220,7 @@ def button_right(): # Intro screen with audio rps_display.introductionScreen() -# Enable the Bluetooth LE radio and set player's name (from secrets.py) +# Enable the Bluetooth LE radio and set player's name (from settings.toml) ble = BLERadio() if ble_name is not None: ble.name = ble_name diff --git a/Cheekmate/CircuitPython/code.py b/Cheekmate/CircuitPython/code.py index 2f3078432..cd1a88cd4 100755 --- a/Cheekmate/CircuitPython/code.py +++ b/Cheekmate/CircuitPython/code.py @@ -6,10 +6,9 @@ CHEEKMATE: secret message receiver using WiFi, Adafruit IO and a haptic buzzer. Periodically polls an Adafruit IO dashboard, converting new messages to Morse code. - -secrets.py file must be present and contain WiFi & Adafruit IO credentials. """ +from os import getenv import gc import time import ssl @@ -23,11 +22,20 @@ import wifi from adafruit_io.adafruit_io import IO_HTTP -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") + +if None in [ssid, password, aio_username, aio_key]: + raise RuntimeError( + "WiFi and Adafruit IO settings are kept in settings.toml, " + "please add them there. The settings file must contain " + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " + "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." + ) # CONFIGURABLE GLOBALS ----------------------------------------------------- @@ -154,10 +162,10 @@ def play(string): # WIFI CONNECT ------------------------------------------------------------- try: - print("Connecting to {}...".format(secrets["ssid"]), end="") - wifi.radio.connect(secrets["ssid"], secrets["password"]) + print(f"Connecting to {ssid}...") + wifi.radio.connect(ssid, password) print("OK") - print("IP:", wifi.radio.ipv4_address) + print(f"IP: {wifi.radio.ipv4_address}") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) @@ -169,8 +177,6 @@ def play(string): # ADAFRUIT IO INITIALIZATION ----------------------------------------------- -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] io = IO_HTTP(aio_username, aio_key, requests) # SUCCESSFUL STARTUP, PROCEED INTO MAIN LOOP ------------------------------- diff --git a/CircuitPython_WeatherCloud/code.py b/CircuitPython_WeatherCloud/code.py index f86ee2333..ed046e914 100644 --- a/CircuitPython_WeatherCloud/code.py +++ b/CircuitPython_WeatherCloud/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import random import audioio @@ -24,21 +25,25 @@ wave = audiocore.WaveFile(wave_file) audio = audioio.AudioOut(board.A0) +# Get WiFi details, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +if None in [ssid, password]: + raise RuntimeError( + "WiFi settings are kept in settings.toml, " + "please add them there. The settings file must contain " + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " + "at a minimum." + ) # Use cityname, country code where countrycode is ISO3166 format. # E.g. "New York, US" or "London, GB" -LOCATION = secrets['timezone'] +LOCATION = getenv('timezone') # Set up where we'll be fetching data from -DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+secrets['timezone'] -DATA_SOURCE += "&appid="+secrets['openweather_token'] +DATA_SOURCE = "http://api.openweathermap.org/data/2.5/weather?q="+LOCATION +DATA_SOURCE += "&appid="+getenv('openweather_token') # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -47,8 +52,8 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) pixels = neopixel.NeoPixel(board.D2, 150, brightness=1.0, auto_write=False) pixels.fill(0x050505) pixels.show() @@ -132,22 +137,17 @@ print(weather_type) # See https://openweathermap.org/weather-conditions # default to no rain or thunder raining = snowing = thundering = has_sound = False + wave_filename = None if weather_type == 'Sunny': palette = sunny_palette - wave_file = open("sound/Clear.wav", "rb") - wave = audiocore.WaveFile(wave_file) - has_sound = True + wave_filename = "sound/Clear.wav" if weather_type == 'Clouds': palette = cloudy_palette - wave_file = open("sound/Clouds.wav", "rb") - wave = audiocore.WaveFile(wave_file) - has_sound = True + wave_filename = "sound/Clouds.wav" if weather_type == 'Rain': palette = cloudy_palette - wave_file = open("sound/Rain.wav", "rb") - wave = audiocore.WaveFile(wave_file) + wave_filename = "sound/Rain.wav" raining = True - has_sound = True if weather_type == 'Thunderstorm': palette = thunder_palette raining = thundering = True @@ -156,9 +156,11 @@ next_bolt_time = time.monotonic() + random.randint(1, 5) if weather_type == 'Snow': palette = cloudy_palette - wave_file = open("sound/Snow.wav", "rb") - wave = audiocore.WaveFile(wave_file) + wave_filename = "sound/Snow.wav" snowing = True + if wave_filename: + wave_file = open(wave_filename, "rb") + wave = audiocore.WaveFile(wave_file) has_sound = True weather_refresh = time.monotonic() except RuntimeError as e: @@ -204,11 +206,13 @@ # pick next thunderbolt time now Thunder = random.randint(0, 2) if Thunder == 0: - wave_file = open("sound/Thunderstorm0.wav", "rb") + wave_filename = "sound/Thunderstorm0.wav" elif Thunder == 1: - wave_file = open("sound/Thunderstorm1.wav", "rb") + wave_filename = "sound/Thunderstorm1.wav" elif Thunder == 2: - wave_file = open("sound/Thunderstorm2.wav", "rb") - wave = audiocore.WaveFile(wave_file) - audio.play(wave) + wave_filename = "sound/Thunderstorm2.wav" + if wave_filename: + wave_file = open(wave_filename, "rb") + wave = audiocore.WaveFile(wave_file) + audio.play(wave) next_bolt_time = time.monotonic() + random.randint(5, 15) # between 5 and 15 s diff --git a/CircuitPython_WeatherCloud/secrets.py b/CircuitPython_WeatherCloud/secrets.py deleted file mode 100644 index 63bdfe3c2..000000000 --- a/CircuitPython_WeatherCloud/secrets.py +++ /dev/null @@ -1,13 +0,0 @@ -# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries -# -# SPDX-License-Identifier: MIT - -# This file is where you keep secret settings, passwords, and tokens! -# If you put them in the code you risk committing that info or sharing it - -secrets = { - 'ssid' : 'my_ssid', - 'password' : 'my_pass', - 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones - 'openweather_token' : 'putYourOpenWeatherTokenHere', - } diff --git a/CircuitPython_WeatherCloud/settings.toml b/CircuitPython_WeatherCloud/settings.toml new file mode 100644 index 000000000..d751ea31a --- /dev/null +++ b/CircuitPython_WeatherCloud/settings.toml @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +# This file is where you keep private settings, passwords, and tokens! +# If you put them in the code you risk committing that info or sharing it + + +CIRCUITPY_WIFI_SSID="your-wifi-ssid" +CIRCUITPY_WIFI_PASSWORD="your-wifi-password" +timezone="America/New_York" # http://worldtimeapi.org/timezones +openweather_token="putYourOpenWeatherTokenHere" diff --git a/CircuitPython_qrio/adafruit_io/code.py b/CircuitPython_qrio/adafruit_io/code.py index 84326cb04..0012af8f0 100644 --- a/CircuitPython_qrio/adafruit_io/code.py +++ b/CircuitPython_qrio/adafruit_io/code.py @@ -4,12 +4,12 @@ """ This demo is designed for the Kaluga development kit version 1.3 with the -ILI9341 display. Your secrets.py must be populated with your wifi credentials +ILI9341 display. Your settings.toml must be populated with your wifi credentials and your Adafruit IO credentials. """ +from os import getenv import ssl -from secrets import secrets from ulab import numpy as np from terminalio import FONT import board @@ -25,6 +25,21 @@ from adafruit_io.adafruit_io import IO_MQTT import adafruit_minimqtt.adafruit_minimqtt as MQTT +# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml +# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +aio_username = getenv("ADAFRUIT_AIO_USERNAME") +aio_key = getenv("ADAFRUIT_AIO_KEY") + +if None in [ssid, password, aio_username, aio_key]: + raise RuntimeError( + "WiFi and Adafruit IO settings are kept in settings.toml, " + "please add them there. The settings file must contain " + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " + "'ADAFRUIT_AIO_USERNAME' and 'ADAFRUIT_AIO_KEY' at a minimum." + ) + # To change the name of the feed on adafruit_io, just modify this string: feed_name = "qrstring" @@ -53,14 +68,14 @@ cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV print("Connecting to WIFI") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) pool = socketpool.SocketPool(wifi.radio) print("Connecting to Adafruit IO") mqtt_client = MQTT.MQTT( broker="io.adafruit.com", - username=secrets["aio_username"], - password=secrets["aio_key"], + username=aio_username, + password=aio_key, socket_pool=pool, ssl_context=ssl.create_default_context(), ) diff --git a/CircuitPython_qrio/usb_hid/code.py b/CircuitPython_qrio/usb_hid/code.py index 5d7aeeee6..cf8288eac 100644 --- a/CircuitPython_qrio/usb_hid/code.py +++ b/CircuitPython_qrio/usb_hid/code.py @@ -4,8 +4,7 @@ """ This demo is designed for the Kaluga development kit version 1.3 with the -ILI9341 display. Your secrets.py must be populated with your wifi credentials -and your Adafruit IO credentials. +ILI9341 display. """ from ulab import numpy as np diff --git a/CircuitStonks/code.py b/CircuitStonks/code.py index 9120bb98f..527ab97ef 100644 --- a/CircuitStonks/code.py +++ b/CircuitStonks/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board from digitalio import DigitalInOut @@ -14,12 +15,17 @@ minitft = minitft_featherwing.MiniTFTFeatherWing() -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +# Get WiFi details, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") + +if None in [ssid, password]: + raise RuntimeError( + "WiFi settings are kept in settings.toml, " + "please add them there. The settings file must contain " + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " + "at a minimum." + ) # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.D13) @@ -27,11 +33,11 @@ esp32_reset = DigitalInOut(board.D12) spi = board.SPI() esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password) # Symbol "INX" for S&P500, "DJIA" for Dow DATA_SOURCE = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&apikey=" -DATA_SOURCE += secrets['alphavantage_key'] +DATA_SOURCE += getenv('alphavantage_key') symbols = ["DJIA", "INX", "AAPL", "TSLA", "MSFT"] # Set text, font, and color diff --git a/CircuitStonks/secrets.py b/CircuitStonks/secrets.py deleted file mode 100644 index ab38afa01..000000000 --- a/CircuitStonks/secrets.py +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries -# -# SPDX-License-Identifier: MIT - - -secrets = { - 'ssid' : 'myssid', - 'password' : 'mypassword', - 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones - 'alphavantage_key' : 'GRABAFREEKEYONLINE' - } diff --git a/CircuitStonks/settings.toml b/CircuitStonks/settings.toml new file mode 100644 index 000000000..7ab57aeb4 --- /dev/null +++ b/CircuitStonks/settings.toml @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +# This file is where you keep private settings, passwords, and tokens! +# If you put them in the code you risk committing that info or sharing it + +CIRCUITPY_WIFI_SSID="your-wifi-ssid" +CIRCUITPY_WIFI_PASSWORD="your-wifi-password" +timezone="America/New_York" # http://worldtimeapi.org/timezones +alphavantage_key="GRABAFREEKEYONLINE" diff --git a/ESP32_S2_WiFi_Tests/code.py b/ESP32_S2_WiFi_Tests/code.py index d20135e84..2fe6494e1 100644 --- a/ESP32_S2_WiFi_Tests/code.py +++ b/ESP32_S2_WiFi_Tests/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import ipaddress import ssl import wifi @@ -13,12 +14,17 @@ JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php" JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython" -# Get wifi details and more from a secrets.py file -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise +# Get WiFi details, ensure these are setup in settings.toml +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") + +if None in [ssid, password]: + raise RuntimeError( + "WiFi settings are kept in settings.toml, " + "please add them there. The settings file must contain " + "'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', " + "at a minimum." + ) print("ESP32-S2 WebClient Test") @@ -30,10 +36,10 @@ network.rssi, network.channel)) wifi.radio.stop_scanning_networks() -print("Connecting to %s"%secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!"%secrets["ssid"]) -print("My IP address is", wifi.radio.ipv4_address) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") +print(f"My IP address is {wifi.radio.ipv4_address}") ipv4 = ipaddress.ip_address("8.8.4.4") print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))