Skip to content

Commit 7e8f3c5

Browse files
authored
Merge pull request #129 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 48fb3ae + ea7c397 commit 7e8f3c5

23 files changed

+258
-410
lines changed

examples/adafruit_io_http/adafruit_io_analog_in.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Example of publishing the value of an ADC to Adafruit IO
55
# adafruit_circuitpython_adafruitio with an esp32spi_socket
6+
from os import getenv
67
import time
78
import board
89
import busio
@@ -13,15 +14,12 @@
1314
import adafruit_requests
1415
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1516

16-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
17-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
18-
# source control.
19-
# pylint: disable=no-name-in-module,wrong-import-order
20-
try:
21-
from secrets import secrets
22-
except ImportError:
23-
print("WiFi secrets are kept in secrets.py, please add them there!")
24-
raise
17+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
ssid = getenv("CIRCUITPY_WIFI_SSID")
20+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
21+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
22+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2523

2624
# If you are using a board with pre-defined ESP32 Pins:
2725
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -39,7 +37,7 @@
3937
print("Connecting to AP...")
4038
while not esp.is_connected:
4139
try:
42-
esp.connect_AP(secrets["ssid"], secrets["password"])
40+
esp.connect_AP(ssid, password)
4341
except RuntimeError as e:
4442
print("could not connect to AP, retrying: ", e)
4543
continue
@@ -50,12 +48,6 @@
5048
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
5149
requests = adafruit_requests.Session(pool, ssl_context)
5250

53-
# Set your Adafruit IO Username and Key in secrets.py
54-
# (visit io.adafruit.com if you need to create an account,
55-
# or if you need your Adafruit IO key.)
56-
aio_username = secrets["aio_username"]
57-
aio_key = secrets["aio_key"]
58-
5951
# Initialize an Adafruit IO HTTP API object
6052
io = IO_HTTP(aio_username, aio_key, requests)
6153

examples/adafruit_io_http/adafruit_io_batch_cpython.py

+7-26
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,19 @@
33

44
# adafruit_circuitpython_adafruitio usage for batch data with a CPython socket.
55
import datetime
6+
from os import getenv
67
import socket
78
import ssl
89
from random import randint
910
import adafruit_requests
1011
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1112

12-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "aio_username"
13-
# and "aio_key" entries with your IO credentials, or set environment variables/defaults below.
14-
# *** DO NOT share that file or commit it into Git or other source control. ***
15-
# pylint: disable=no-name-in-module,wrong-import-order
16-
try:
17-
from secrets import secrets
18-
except ImportError:
19-
import os
20-
21-
secrets = {
22-
"aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"),
23-
"aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"),
24-
}
25-
if (
26-
secrets["aio_key"] == "Your_Adafruit_IO_Key_Here"
27-
or secrets["aio_username"] == "Your_Username_Here"
28-
):
29-
print("Adafruit IO secrets are kept in secrets.py, please add them there!")
30-
raise
31-
32-
# Set your Adafruit IO Username and Key in secrets.py
33-
# (visit io.adafruit.com if you need to create an account,
34-
# or if you need your Adafruit IO key.)
35-
aio_username = secrets["aio_username"]
36-
aio_key = secrets["aio_key"]
37-
13+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
14+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
15+
ssid = getenv("CIRCUITPY_WIFI_SSID")
16+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
17+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
18+
aio_key = getenv("ADAFRUIT_AIO_KEY")
3819

3920
requests = adafruit_requests.Session(socket, ssl.create_default_context())
4021
# Initialize an Adafruit IO HTTP API object

examples/adafruit_io_http/adafruit_io_create_and_get_feed.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,28 @@
44
Example using create_and_get_feed. Creates a new feed if it does not exist and sends to it, or
55
sends to an existing feed once it has been created.
66
"""
7-
import ssl
7+
from os import getenv
88
import adafruit_requests
9-
import socketpool
109
import wifi
1110
import microcontroller
11+
import adafruit_connection_manager
1212
from adafruit_io.adafruit_io import IO_HTTP
1313

14-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
15-
# "password" keys with your WiFi credentials, and "aio_username" and "aio_key" keys with your
16-
# Adafruit IO credentials, DO NOT share that file or commit it into Git or other
17-
# source control.
18-
# pylint: disable=no-name-in-module,wrong-import-order
19-
try:
20-
from secrets import secrets
21-
except ImportError:
22-
print(
23-
"WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!"
24-
)
25-
raise
14+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
15+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
18+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
19+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2620

27-
# Connect to Wi-Fi using credentials from secrets.py
28-
wifi.radio.connect(secrets["ssid"], secrets["password"])
29-
print("Connected to {}!".format(secrets["ssid"]))
21+
# Connect to Wi-Fi using credentials from settings.toml
22+
wifi.radio.connect(ssid, password)
23+
print("Connected to {}!".format(ssid))
3024
print("IP:", wifi.radio.ipv4_address)
3125

32-
pool = socketpool.SocketPool(wifi.radio)
33-
requests = adafruit_requests.Session(pool, ssl.create_default_context())
34-
35-
# Obtain Adafruit IO credentials from secrets.py
36-
aio_username = secrets["aio_username"]
37-
aio_key = secrets["aio_key"]
26+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
27+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
28+
requests = adafruit_requests.Session(pool, ssl_context)
3829

3930
# Initialize an Adafruit IO HTTP API object
4031
io = IO_HTTP(aio_username, aio_key, requests)

examples/adafruit_io_http/adafruit_io_digital_out.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Turn on and off a LED from your Adafruit IO Dashboard.
55
# adafruit_circuitpython_adafruitio with an esp32spi_socket
66
import time
7+
from os import getenv
78
import board
89
import busio
910
from digitalio import DigitalInOut, Direction
@@ -12,15 +13,12 @@
1213
import adafruit_requests
1314
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1415

15-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
16-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
17-
# source control.
18-
# pylint: disable=no-name-in-module,wrong-import-order
19-
try:
20-
from secrets import secrets
21-
except ImportError:
22-
print("WiFi secrets are kept in secrets.py, please add them there!")
23-
raise
16+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
17+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
20+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
21+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2422

2523
# If you are using a board with pre-defined ESP32 Pins:
2624
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -38,7 +36,7 @@
3836
print("Connecting to AP...")
3937
while not esp.is_connected:
4038
try:
41-
esp.connect_AP(secrets["ssid"], secrets["password"])
39+
esp.connect_AP(ssid, password)
4240
except RuntimeError as e:
4341
print("could not connect to AP, retrying: ", e)
4442
continue
@@ -49,12 +47,6 @@
4947
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
5048
requests = adafruit_requests.Session(pool, ssl_context)
5149

52-
# Set your Adafruit IO Username and Key in secrets.py
53-
# (visit io.adafruit.com if you need to create an account,
54-
# or if you need your Adafruit IO key.)
55-
aio_username = secrets["aio_username"]
56-
aio_key = secrets["aio_key"]
57-
5850
# Initialize an Adafruit IO HTTP API object
5951
io = IO_HTTP(aio_username, aio_key, requests)
6052

examples/adafruit_io_http/adafruit_io_feeds.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Adafruit IO HTTP API - Feed Interactions
55
# Documentation: https://io.adafruit.com/api/docs/#feeds
66
# adafruit_circuitpython_adafruitio with an esp32spi_socket
7+
from os import getenv
78
import board
89
import busio
910
from digitalio import DigitalInOut
@@ -12,15 +13,12 @@
1213
import adafruit_requests
1314
from adafruit_io.adafruit_io import IO_HTTP
1415

15-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
16-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
17-
# source control.
18-
# pylint: disable=no-name-in-module,wrong-import-order
19-
try:
20-
from secrets import secrets
21-
except ImportError:
22-
print("WiFi secrets are kept in secrets.py, please add them there!")
23-
raise
16+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
17+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
20+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
21+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2422

2523
# If you are using a board with pre-defined ESP32 Pins:
2624
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -38,7 +36,7 @@
3836
print("Connecting to AP...")
3937
while not esp.is_connected:
4038
try:
41-
esp.connect_AP(secrets["ssid"], secrets["password"])
39+
esp.connect_AP(ssid, password)
4240
except RuntimeError as e:
4341
print("could not connect to AP, retrying: ", e)
4442
continue
@@ -49,12 +47,6 @@
4947
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
5048
requests = adafruit_requests.Session(pool, ssl_context)
5149

52-
# Set your Adafruit IO Username and Key in secrets.py
53-
# (visit io.adafruit.com if you need to create an account,
54-
# or if you need your Adafruit IO key.)
55-
aio_username = secrets["aio_username"]
56-
aio_key = secrets["aio_key"]
57-
5850
# Initialize an Adafruit IO HTTP API object
5951
io = IO_HTTP(aio_username, aio_key, requests)
6052

examples/adafruit_io_http/adafruit_io_groups.py

+9-35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Adafruit IO HTTP API - Group Interactions
55
# Documentation: https://io.adafruit.com/api/docs/#groups
66
# adafruit_circuitpython_adafruitio with an esp32spi_socket
7+
from os import getenv
78
import adafruit_datetime as datetime
89
import board
910
import busio
@@ -13,28 +14,12 @@
1314
import adafruit_requests
1415
from adafruit_io.adafruit_io import IO_HTTP
1516

16-
17-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
18-
# "password" keys with your WiFi credentials, along with "aio_username" and "aio_key" for
19-
# your Adafruit IO user/key. DO NOT share that file or commit it into Git or other source control.
20-
# pylint: disable=no-name-in-module,wrong-import-order
21-
try:
22-
from secrets import secrets
23-
except ImportError:
24-
import os
25-
26-
if os.getenv("ADAFRUIT_AIO_USERNAME") and os.getenv("ADAFRUIT_AIO_KEY"):
27-
secrets = {
28-
"aio_username": os.getenv("ADAFRUIT_AIO_USERNAME", "Your_Username_Here"),
29-
"aio_key": os.getenv("ADAFRUIT_AIO_KEY", "Your_Adafruit_IO_Key_Here"),
30-
"ssid": os.getenv("CIRCUITPY_WIFI_SSID", ""),
31-
"password": os.getenv("CIRCUITPY_WIFI_PASSWORD", ""),
32-
}
33-
else:
34-
print(
35-
"WiFi + Adafruit IO secrets are kept in secrets.py, please add them there!"
36-
)
37-
raise
17+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
18+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
19+
ssid = getenv("CIRCUITPY_WIFI_SSID")
20+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
21+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
22+
aio_key = getenv("ADAFRUIT_AIO_KEY")
3823

3924
# If you are using a board with pre-defined ESP32 Pins:
4025
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -52,14 +37,14 @@
5237
print("Connecting to AP...")
5338
while not esp.is_connected:
5439
try:
55-
esp.connect_AP(secrets["ssid"], secrets["password"])
40+
esp.connect_AP(ssid, password)
5641
except RuntimeError as e:
5742
print("could not connect to AP, retrying: ", e)
5843
continue
5944
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
6045

6146
# If you are using a wifi based mcu use this instead of esp code above, remove the from
62-
# adafruit_esp32spi import line, optionally esp.connect(secrets["ssid"], secrets["password"])
47+
# adafruit_esp32spi import line, optionally esp.connect(ssid, password)
6348
# import wifi
6449
# esp = wifi.radio
6550

@@ -71,19 +56,8 @@
7156
# If you are testing on python with blinka, use real requests below and comment out above:
7257
# import os, datetime, requests as real_requests
7358
# from adafruit_io.adafruit_io import IO_HTTP
74-
# secrets = {
75-
# "aio_username": os.getenv("ADAFRUIT_AIO_USERNAME"),
76-
# "aio_key": os.getenv("ADAFRUIT_AIO_KEY"),
77-
# }
7859
# requests = real_requests.Session()
7960

80-
81-
# Set your Adafruit IO Username and Key in secrets.py
82-
# (visit io.adafruit.com if you need to create an account,
83-
# or if you need your Adafruit IO key.)
84-
aio_username = secrets["aio_username"]
85-
aio_key = secrets["aio_key"]
86-
8761
# Initialize an Adafruit IO HTTP API object
8862
io = IO_HTTP(aio_username, aio_key, requests)
8963

examples/adafruit_io_http/adafruit_io_metadata.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Adafruit IO HTTP API - Sending values with optional metadata
55
# adafruit_circuitpython_adafruitio with an esp32spi_socket
6+
from os import getenv
67
import board
78
import busio
89
from digitalio import DigitalInOut
@@ -11,15 +12,12 @@
1112
import adafruit_requests
1213
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1314

14-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
15-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
16-
# source control.
17-
# pylint: disable=no-name-in-module,wrong-import-order
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
22-
raise
15+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
16+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
17+
ssid = getenv("CIRCUITPY_WIFI_SSID")
18+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
19+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
20+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2321

2422
# If you are using a board with pre-defined ESP32 Pins:
2523
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -37,7 +35,7 @@
3735
print("Connecting to AP...")
3836
while not esp.is_connected:
3937
try:
40-
esp.connect_AP(secrets["ssid"], secrets["password"])
38+
esp.connect_AP(ssid, password)
4139
except RuntimeError as e:
4240
print("could not connect to AP, retrying: ", e)
4341
continue
@@ -48,12 +46,6 @@
4846
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
4947
requests = adafruit_requests.Session(pool, ssl_context)
5048

51-
# Set your Adafruit IO Username and Key in secrets.py
52-
# (visit io.adafruit.com if you need to create an account,
53-
# or if you need your Adafruit IO key.)
54-
aio_username = secrets["aio_username"]
55-
aio_key = secrets["aio_key"]
56-
5749
# Initialize an Adafruit IO HTTP API object
5850
io = IO_HTTP(aio_username, aio_key, requests)
5951

0 commit comments

Comments
 (0)