From 2642e4885612ce71b0896da49c07ea8752384f19 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Thu, 10 Apr 2025 10:27:40 -0700 Subject: [PATCH] Secrets Cleanup Q-W --- Minesweep/.circuitpython.skip | 1 - .../Minesweep}/SpriteSheet.bmp | Bin {Minesweep => PyPortal/Minesweep}/code.py | 2 +- {Minesweep => PyPortal/Minesweep}/lose.wav | Bin {Minesweep => PyPortal/Minesweep}/win.wav | Bin {Twitter_API => PyPortal/Twitter_API}/code.py | 21 +++-- .../Twitter_API}/fonts/Helvetica-Bold-16.bdf | 0 .../Twitter_API}/twitter_background.bmp | Bin QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/AIO/code.py | 21 ++--- QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/NTP/code.py | 12 ++- QT_Py/QT_Py_Cube/code.py | 28 +++++-- .../featherTft_bme680/code.py | 24 ++++-- .../qtPyEsp32S2_co2/code.py | 22 +++-- Shadow_Box/code.py | 39 +++++---- Smart_Alarm_Clock/button/code.py | 34 +++++--- Smart_Alarm_Clock/code.py | 78 ++++++++++-------- .../code.py | 23 ++++-- TFT_Spirit_Board/pyportal/code.py | 25 ++++-- TFT_Spirit_Board/pyportal_titano/code.py | 25 ++++-- Two_Way_Servo_Telegraph/code.py | 31 ++++--- Weather_Display_Matrix/.circuitpython.skip | 1 - Weather_Display_Matrix/code.py | 25 ++++-- Webhook_Plant_Sensor/code.py | 31 ++++--- WiFi_Mailbox_Notifier/mailbox_code/code.py | 30 ++++--- .../code.py | 26 +++--- 25 files changed, 308 insertions(+), 191 deletions(-) delete mode 100644 Minesweep/.circuitpython.skip rename {Minesweep => PyPortal/Minesweep}/SpriteSheet.bmp (100%) rename {Minesweep => PyPortal/Minesweep}/code.py (99%) rename {Minesweep => PyPortal/Minesweep}/lose.wav (100%) rename {Minesweep => PyPortal/Minesweep}/win.wav (100%) rename {Twitter_API => PyPortal/Twitter_API}/code.py (78%) rename {Twitter_API => PyPortal/Twitter_API}/fonts/Helvetica-Bold-16.bdf (100%) rename {Twitter_API => PyPortal/Twitter_API}/twitter_background.bmp (100%) delete mode 100644 Weather_Display_Matrix/.circuitpython.skip diff --git a/Minesweep/.circuitpython.skip b/Minesweep/.circuitpython.skip deleted file mode 100644 index 353ad158a..000000000 --- a/Minesweep/.circuitpython.skip +++ /dev/null @@ -1 +0,0 @@ -Minesweep/code.py 200: Simplify chained comparison between the operands (chained-comparison) diff --git a/Minesweep/SpriteSheet.bmp b/PyPortal/Minesweep/SpriteSheet.bmp similarity index 100% rename from Minesweep/SpriteSheet.bmp rename to PyPortal/Minesweep/SpriteSheet.bmp diff --git a/Minesweep/code.py b/PyPortal/Minesweep/code.py similarity index 99% rename from Minesweep/code.py rename to PyPortal/Minesweep/code.py index 409686310..95deb634e 100644 --- a/Minesweep/code.py +++ b/PyPortal/Minesweep/code.py @@ -201,7 +201,7 @@ def play_a_game(): set_data(touch_x, touch_y, BOMBDEATH) #reveal a red bomb tilegrid[touch_x, touch_y] = BOMBDEATH return False #lost - elif under_the_tile > OPEN0 and under_the_tile <= OPEN8: + elif OPEN0 < under_the_tile <= OPEN8: tilegrid[touch_x, touch_y] = under_the_tile elif under_the_tile == OPEN0: tilegrid[touch_x, touch_y] = BLANK diff --git a/Minesweep/lose.wav b/PyPortal/Minesweep/lose.wav similarity index 100% rename from Minesweep/lose.wav rename to PyPortal/Minesweep/lose.wav diff --git a/Minesweep/win.wav b/PyPortal/Minesweep/win.wav similarity index 100% rename from Minesweep/win.wav rename to PyPortal/Minesweep/win.wav diff --git a/Twitter_API/code.py b/PyPortal/Twitter_API/code.py similarity index 78% rename from Twitter_API/code.py rename to PyPortal/Twitter_API/code.py index 06c3edafc..9dd30f519 100644 --- a/Twitter_API/code.py +++ b/PyPortal/Twitter_API/code.py @@ -17,16 +17,23 @@ """ #pylint:disable=invalid-name + +from os import getenv import time import board from adafruit_pyportal import PyPortal -try: - from secrets import secrets -except ImportError: - print("""WiFi settings are kept in secrets.py, please add them there! -the secrets dictionary must contain 'ssid' and 'password' at a minimum""") - 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." + ) # Set this to the username you'd like to display tweets from username = 'codewisdom' @@ -53,7 +60,7 @@ caption_color=0x808080) # Set OAuth2.0 Bearer Token -bearer_token = secrets['twitter_bearer_token'] +bearer_token = getenv('twitter_bearer_token') pyportal.set_headers({'Authorization': 'Bearer ' + bearer_token}) while True: diff --git a/Twitter_API/fonts/Helvetica-Bold-16.bdf b/PyPortal/Twitter_API/fonts/Helvetica-Bold-16.bdf similarity index 100% rename from Twitter_API/fonts/Helvetica-Bold-16.bdf rename to PyPortal/Twitter_API/fonts/Helvetica-Bold-16.bdf diff --git a/Twitter_API/twitter_background.bmp b/PyPortal/Twitter_API/twitter_background.bmp similarity index 100% rename from Twitter_API/twitter_background.bmp rename to PyPortal/Twitter_API/twitter_background.bmp diff --git a/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/AIO/code.py b/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/AIO/code.py index c69000306..0303e6c2f 100755 --- a/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/AIO/code.py +++ b/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/AIO/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import ssl import board @@ -22,26 +23,26 @@ NEO_CNT = 12 # neopixel count # ------------------------------------------------- +# 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") + # Set up NeoPixels pixels = neopixel.NeoPixel(NEO_PIN, NEO_CNT) -# 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 - # Setup AIO time query URL TIME_URL = "https://io.adafruit.com/api/v2/" -TIME_URL += secrets["aio_username"] +TIME_URL += aio_username TIME_URL += "/integrations/time/strftime?x-aio-key=" -TIME_URL += secrets["aio_key"] +TIME_URL += aio_key TIME_URL += "&fmt=%25Y%3A%25m%3A%25d%3A%25H%3A%25M%3A%25S" # Connect to local network try: - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) except ConnectionError: print("Wifi failed to connect.") while True: diff --git a/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/NTP/code.py b/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/NTP/code.py index 948ef1636..7e1ece5e8 100755 --- a/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/NTP/code.py +++ b/QT_Py/AT_Py_ESP32-S2_Sunrise_Lamp/NTP/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board import rtc @@ -25,16 +26,13 @@ # Set up NeoPixels pixels = neopixel.NeoPixel(NEO_PIN, NEO_CNT) -# 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") # Connect to local network try: - wifi.radio.connect(secrets["ssid"], secrets["password"]) + wifi.radio.connect(ssid, password) except ConnectionError: print("Wifi failed to connect.") while True: diff --git a/QT_Py/QT_Py_Cube/code.py b/QT_Py/QT_Py_Cube/code.py index e4d607439..3dbb97c7c 100644 --- a/QT_Py/QT_Py_Cube/code.py +++ b/QT_Py/QT_Py_Cube/code.py @@ -1,7 +1,8 @@ # SPDX-FileCopyrightText: 2022 Charlyn Gonda for Adafruit Industries # # SPDX-License-Identifier: MIT -from secrets import secrets + +from os import getenv import ssl import busio import board @@ -16,6 +17,21 @@ from cube import Cube +# 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." + ) + # Specify pins top_cin = board.A0 top_din = board.A1 @@ -42,9 +58,9 @@ connected = False while not connected: try: - wifi.radio.connect(secrets["ssid"], secrets["password"]) - print("Connected to %s!" % secrets["ssid"]) - print("My IP address is", wifi.radio.ipv4_address) + wifi.radio.connect(ssid, password) + print(f"Connected to {ssid}!") + print(f"My IP address is {wifi.radio.ipv4_address}") connected = True # pylint: disable=broad-except except Exception as error: @@ -54,8 +70,8 @@ # Setup for http requests pool = socketpool.SocketPool(wifi.radio) -REQUESTS = adafruit_requests.Session(pool, ssl.create_default_context()) -IO = IO_HTTP(secrets["aio_username"], secrets["aio_key"], REQUESTS) +requests = adafruit_requests.Session(pool, ssl.create_default_context()) +IO = IO_HTTP(aio_username, aio_key, requests) # Data for top pixels, will be updated by update_data() TOP_PIXELS_ON = [] diff --git a/Raspberry_Pi_Azure_IoT_Hub_Dashboard/featherTft_bme680/code.py b/Raspberry_Pi_Azure_IoT_Hub_Dashboard/featherTft_bme680/code.py index ceee73fa3..88592f92b 100644 --- a/Raspberry_Pi_Azure_IoT_Hub_Dashboard/featherTft_bme680/code.py +++ b/Raspberry_Pi_Azure_IoT_Hub_Dashboard/featherTft_bme680/code.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import json import time import digitalio @@ -17,18 +18,23 @@ from adafruit_display_text import bitmap_label, wrap_text_to_lines from adafruit_bitmap_font import bitmap_font from adafruit_azureiot import IoTHubDevice -import adafruit_bme680 from adafruit_lc709203f import LC709203F, PackSize +import adafruit_bme680 + +# 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." + ) print("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) print("Connected to WiFi!") @@ -47,7 +53,7 @@ esp = None pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, esp, getenv("device_connection_string")) print("Connecting to Azure IoT Hub...") diff --git a/Raspberry_Pi_Azure_IoT_Hub_Dashboard/qtPyEsp32S2_co2/code.py b/Raspberry_Pi_Azure_IoT_Hub_Dashboard/qtPyEsp32S2_co2/code.py index 01b98de0a..90f4bd9a8 100644 --- a/Raspberry_Pi_Azure_IoT_Hub_Dashboard/qtPyEsp32S2_co2/code.py +++ b/Raspberry_Pi_Azure_IoT_Hub_Dashboard/qtPyEsp32S2_co2/code.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import json import digitalio @@ -13,15 +14,20 @@ from adafruit_azureiot import IoTHubDevice import adafruit_scd4x -# 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("Connecting to WiFi...") -wifi.radio.connect(secrets["ssid"], secrets["password"]) +wifi.radio.connect(ssid, password) print("Connected to WiFi!") @@ -40,7 +46,7 @@ esp = None pool = socketpool.SocketPool(wifi.radio) # Create an IoT Hub device client and connect -device = IoTHubDevice(pool, esp, secrets["device_connection_string"]) +device = IoTHubDevice(pool, esp, getenv("device_connection_string")) print("Connecting to Azure IoT Hub...") diff --git a/Shadow_Box/code.py b/Shadow_Box/code.py index 72717a7c5..74d9cd6ff 100644 --- a/Shadow_Box/code.py +++ b/Shadow_Box/code.py @@ -5,12 +5,14 @@ """ Clock & sky colorbox for Adafruit MagTag: displays current time while NeoPixels provide theme lighting for the time of day. Requires WiFi -internet access -- configure credentials in secrets.py. An Adafruit IO +internet access -- configure credentials in settings.toml. An Adafruit IO user name and API key are also needed there, plus timezone and geographic coords. """ # pylint: disable=import-error + +from os import getenv import time import json import board @@ -18,12 +20,20 @@ from adafruit_magtag.magtag import MagTag import adafruit_fancyled.adafruit_fancyled as fancy -# UTC offset queries require some info from the secrets table... -try: - from secrets import secrets -except ImportError: - print('Please set up secrets.py with network credentials.') +# 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 SETTINGS ---------------------------------------------------- @@ -179,16 +189,13 @@ def blend(palette1, palette2, weight2, offset): # Since time is synced only once per hour, the extra request # isn't particularly burdensome. try: - RESPONSE = MAGTAG.network.requests.get( - 'https://io.adafruit.com/api/v2/%s/integrations/time/' - 'strftime?x-aio-key=%s&tz=%s' % (secrets.get('aio_username'), - secrets.get('aio_key'), - secrets.get('timezone')) + - '&fmt=%25z') - if RESPONSE.status_code == 200: + url = f"https://io.adafruit.com/api/v2/{aio_username}/integrations/time/strftime" + url += f'x-aio-key={aio_key}tz={getenv("timezone")}&fmt=%25z' + response = MAGTAG.network.requests.get(url) + if response.status_code == 200: # Arrives as sHHMM, convert to sHH:MM - print(RESPONSE.text) - UTC_OFFSET = RESPONSE.text[:3] + ':' + RESPONSE.text[-2:] + print(response.text) + UTC_OFFSET = response.text[:3] + ':' + response.text[-2:] except: # pylint: disable=bare-except # If query fails, prior value is kept until next query. # Only changes 2X a year anyway -- worst case, if these @@ -217,7 +224,7 @@ def blend(palette1, palette2, weight2, offset): try: URL = ('https://api.met.no/weatherapi/sunrise/2.0/.json?' 'lat=%s&lon=%s&date=%s-%s-%s&offset=%s' % - (secrets.get('latitude'), secrets.get('longitude'), + (getenv('latitude'), getenv('longitude'), str(NOW.tm_year), '{0:0>2}'.format(NOW.tm_mon), '{0:0>2}'.format(NOW.tm_mday), UTC_OFFSET)) print('Fetching sun data via', URL) diff --git a/Smart_Alarm_Clock/button/code.py b/Smart_Alarm_Clock/button/code.py index eff9eb3dd..228820cec 100644 --- a/Smart_Alarm_Clock/button/code.py +++ b/Smart_Alarm_Clock/button/code.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import ssl import time import board @@ -17,19 +18,28 @@ btn1.direction = digitalio.Direction.INPUT btn1.pull = digitalio.Pull.DOWN +# 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." + ) + + ALARM = None ### WiFi ### -# 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 - -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") # Define callback functions which will be called when certain events happen. # pylint: disable=unused-argument @@ -78,8 +88,8 @@ def on_alarm(client, feed_id, payload): # Initialize a new MQTT Client object 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/Smart_Alarm_Clock/code.py b/Smart_Alarm_Clock/code.py index c7fe68600..1c9fd89bb 100644 --- a/Smart_Alarm_Clock/code.py +++ b/Smart_Alarm_Clock/code.py @@ -2,6 +2,8 @@ # SPDX-License-Identifier: MIT # General imports + +from os import getenv import gc import time import math @@ -35,6 +37,22 @@ import audiocore import audiobusio +# 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." + ) + + TIMES = {} ENABLED = {} @@ -59,21 +77,9 @@ 6: "sunday", } -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - -# Set your Adafruit IO Username and Key in secrets.py -# (visit io.adafruit.com if you need to create an account, -# or if you need your Adafruit IO key.) -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] - -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") # Define callback functions which will be called when certain events happen. # pylint: disable=unused-argument @@ -97,7 +103,7 @@ def connected(client, userdata, flags, rc): "alarm", ] for feed in feeds: - feed_slug = secrets["aio_username"] + "/feeds/alarm-clock." + feed + feed_slug = f"{aio_username}/feeds/alarm-clock." + feed print("Subscribed to: alarm-clock." + feed) client.subscribe(feed_slug, 1) @@ -126,7 +132,7 @@ def fade(warm_val, cool_val): def get(feed_key): - mqtt_client.publish(f'{secrets["aio_username"]}/feeds/{feed_key}/get', "\0") + mqtt_client.publish(f'{aio_username}/feeds/{feed_key}/get', "\0") time.sleep(0.1) @@ -169,7 +175,7 @@ def on_iso(client, feed_id, payload): and WAIT < time.monotonic() ): mqtt_client.publish( - f"{secrets['aio_username']}/feeds/alarm-clock.alarm", "True" + f"{aio_username}/feeds/alarm-clock.alarm", "True" ) get("alarm-clock.alarm") gc.collect() @@ -314,8 +320,8 @@ def brighten_cool(i): # Initialize a new MQTT Client object 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(), ) @@ -331,51 +337,51 @@ def brighten_cool(i): mqtt_client.add_topic_callback("time/ISO-8601", on_iso) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.alarm", on_alarm + f"{aio_username}/feeds/alarm-clock.alarm", on_alarm ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.sunday", on_time + f"{aio_username}/feeds/alarm-clock.sunday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.monday", on_time + f"{aio_username}/feeds/alarm-clock.monday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.tuesday", on_time + f"{aio_username}/feeds/alarm-clock.tuesday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.wednesday", on_time + f"{aio_username}/feeds/alarm-clock.wednesday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.thursday", on_time + f"{aio_username}/feeds/alarm-clock.thursday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.friday", on_time + f"{aio_username}/feeds/alarm-clock.friday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.saturday", on_time + f"{aio_username}/feeds/alarm-clock.saturday", on_time ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.sunday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.sunday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.monday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.monday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.tuesday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.tuesday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.wednesday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.wednesday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.thursday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.thursday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.friday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.friday-enable", on_enable ) mqtt_client.add_topic_callback( - secrets["aio_username"] + "/feeds/alarm-clock.saturday-enable", on_enable + f"{aio_username}/feeds/alarm-clock.saturday-enable", on_enable ) @@ -398,7 +404,7 @@ def brighten_cool(i): while len(TIMES) < 7 or len(ENABLED) < 7: mqtt_client.loop() -mqtt_client.publish(secrets["aio_username"] + "/feeds/alarm-clock.alarm", "False") +mqtt_client.publish(f"{aio_username}/feeds/alarm-clock.alarm", "False") mqtt_client.subscribe("time/ISO-8601", 1) print("Starting") diff --git a/TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/code.py b/TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/code.py index 4b03a422a..deca670dd 100644 --- a/TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/code.py +++ b/TFT_Spirit_Board/esp32s3_s2_tft_featherwing_480x320/code.py @@ -8,7 +8,8 @@ Receive and display messages from the spirits. """ # pylint: disable=import-error, invalid-name -import os + +from os import getenv import displayio import fourwire import board @@ -22,6 +23,20 @@ from spirit_board import SpiritBoard +# 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." + ) # 3.5" TFT Featherwing is 480x320 @@ -49,12 +64,6 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) requests = adafruit_requests.Session(pool, ssl_context) - # Set your Adafruit IO Username and Key in secrets.py - # (visit io.adafruit.com if you need to create an account, - # or if you need your Adafruit IO key.) - aio_username = os.getenv("AIO_USERNAME") - aio_key = os.getenv("AIO_KEY") - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) except (RuntimeError, TypeError) as e: diff --git a/TFT_Spirit_Board/pyportal/code.py b/TFT_Spirit_Board/pyportal/code.py index 441a4733d..84cd1adf0 100644 --- a/TFT_Spirit_Board/pyportal/code.py +++ b/TFT_Spirit_Board/pyportal/code.py @@ -9,7 +9,7 @@ """ # pylint: disable=import-error, invalid-name -import os +from os import getenv import board from digitalio import DigitalInOut import adafruit_connection_manager @@ -22,6 +22,21 @@ display = board.DISPLAY +# 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." + ) + # Initialize the touch overlay touchscreen = adafruit_touchscreen.Touchscreen( board.TOUCH_XL, @@ -43,19 +58,13 @@ print("Connecting to AP...") try: - esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) + esp.connect_AP(ssid, password) # Initialize a requests session pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) - # Set your Adafruit IO Username and Key in secrets.py - # (visit io.adafruit.com if you need to create an account, - # or if you need your Adafruit IO key.) - aio_username = os.getenv("AIO_USERNAME") - aio_key = os.getenv("AIO_KEY") - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) except (RuntimeError, TypeError) as e: diff --git a/TFT_Spirit_Board/pyportal_titano/code.py b/TFT_Spirit_Board/pyportal_titano/code.py index 68b77d401..1329d330d 100644 --- a/TFT_Spirit_Board/pyportal_titano/code.py +++ b/TFT_Spirit_Board/pyportal_titano/code.py @@ -9,7 +9,7 @@ """ # pylint: disable=import-error, invalid-name -import os +from os import getenv import board from digitalio import DigitalInOut import adafruit_connection_manager @@ -22,6 +22,21 @@ display = board.DISPLAY +# 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." + ) + # Initialize the touch overlay touchscreen = adafruit_touchscreen.Touchscreen( board.TOUCH_XL, @@ -43,19 +58,13 @@ print("Connecting to AP...") try: - esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")) + esp.connect_AP(ssid, password) # Initialize a requests session pool = adafruit_connection_manager.get_radio_socketpool(esp) ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) requests = adafruit_requests.Session(pool, ssl_context) - # Set your Adafruit IO Username and Key in secrets.py - # (visit io.adafruit.com if you need to create an account, - # or if you need your Adafruit IO key.) - aio_username = os.getenv("AIO_USERNAME") - aio_key = os.getenv("AIO_KEY") - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) except (RuntimeError, TypeError) as e: diff --git a/Two_Way_Servo_Telegraph/code.py b/Two_Way_Servo_Telegraph/code.py index 92832e659..e2f1c5683 100644 --- a/Two_Way_Servo_Telegraph/code.py +++ b/Two_Way_Servo_Telegraph/code.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import ssl import board @@ -14,6 +15,21 @@ from simpleio import map_range from adafruit_motor import servo +# 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." + ) + # select which display is running the code servo_one = True # servo_two = True @@ -22,18 +38,9 @@ ANGLE_MIN = 0 ANGLE_MAX = 180 -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise -# connect to adafruitio -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] - -print("Connecting to %s" % secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!" % secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) diff --git a/Weather_Display_Matrix/.circuitpython.skip b/Weather_Display_Matrix/.circuitpython.skip deleted file mode 100644 index 29726fd3e..000000000 --- a/Weather_Display_Matrix/.circuitpython.skip +++ /dev/null @@ -1 +0,0 @@ -Weather_Display_Matrix/weather_display_matrix.py 71: Consider merging these comparisons with "in" to "UNITS in ('imperial', 'metric')" (consider-using-in) diff --git a/Weather_Display_Matrix/code.py b/Weather_Display_Matrix/code.py index ef9e3d70f..2a062f772 100644 --- a/Weather_Display_Matrix/code.py +++ b/Weather_Display_Matrix/code.py @@ -10,6 +10,8 @@ weather for your location... and display it on a screen! if you can find something that spits out JSON data, we can display it """ + +from os import getenv import time import board import microcontroller @@ -18,12 +20,17 @@ from adafruit_matrixportal.matrix import Matrix import openweather_graphics # pylint: disable=wrong-import-position -# 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 hasattr(board, "D12"): jumper = DigitalInOut(board.D12) @@ -62,10 +69,10 @@ DATA_SOURCE = ( "http://api.openweathermap.org/data/2.5/weather?q=" + LOCATION + "&units=" + UNITS ) -DATA_SOURCE += "&appid=" + secrets["openweather_token"] +DATA_SOURCE += "&appid=" + getenv("openweather_token") # You'll need to get a token from openweather.org, looks like 'b6907d289e10d714a6e88b30761fae22' -# it goes in your secrets.py file on a line such as: -# 'openweather_token' : 'your_big_humongous_gigantor_token', +# it goes in your settings.toml file on a line such as: +# 'openweather_token="your_big_humongous_gigantor_token"', DATA_LOCATION = [] SCROLL_HOLD_TIME = 0 # set this to hold each line before finishing scroll diff --git a/Webhook_Plant_Sensor/code.py b/Webhook_Plant_Sensor/code.py index f02dc95e9..ea25ee7ab 100644 --- a/Webhook_Plant_Sensor/code.py +++ b/Webhook_Plant_Sensor/code.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2021 Eva Herrada for Adafruit Industries # SPDX-License-Identifier: MIT +from os import getenv import time import board @@ -14,6 +15,21 @@ from adafruit_seesaw.seesaw import Seesaw import busio +# 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." + ) + # Used to make sure that the Adafruit IO Trigger is only run once when the moisture value is below # the desired threshold, set in MIN LOW = False @@ -28,13 +44,6 @@ # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller seesaw = Seesaw(i2c, addr=0x36) -# 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 - # Set up WiFi esp32_cs = DigitalInOut(board.D13) esp32_ready = DigitalInOut(board.D11) @@ -42,8 +51,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) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Define callback functions which will be called when certain events happen. # pylint: disable=unused-argument @@ -73,8 +82,8 @@ def message(client, feed_id, payload): # Initialize a new MQTT Client object 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_context, ) diff --git a/WiFi_Mailbox_Notifier/mailbox_code/code.py b/WiFi_Mailbox_Notifier/mailbox_code/code.py index c51e98cb7..f51ffafb1 100644 --- a/WiFi_Mailbox_Notifier/mailbox_code/code.py +++ b/WiFi_Mailbox_Notifier/mailbox_code/code.py @@ -4,6 +4,7 @@ """CircuitPython WiFi Mailbox Notifier""" +from os import getenv import time import ssl import alarm @@ -17,12 +18,20 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP -# Get WiFi/Adafruit IO details from secrets.py -try: - from secrets import secrets -except ImportError: - print("Please create secrets.py and add your WiFi and AIO credentials 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." + ) # Update to True if you want metadata sent to Adafruit IO. Defaults to False. METADATA = False @@ -81,9 +90,9 @@ def send_io_data(feed_name, value): # Connect to WiFi try: - wifi.radio.connect(secrets["ssid"], secrets["password"]) - print("Connected to {}!".format(secrets["ssid"])) - print("IP:", wifi.radio.ipv4_address) + wifi.radio.connect(ssid, password) + print(f"Connected to {ssid}!") + print(f"IP: {wifi.radio.ipv4_address}") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) @@ -94,9 +103,6 @@ def send_io_data(feed_name, value): time.sleep(15) supervisor.reload() -# Pull your Adafruit IO username and key from secrets.py -aio_username = secrets["aio_username"] -aio_key = secrets["aio_key"] # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/Wireless_ESP32-S2_Controller_For_Pure_Data/code.py b/Wireless_ESP32-S2_Controller_For_Pure_Data/code.py index 0edc406fa..87a91f2dd 100644 --- a/Wireless_ESP32-S2_Controller_For_Pure_Data/code.py +++ b/Wireless_ESP32-S2_Controller_For_Pure_Data/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import ipaddress import wifi import socketpool @@ -10,12 +11,17 @@ import adafruit_tsc2007 import adafruit_adxl34x -# Get wifi details and host IP 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." + ) # I2C setup for STEMMA port i2c = board.STEMMA_I2C() @@ -56,16 +62,16 @@ new_val = False # URLs to fetch from -HOST = secrets["host"] +HOST = getenv("host") PORT = 12345 TIMEOUT = 5 INTERVAL = 5 MAXBUF = 256 # connect to WIFI -print("Connecting to %s"%secrets["ssid"]) -wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!"%secrets["ssid"]) +print(f"Connecting to {ssid}") +wifi.radio.connect(ssid, password) +print(f"Connected to {ssid}!") pool = socketpool.SocketPool(wifi.radio)