diff --git a/Feather_ESP32-S2_TFT_Azure/code.py b/Feather_ESP32-S2_TFT_Azure/code.py index 64c3f1135..2571d3d10 100644 --- a/Feather_ESP32-S2_TFT_Azure/code.py +++ b/Feather_ESP32-S2_TFT_Azure/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 supervisor @@ -17,20 +18,24 @@ from adafruit_display_text import bitmap_label, wrap_text_to_lines from adafruit_bitmap_font import bitmap_font from adafruit_azureiot import IoTCentralDevice -import adafruit_bme680 import adafruit_max1704x +import adafruit_bme680 #from adafruit_lc709203f import LC709203F, PackSize +# 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!") @@ -75,7 +80,7 @@ # # Next create a device using the device template, and select Connect to get the # device connection details. -# Add the connection details to your secrets.py file, using the following values: +# Add the connection details to your settings.toml file, using the following values: # # 'id_scope' - the devices ID scope # 'device_id' - the devices device id @@ -100,7 +105,7 @@ esp = None pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, ssl_context, secrets["id_scope"], secrets["device_id"], secrets["device_primary_key"] + pool, ssl_context, getenv("id_scope"), getenv("device_id"), getenv("device_primary_key") ) print("Connecting to Azure IoT Central...") diff --git a/Feather_Freezer_Alarm/code.py b/Feather_Freezer_Alarm/code.py index 013fbceb7..733999665 100644 --- a/Feather_Freezer_Alarm/code.py +++ b/Feather_Freezer_Alarm/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import ssl import microcontroller @@ -38,23 +39,26 @@ pir.direction = digitalio.Direction.INPUT motion = Debouncer(pir) -try: - from secrets import secrets -except ImportError: - print("WiFi and Adafruit IO credentials 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") -# Add your Adafruit IO Username and Key to secrets.py -# (visit io.adafruit.com if you need to create an account, -# or if you need to obtain your Adafruit IO key.) -aio_username = secrets["aio_username"] -aio_key = secrets["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." + ) # WiFi try: - 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}!") # Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad. except Exception as e: # pylint: disable=broad-except print("Failed to connect to WiFi. Error:", e, "\nBoard will restart in 5 seconds.") @@ -67,8 +71,8 @@ # 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/FunHouse_IOT_Hub/battery_peripheral/code.py b/FunHouse_IOT_Hub/battery_peripheral/code.py index 7bd2ff758..6f633493d 100644 --- a/FunHouse_IOT_Hub/battery_peripheral/code.py +++ b/FunHouse_IOT_Hub/battery_peripheral/code.py @@ -12,6 +12,7 @@ Either the Feather or the Airlift Featherwing should have stacking headers for the display. """ +from os import getenv import time import board from adafruit_lc709203f import LC709203F @@ -30,18 +31,25 @@ from adafruit_display_text import label import adafruit_displayio_ssd1306 +# 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." + ) + displayio.release_displays() ### 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 - - # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.D13) esp32_ready = DigitalInOut(board.D11) @@ -50,10 +58,10 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +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 @@ -81,8 +89,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/FunHouse_IOT_Hub/door_peripheral/code.py b/FunHouse_IOT_Hub/door_peripheral/code.py index ae8854f95..e11c2e9a8 100644 --- a/FunHouse_IOT_Hub/door_peripheral/code.py +++ b/FunHouse_IOT_Hub/door_peripheral/code.py @@ -14,6 +14,7 @@ either the Feather or the AirLift Featherwing has stacking headers. """ +from os import getenv import board import busio import adafruit_connection_manager @@ -25,14 +26,22 @@ from digitalio import DigitalInOut, Direction, Pull from adafruit_debouncer import Debouncer -### WiFi ### +# 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." + ) -# 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 +### WiFi ### switch_pin = DigitalInOut(board.D10) switch_pin.direction = Direction.INPUT @@ -47,10 +56,10 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Connect to WiFi print("Connecting to WiFi...") @@ -63,8 +72,8 @@ # 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/FunHouse_IOT_Hub/iot_hub/code.py b/FunHouse_IOT_Hub/iot_hub/code.py index c09c11844..a13efe80b 100644 --- a/FunHouse_IOT_Hub/iot_hub/code.py +++ b/FunHouse_IOT_Hub/iot_hub/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 ssl import displayio @@ -15,6 +16,21 @@ from adafruit_io.adafruit_io import IO_MQTT from adafruit_dash_display import Hub +# 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." + ) + # Set up navigation buttons up = DigitalInOut(board.BUTTON_UP) up.direction = Direction.INPUT @@ -31,14 +47,6 @@ back = touchio.TouchIn(board.CAP7) submit = touchio.TouchIn(board.CAP8) -# Check for secrets.py. Note: for this project, your secrets.py needs an adafruit io api key as -# well as the wifi information -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise - # Make the rgb group for setting rgb hex values for NeoPixels rgb_group = displayio.Group() R_label = Label( @@ -174,15 +182,9 @@ def pub_lamp(lamp): display = board.DISPLAY -# 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}!") # Create a socket pool pool = socketpool.SocketPool(wifi.radio) @@ -190,8 +192,8 @@ def pub_lamp(lamp): # 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/FunHouse_IOT_Hub/neopixel_remote/code.py b/FunHouse_IOT_Hub/neopixel_remote/code.py index 685a7bdae..7e30ed84b 100644 --- a/FunHouse_IOT_Hub/neopixel_remote/code.py +++ b/FunHouse_IOT_Hub/neopixel_remote/code.py @@ -7,6 +7,7 @@ Colors used are taken from Adafruit_CircuitPython_LED_Animation library """ +from os import getenv import time import math import board @@ -26,6 +27,21 @@ # Import Adafruit IO HTTP Client from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError +# 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." + ) + ts = adafruit_touchscreen.Touchscreen( board.TOUCH_XL, board.TOUCH_XR, @@ -92,30 +108,17 @@ group.append(rect) print(len(group)) -# 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 - # PyPortal ESP32 Setup esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) esp32_reset = DigitalInOut(board.ESP_RESET) 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) - -# 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.) -ADAFRUIT_IO_USER = secrets["aio_username"] -ADAFRUIT_IO_KEY = secrets["aio_key"] +status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Create an instance of the Adafruit IO HTTP client -io = IO_HTTP(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) +io = IO_HTTP(aio_username, aio_key, wifi) try: # Get the 'temperature' feed from Adafruit IO diff --git a/FunHouse_IOT_Hub/neopixel_remote/feather_code/code.py b/FunHouse_IOT_Hub/neopixel_remote/feather_code/code.py index 30f3d960a..76fd255a6 100644 --- a/FunHouse_IOT_Hub/neopixel_remote/feather_code/code.py +++ b/FunHouse_IOT_Hub/neopixel_remote/feather_code/code.py @@ -15,6 +15,7 @@ But if it doesn't, use stacking headers on either the Feather or the AirLift FeatherWing """ +from os import getenv import board import busio import adafruit_connection_manager @@ -25,14 +26,22 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut -### WiFi ### +# 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." + ) -# 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 +### WiFi ### # Change the first number to the pin the data line is plugged in to and the second number # to the number of pixels @@ -46,10 +55,10 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +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 @@ -82,8 +91,8 @@ def on_neopixel(client, topic, message): # 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/FunHouse_IOT_Hub/relay_heatindex_peripheral/code.py b/FunHouse_IOT_Hub/relay_heatindex_peripheral/code.py index ca4882707..8f58e09b5 100644 --- a/FunHouse_IOT_Hub/relay_heatindex_peripheral/code.py +++ b/FunHouse_IOT_Hub/relay_heatindex_peripheral/code.py @@ -17,6 +17,7 @@ a box fan connected to it. """ +from os import getenv import board import busio import adafruit_connection_manager @@ -27,14 +28,22 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut, Direction -### WiFi ### +# 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." + ) -# 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 +### WiFi ### RELAY = DigitalInOut(board.D10) RELAY.direction = Direction.OUTPUT @@ -47,10 +56,10 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +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 @@ -81,8 +90,8 @@ def on_hi(client, topic, message): # 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/FunHouse_IOT_Hub/relay_peripheral/code.py b/FunHouse_IOT_Hub/relay_peripheral/code.py index 0f2dbfae4..cdb8f0d22 100644 --- a/FunHouse_IOT_Hub/relay_peripheral/code.py +++ b/FunHouse_IOT_Hub/relay_peripheral/code.py @@ -12,6 +12,8 @@ OR * https://www.adafruit.com/product/2935 """ + +from os import getenv import board import busio import adafruit_connection_manager @@ -22,14 +24,22 @@ from adafruit_io.adafruit_io import IO_MQTT from digitalio import DigitalInOut, Direction -### WiFi ### +# 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." + ) -# 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 +### WiFi ### # If you are using a Relay FeatherWing, make sure this pin corresponds to the pin shorted on the # bottom of the Relay FeatherWing @@ -44,10 +54,10 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) """Use below for Most Boards""" -status_light = neopixel.NeoPixel( +status_pixel = neopixel.NeoPixel( board.NEOPIXEL, 1, brightness=0.2 ) # Uncomment for Most Boards -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +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 @@ -75,8 +85,8 @@ def on_lamp(client, topic, message): # 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/FunHouse_IOT_Hub/temp_humid_peripheral/code.py b/FunHouse_IOT_Hub/temp_humid_peripheral/code.py index b744a0faa..fa80c67ea 100644 --- a/FunHouse_IOT_Hub/temp_humid_peripheral/code.py +++ b/FunHouse_IOT_Hub/temp_humid_peripheral/code.py @@ -14,6 +14,7 @@ * https://www.adafruit.com/product/4399 """ +from os import getenv import ssl import socketpool import wifi @@ -25,33 +26,30 @@ import board import adafruit_sht4x -### WiFi ### +# 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." + ) -# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and -# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other -# source control. -# pylint: disable=no-name-in-module,wrong-import-order +### WiFi ### magtag = MagTag() -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"] - - connected = False try: - 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}!") # Create a socket pool pool = socketpool.SocketPool(wifi.radio) @@ -59,8 +57,8 @@ # 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/FunHouse_LIFX/.circuitpython.skip b/FunHouse_LIFX/.circuitpython.skip deleted file mode 100644 index 69a38db93..000000000 --- a/FunHouse_LIFX/.circuitpython.skip +++ /dev/null @@ -1,2 +0,0 @@ -FunHouse_LIFX/code.py 117: Comparison to literal (literal-comparison) -FunHouse_LIFX/code.py 136: Comparison to literal (literal-comparison) diff --git a/FunHouse_LIFX/code.py b/FunHouse_LIFX/code.py index 2cb211075..11ce23d26 100644 --- a/FunHouse_LIFX/code.py +++ b/FunHouse_LIFX/code.py @@ -1,21 +1,28 @@ # SPDX-FileCopyrightText: 2021 John Park for Adafruit Industries # SPDX-License-Identifier: MIT # FunHouse PIR Motion Sensor for LIFX light bulbs + +from os import getenv import time import ssl import socketpool import wifi import adafruit_requests -from adafruit_funhouse import FunHouse from displayio import CIRCUITPYTHON_TERMINAL import adafruit_lifx +from adafruit_funhouse import FunHouse + +# 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 and API 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." + ) # choose colors here. Note formatting differences. default_bulb_color = "#002010" @@ -24,13 +31,13 @@ tripped_led_color = 0x440044 # Set up ESP32-S2 and adafruit_requests session -wifi.radio.connect(ssid=secrets["ssid"], password=secrets["password"]) +wifi.radio.connect(ssid, password) pool = socketpool.SocketPool(wifi.radio) http_session = adafruit_requests.Session(pool, ssl.create_default_context()) -# Add your LIFX Personal Access token to secrets.py +# Add your LIFX Personal Access token to settings.toml # (to obtain a token, visit: https://cloud.lifx.com/settings) -lifx_token = secrets["lifx_token"] +lifx_token = getenv("lifx_token") # Set this to your LIFX light separator label # https://api.developer.lifx.com/docs/selectors @@ -115,7 +122,7 @@ def set_label_color(conditional, index, on_color): time.sleep(0.5) # when sensor is tripped, set the color x amount of time - if running_state is True and funhouse.peripherals.pir_sensor and pir_state is 0: + if running_state is True and funhouse.peripherals.pir_sensor and pir_state == 0: funhouse.peripherals.dotstars.fill(tripped_led_color) funhouse.set_text("tripped", running_label) lifx.set_color( @@ -134,7 +141,7 @@ def set_label_color(conditional, index, on_color): # return to default color elif ( - running_state is True and not funhouse.peripherals.pir_sensor and pir_state is 1 + running_state is True and not funhouse.peripherals.pir_sensor and pir_state == 1 ): funhouse.peripherals.dotstars.fill(default_led_color) funhouse.set_text("sensing...", running_label) diff --git a/IoT_Environment_Sensor/.circuitpython.skip b/IoT_Environment_Sensor/.circuitpython.skip deleted file mode 100644 index 117ae2f31..000000000 --- a/IoT_Environment_Sensor/.circuitpython.skip +++ /dev/null @@ -1,5 +0,0 @@ -IoT_Environment_Sensor/gps.py 22: Class 'Gps' inherits from object, can be safely removed from bases in python3 (useless-object-inheritance) -IoT_Environment_Sensor/air_quality.py 24: Class 'AirQualitySensor' inherits from object, can be safely removed from bases in python3 (useless-object-inheritance) -IoT_Environment_Sensor/aio.py 41: Class 'AIO' inherits from object, can be safely removed from bases in python3 (useless-object-inheritance) -IoT_Environment_Sensor/aio.py 120: Consider explicitly re-raising using the 'from' keyword (raise-missing-from) -IoT_Environment_Sensor/aio.py 142: Consider explicitly re-raising using the 'from' keyword (raise-missing-from) diff --git a/IoT_Environment_Sensor/aio.py b/IoT_Environment_Sensor/aio.py index fd4e74519..b3b178c3a 100644 --- a/IoT_Environment_Sensor/aio.py +++ b/IoT_Environment_Sensor/aio.py @@ -16,6 +16,7 @@ All text above must be included in any redistribution. """ +from os import getenv import time import gc import board @@ -37,14 +38,22 @@ -# Get wifi details and more from a settings.py file -try: - from secrets import secrets -except ImportError: - logger.critical('WiFi settings are kept in settings.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") -class AIO(object): +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." + ) + +class AIO: def __init__(self): try: @@ -76,16 +85,16 @@ def connect(self): logger.debug("Connecting...") while not self._esp.is_connected: try: - self._esp.connect_AP(secrets['ssid'], secrets['password']) + self._esp.connect_AP(ssid, password) except RuntimeError as e: logger.error("could not connect to AP, retrying: %s", e) continue def post(self, feed, payload): - api_url = 'https://io.adafruit.com/api/v2/{0}/feeds/{1}/data'.format(secrets['aio_username'], feed)# pylint: disable=line-too-long + api_url = f'https://io.adafruit.com/api/v2/{aio_username}/feeds/{feed}/data' logger.info('POSTing to %s', api_url) logger.info('payload: %s', str(payload)) - auth_header = {'X-AIO-KEY':secrets['aio_key']} + auth_header = {'X-AIO-KEY': aio_key} self.connect() r = None tries = 0 @@ -119,13 +128,8 @@ def refresh_local_time(self): """ # pylint: enable=line-too-long api_url = None - try: - aio_username = secrets['aio_username'] - aio_key = secrets['aio_key'] - except KeyError: - raise KeyError("\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long - location = secrets['timezone'] + location = getenv('timezone') if location: logger.debug('Getting time for timezone %s', location) api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location) @@ -144,8 +148,8 @@ def refresh_local_time(self): year_day = int(times[2]) week_day = int(times[3]) is_dst = None # no way to know yet - except KeyError: - raise KeyError("Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long + except KeyError as exc: + raise KeyError("Was unable to lookup the time, try setting timezone in your settings.toml according to http://worldtimeapi.org/timezones") from exc # pylint: disable=line-too-long year, month, mday = [int(x) for x in the_date.split('-')] the_time = the_time.split('.')[0] hours, minutes, seconds = [int(x) for x in the_time.split(':')] diff --git a/IoT_Environment_Sensor/air_quality.py b/IoT_Environment_Sensor/air_quality.py index bb392cc2e..a2fff82fa 100644 --- a/IoT_Environment_Sensor/air_quality.py +++ b/IoT_Environment_Sensor/air_quality.py @@ -27,7 +27,7 @@ if not logger.hasHandlers(): logger.addHandler(logging.StreamHandler()) -class AirQualitySensor (object): +class AirQualitySensor: def __init__(self, uart): self._uart = uart diff --git a/IoT_Environment_Sensor/code.py b/IoT_Environment_Sensor/code.py index 5ff2970cf..2ffe9397f 100644 --- a/IoT_Environment_Sensor/code.py +++ b/IoT_Environment_Sensor/code.py @@ -21,9 +21,9 @@ import busio import air_quality import gps -import adafruit_bme280 import aio import adafruit_logging as logging +import adafruit_bme280 logger = logging.getLogger('main') if not logger.hasHandlers(): diff --git a/IoT_Environment_Sensor/gps.py b/IoT_Environment_Sensor/gps.py index 9839350c1..5b8014422 100644 --- a/IoT_Environment_Sensor/gps.py +++ b/IoT_Environment_Sensor/gps.py @@ -25,7 +25,7 @@ if not logger.hasHandlers(): logger.addHandler(logging.StreamHandler()) -class Gps(object): +class Gps: def __init__(self, uart): self._gps = adafruit_gps.GPS(uart, debug=False) diff --git a/IoT_Environment_Sensor/secrets.py b/IoT_Environment_Sensor/secrets.py deleted file mode 100644 index f55951b18..000000000 --- a/IoT_Environment_Sensor/secrets.py +++ /dev/null @@ -1,14 +0,0 @@ -# SPDX-FileCopyrightText: 2019 Dave Astels 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": b"My_SSID", - "password": b"My_WIFI_Password", - "timezone": "Area/City", - "aio_username": "my_username", - "aio_key": "my_key", -} diff --git a/IoT_Environment_Sensor/settings.toml b/IoT_Environment_Sensor/settings.toml new file mode 100644 index 000000000..c6b66bd33 --- /dev/null +++ b/IoT_Environment_Sensor/settings.toml @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2019 Dave Astels 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" +ADAFRUIT_AIO_USERNAME="my_username" +ADAFRUIT_AIO_KEY="my_key" +timezone="America/New_York" # http://worldtimeapi.org/timezones diff --git a/IoT_NeoPixel_Sign/.circuitpython.skip b/IoT_NeoPixel_Sign/.circuitpython.skip deleted file mode 100644 index e6f96e785..000000000 --- a/IoT_NeoPixel_Sign/.circuitpython.skip +++ /dev/null @@ -1 +0,0 @@ -IoT_NeoPixel_Sign/code.py 47: Using the global statement (global-statement) diff --git a/IoT_NeoPixel_Sign/code.py b/IoT_NeoPixel_Sign/code.py index 3c0130464..2e35fbbcd 100644 --- a/IoT_NeoPixel_Sign/code.py +++ b/IoT_NeoPixel_Sign/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import ssl import board import neopixel @@ -12,20 +13,26 @@ from adafruit_pixel_framebuf import PixelFramebuffer # adafruit_circuitpython_adafruitio usage with native wifi networking +# 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." + ) + # Neopixel matrix configuration PIXEL_PIN = board.IO6 PIXEL_WIDTH = 12 PIXEL_HEIGHT = 12 -# secrets.py has SSID/password and adafruit.io -try: - from secrets import secrets -except ImportError: - print("WiFi secrets are kept in secrets.py, please add them there!") - raise -AIO_USERNAME = secrets["aio_username"] -AIO_KEY = secrets["aio_key"] - # LED matrix creation PIXELS = neopixel.NeoPixel( PIXEL_PIN, PIXEL_WIDTH * PIXEL_HEIGHT, brightness=0.5, auto_write=False, @@ -48,7 +55,7 @@ # Helper function to get updated data from Adafruit.io def update_data(): - global CURRENT_TEXT, CURRENT_COLOR + global CURRENT_TEXT, CURRENT_COLOR # pylint: disable=global-statement print("Updating data from Adafruit IO") try: quote_feed = IO.get_feed(QUOTE_FEED) @@ -63,15 +70,15 @@ def update_data(): # 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}!") # Setup Adafruit IO connection -POOL = socketpool.SocketPool(wifi.radio) -REQUESTS = adafruit_requests.Session(POOL, ssl.create_default_context()) +pool = socketpool.SocketPool(wifi.radio) +requests = adafruit_requests.Session(pool, ssl.create_default_context()) # Initialize an Adafruit IO HTTP API object -IO = IO_HTTP(AIO_USERNAME, AIO_KEY, REQUESTS) +IO = IO_HTTP(aio_username, aio_key, requests) while True: diff --git a/IoT_Party_Parrot/code.py b/IoT_Party_Parrot/code.py index a4486ee23..55b24a76e 100644 --- a/IoT_Party_Parrot/code.py +++ b/IoT_Party_Parrot/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board import displayio @@ -9,17 +10,22 @@ from adafruit_matrixportal.matrix import Matrix import adafruit_imageload -# 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("Party Parrot Twitter Matrix") +# 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." + ) + # import your bearer token -bear = secrets['bearer_token'] +bear = getenv('bearer_token') # query URL for tweets. looking for hashtag partyparrot sent to a specific username DATA_SOURCE = 'https://api.twitter.com/2/tweets/search/recent?query=#partyparrot to:blitzcitydiy' diff --git a/IoT_Party_Parrot/secrets.py b/IoT_Party_Parrot/secrets.py deleted file mode 100644 index 39bdad947..000000000 --- a/IoT_Party_Parrot/secrets.py +++ /dev/null @@ -1,15 +0,0 @@ -# SPDX-FileCopyrightText: 2020 Liz Clark 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' : 'insert your network name here', - 'password' : 'insert your network password here', - 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones - 'twitter_api_key' : 'insert your twitter api key here', - 'twitter_secret_key' : 'insert your twitter secret key here', - 'bearer_token' : 'insert your bearer token here' - } diff --git a/IoT_Party_Parrot/settings.toml b/IoT_Party_Parrot/settings.toml new file mode 100644 index 000000000..4b4d629e3 --- /dev/null +++ b/IoT_Party_Parrot/settings.toml @@ -0,0 +1,13 @@ +# SPDX-FileCopyrightText: 2020 Liz Clark 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 +twitter_api_key="insert your twitter api key here" +twitter_secret_key="insert your twitter secret key here" +bearer_token="insert your bearer token here"