Merge pull request #3016 from justmobilize/secrets-cleanup-q-w

Secrets Cleanup Q-W
This commit is contained in:
foamyguy 2025-04-16 12:39:29 -05:00 committed by GitHub
commit 3eaa064690
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 308 additions and 191 deletions

View file

@ -1 +0,0 @@
Minesweep/code.py 200: Simplify chained comparison between the operands (chained-comparison)

View file

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -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

View file

@ -17,16 +17,23 @@ All text above must be included in any redistribution.
"""
#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 @@ pyportal = PyPortal(url=url,
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:

View file

Before

Width:  |  Height:  |  Size: 225 KiB

After

Width:  |  Height:  |  Size: 225 KiB

View file

@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT
from os import getenv
import time
import ssl
import board
@ -22,26 +23,26 @@ NEO_PIN = board.SCK # neopixel pin
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:

View file

@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT
from os import getenv
import time
import board
import rtc
@ -25,16 +26,13 @@ NEO_CNT = 12 # neopixel count
# 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:

View file

@ -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 adafruit_io.adafruit_io import IO_HTTP
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 @@ lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
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 @@ while not connected:
# 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 = []

View file

@ -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 @@ import adafruit_ntp
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 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!")
@ -47,7 +53,7 @@ else:
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...")

View file

@ -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 @@ import adafruit_ntp
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 @@ else:
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...")

View file

@ -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 @@ import neopixel
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 @@ while True:
# 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 @@ while True:
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)

View file

@ -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 = digitalio.DigitalInOut(board.IO9)
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 @@ pool = socketpool.SocketPool(wifi.radio)
# 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(),
)

View file

@ -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 adafruit_minimqtt.adafruit_minimqtt as MQTT
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 @@ days = {
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 @@ pool = socketpool.SocketPool(wifi.radio)
# 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 @@ mqtt_client.connect()
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 @@ get("alarm-clock.saturday-enable")
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")

View file

@ -8,7 +8,8 @@ Feather ESP32-S3 or S2 + TFT Featherwing 3.5" 480x320 pixels
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 @@ import adafruit_tsc2007
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 @@ try:
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:

View file

@ -9,7 +9,7 @@ Receive and display messages from the spirits.
"""
# 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 @@ from spirit_board import SpiritBoard
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 @@ esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
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:

View file

@ -9,7 +9,7 @@ Receive and display messages from the spirits.
"""
# 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 @@ from spirit_board import SpiritBoard
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 @@ esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
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:

View file

@ -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 adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
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 @@ servo_one = True
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())

View file

@ -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)

View file

@ -10,6 +10,8 @@ This example queries the Open Weather Maps site API to find out the current
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.network import Network
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 @@ print("Getting weather for {}".format(LOCATION))
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

View file

@ -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_io.adafruit_io import IO_MQTT
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.I2C() # uses board.SCL and board.SDA
# 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 @@ esp32_reset = DigitalInOut(board.D12)
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 @@ ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
# 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,
)

View file

@ -4,6 +4,7 @@
"""CircuitPython WiFi Mailbox Notifier"""
from os import getenv
import time
import ssl
import alarm
@ -17,12 +18,20 @@ import microcontroller
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 @@ except Exception as error: # pylint: disable=broad-except
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)

View file

@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT
from os import getenv
import ipaddress
import wifi
import socketpool
@ -10,12 +11,17 @@ import simpleio
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 @@ last_tap = False
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)