diff --git a/Adafruit_Feather_ESP32-S2/BME280_LC709203_Adafruit_IO/code.py b/Adafruit_Feather_ESP32-S2/BME280_LC709203_Adafruit_IO/code.py index b98723522..f1abce54c 100644 --- a/Adafruit_Feather_ESP32-S2/BME280_LC709203_Adafruit_IO/code.py +++ b/Adafruit_Feather_ESP32-S2/BME280_LC709203_Adafruit_IO/code.py @@ -3,6 +3,8 @@ """ CircuitPython Adafruit IO Example for BME280 and LC709203 Sensors """ + +from os import getenv import time import ssl import alarm @@ -14,11 +16,21 @@ from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError from adafruit_lc709203f import LC709203F, PackSize from adafruit_bme280 import basic as adafruit_bme280 -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") + +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." + ) # Duration of sleep in seconds. Default is 600 seconds (10 minutes). # Feather will sleep for this duration between sensor readings / sending data to AdafruitIO @@ -78,9 +90,9 @@ def send_io_data(feed, value): # Wi-Fi connections can have issues! This ensures the code will continue to run. try: # Connect to Wi-Fi - 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()) @@ -90,12 +102,6 @@ def send_io_data(feed, value): print(e) go_to_sleep(60) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/Adafruit_Feather_ESP32-S2/BME280_LC709203_Simple_Data/code.py b/Adafruit_Feather_ESP32-S2/BME280_LC709203_Simple_Data/code.py index 5b8e69d73..3c46f3c2d 100644 --- a/Adafruit_Feather_ESP32-S2/BME280_LC709203_Simple_Data/code.py +++ b/Adafruit_Feather_ESP32-S2/BME280_LC709203_Simple_Data/code.py @@ -5,8 +5,8 @@ """ import time import board -from adafruit_bme280 import basic as adafruit_bme280 from adafruit_lc709203f import LC709203F, PackSize +from adafruit_bme280 import basic as adafruit_bme280 # Create sensor objects, using the board's default I2C bus. i2c = board.I2C() # uses board.SCL and board.SDA diff --git a/Adafruit_Feather_TFT_ESP32-S2/LC709203_Adafruit_IO/code.py b/Adafruit_Feather_TFT_ESP32-S2/LC709203_Adafruit_IO/code.py index 1a3add4b1..c3cb3537d 100644 --- a/Adafruit_Feather_TFT_ESP32-S2/LC709203_Adafruit_IO/code.py +++ b/Adafruit_Feather_TFT_ESP32-S2/LC709203_Adafruit_IO/code.py @@ -3,6 +3,8 @@ """ CircuitPython Adafruit IO Example for LC709203 Sensor """ + +from os import getenv import time import ssl import alarm @@ -13,11 +15,21 @@ import adafruit_requests from adafruit_io.adafruit_io import IO_HTTP from adafruit_lc709203f import LC709203F, PackSize -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") + +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." + ) # Duration of sleep in seconds. Default is 600 seconds (10 minutes). # Feather will sleep for this duration between sensor readings / sending data to AdafruitIO @@ -58,9 +70,9 @@ def send_io_data(feed, value): # Wi-Fi connections can have issues! This ensures the code will continue to run. try: # Connect to Wi-Fi - 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()) @@ -70,12 +82,6 @@ def send_io_data(feed, value): print(e) go_to_sleep(60) -# 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"] - # Initialize an Adafruit IO HTTP API object io = IO_HTTP(aio_username, aio_key, requests) diff --git a/Adafruit_Feather_TFT_ESP32-S2/TFT_GitHub_Stars/code.py b/Adafruit_Feather_TFT_ESP32-S2/TFT_GitHub_Stars/code.py index 9e879ad02..b0122b99e 100644 --- a/Adafruit_Feather_TFT_ESP32-S2/TFT_GitHub_Stars/code.py +++ b/Adafruit_Feather_TFT_ESP32-S2/TFT_GitHub_Stars/code.py @@ -3,6 +3,8 @@ """ CircuitPython GitHub Stars viewer """ + +from os import getenv import time import ssl import wifi @@ -13,12 +15,17 @@ from adafruit_bitmap_font import bitmap_font import adafruit_requests -# Get WiFi details 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." + ) display = board.DISPLAY @@ -38,10 +45,10 @@ display.root_group = group # Connect to WiFi -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}") pool = socketpool.SocketPool(wifi.radio) requests = adafruit_requests.Session(pool, ssl.create_default_context()) diff --git a/Adafruit_IO_Air_Quality/code.py b/Adafruit_IO_Air_Quality/code.py index 734b97695..cc2b74441 100755 --- a/Adafruit_IO_Air_Quality/code.py +++ b/Adafruit_IO_Air_Quality/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -22,14 +23,22 @@ # Interval the sensor publishes to Adafruit IO, in minutes PUBLISH_INTERVAL = 10 -### 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 ### # AirLift FeatherWing esp32_cs = DigitalInOut(board.D13) @@ -38,8 +47,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) # Connect to a PM2.5 sensor over UART reset_pin = None @@ -142,7 +151,7 @@ def read_bme(is_celsius=False): # Create an instance of the Adafruit IO HTTP client -io = IO_HTTP(secrets["aio_user"], secrets["aio_key"], wifi) +io = IO_HTTP(aio_username, aio_key, wifi) # Describes feeds used to hold Adafruit IO data feed_aqi = io.get_feed("air-quality-sensor.aqi") @@ -150,11 +159,11 @@ def read_bme(is_celsius=False): feed_humidity = io.get_feed("air-quality-sensor.humidity") feed_temperature = io.get_feed("air-quality-sensor.temperature") -# Set up location metadata from secrets.py file +# Set up location metadata from settings.toml file location_metadata = { - "lat": secrets["latitude"], - "lon": secrets["longitude"], - "ele": secrets["elevation"], + "lat": getenv("latitude"), + "lon": getenv("longitude"), + "ele": getenv("elevation"), } elapsed_minutes = 0 diff --git a/Adafruit_IO_Power_Relay/code.py b/Adafruit_IO_Power_Relay/code.py index 2361eb69f..1ccc118eb 100755 --- a/Adafruit_IO_Power_Relay/code.py +++ b/Adafruit_IO_Power_Relay/code.py @@ -14,12 +14,19 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT -### WiFi ### +# Get WiFi details, ensure these are setup in settings.toml +ssid = os.getenv("CIRCUITPY_WIFI_SSID") +password = os.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." + ) -secrets = { - "ssid" : os.getenv("CIRCUITPY_WIFI_SSID"), - "password" : os.getenv("CIRCUITPY_WIFI_PASSWORD"), -} +### WiFi ### # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -34,19 +41,19 @@ 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 """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Set up a pin for controlling the relay power_pin = DigitalInOut(board.D3) diff --git a/Adafruit_IO_Power_Relay/code_light_sensor/code.py b/Adafruit_IO_Power_Relay/code_light_sensor/code.py index f37f9c727..bd33d75b1 100755 --- a/Adafruit_IO_Power_Relay/code_light_sensor/code.py +++ b/Adafruit_IO_Power_Relay/code_light_sensor/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -14,6 +15,21 @@ 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." + ) + ### Sensor Calibration ### # Appliance power LED's light level, in Lux APPLIANCE_ON_LUX = 30.0 @@ -22,13 +38,6 @@ ### 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.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -42,19 +51,19 @@ 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 """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Set up a pin for controlling the relay power_pin = DigitalInOut(board.D3) @@ -67,10 +76,10 @@ ### Feeds ### # Set up a feed named Relay for subscribing to the relay feed on Adafruit IO -feed_relay = secrets["aio_username"] + "/feeds/relay" +feed_relay = f"{aio_username}/feeds/relay" # Set up a feed named status for subscribing to the status feed on Adafruit IO -feed_status = secrets["aio_username"] + "/feeds/status" +feed_status = f"{aio_username}/feeds/status" ### Code ### @@ -122,8 +131,8 @@ def on_relay_msg(client, topic, value): # Set up a MiniMQTT Client 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/Adafruit_IO_Schedule_Trigger/code.py b/Adafruit_IO_Schedule_Trigger/code.py index a46feb49f..339a0f324 100755 --- a/Adafruit_IO_Schedule_Trigger/code.py +++ b/Adafruit_IO_Schedule_Trigger/code.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT +from os import getenv import time import board import busio @@ -13,14 +14,22 @@ import adafruit_minimqtt.adafruit_minimqtt as MQTT from adafruit_io.adafruit_io import IO_MQTT -### 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 board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -35,19 +44,19 @@ 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 """Uncomment below for ItsyBitsy M4""" -# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) +# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) # Uncomment below for an externally defined RGB LED # import adafruit_rgbled # from adafruit_esp32spi import PWMOut # RED_LED = PWMOut.PWMOut(esp, 26) # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) -# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) +wifi = adafruit_esp32spi_wifimanager.WiFiManager(esp, ssid, password, status_pixel=status_pixel) # Set up a pin for controlling the relay power_pin = DigitalInOut(board.D3) @@ -110,8 +119,8 @@ def on_relay_msg(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/Arduino_Nano_RP2040_Connect/arduino_nano_rp2040_connect_wifi/code.py b/Arduino_Nano_RP2040_Connect/arduino_nano_rp2040_connect_wifi/code.py index 06d799abb..63d7865e9 100644 --- a/Arduino_Nano_RP2040_Connect/arduino_nano_rp2040_connect_wifi/code.py +++ b/Arduino_Nano_RP2040_Connect/arduino_nano_rp2040_connect_wifi/code.py @@ -7,6 +7,7 @@ https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/ blob/master/examples/esp32spi_simpletest.py ''' +from os import getenv import board import busio from digitalio import DigitalInOut @@ -14,12 +15,17 @@ import adafruit_requests from adafruit_esp32spi import adafruit_esp32spi -# 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("Arduino Nano RP2040 Connect webclient test") @@ -51,7 +57,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except RuntimeError as e: print("could not connect to AP, retrying: ", e) continue diff --git a/Azure_Food_Scale/code.py b/Azure_Food_Scale/code.py index 2386afa34..3c6204a5e 100644 --- a/Azure_Food_Scale/code.py +++ b/Azure_Food_Scale/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 board @@ -38,15 +39,20 @@ nau7802.gain = 128 enabled = nau7802.enable(True) -# 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!") # check system time @@ -65,7 +71,7 @@ esp = None pool = socketpool.SocketPool(wifi.radio) device = IoTCentralDevice( - pool, esp, secrets['id_scope'], secrets['device_id'], secrets['device_primary_key'] + pool, esp, getenv("id_scope"), getenv("device_id"), getenv("device_primary_key") ) display.fill(0) display.print("DIALING*") @@ -191,6 +197,8 @@ def send_to_azure(current_oz, current_grams): values = [] val_offset = 0 avg_values = [] +the_ounces = 0 +the_grams = 0 # initial reading from the scale for w in range(5):