-
Notifications
You must be signed in to change notification settings - Fork 789
Secrets Cleanup: A #2989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Secrets Cleanup: A #2989
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}") | ||
Comment on lines
+93
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. standard cleanup |
||
|
||
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"] | ||
|
||
Comment on lines
-93
to
-98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. common to have this after the fact, now up top |
||
# Initialize an Adafruit IO HTTP API object | ||
io = IO_HTTP(aio_username, aio_key, requests) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pylint |
||
|
||
# Create sensor objects, using the board's default I2C bus. | ||
i2c = board.I2C() # uses board.SCL and board.SDA | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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." | ||
) | ||
Comment on lines
+18
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new standard wifi only block |
||
|
||
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()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. standard update to |
||
|
||
# Connect to a PM2.5 sensor over UART | ||
reset_pin = None | ||
|
@@ -142,19 +151,19 @@ 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") | ||
feed_aqi_category = io.get_feed("air-quality-sensor.category") | ||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some |
||
|
||
# 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, | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New standard wifi+aio block