Skip to content

Commit a8a97af

Browse files
authored
Merge pull request #32 from justmobilize/remove-secrets-usage
Remove secrets usage
2 parents 5358856 + dbd3761 commit a8a97af

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ To install in a virtual environment in your current project:
6060
Usage Example
6161
=============
6262

63-
Add a secrets.py file and then run ``ble_broadcastnet_blinka_bridge.py``.
63+
Add a settings.toml file and then run ``ble_broadcastnet_blinka_bridge.py``.
6464

6565
Documentation
6666
=============

examples/ble_broadcastnet_blinka_bridge.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
# SPDX-License-Identifier: MIT
33

44
"""This example bridges from BLE to Adafruit IO on a Raspberry Pi."""
5-
from secrets import secrets # pylint: disable=no-name-in-module
5+
from os import getenv
66
import time
77
import requests
88
from adafruit_ble.advertising.standard import ManufacturerDataField
9+
from adafruit_blinka import load_settings_toml
910
import adafruit_ble
1011
import adafruit_ble_broadcastnet
1112

12-
aio_auth_header = {"X-AIO-KEY": secrets["aio_key"]}
13-
aio_base_url = "https://io.adafruit.com/api/v2/" + secrets["aio_username"]
13+
# Get 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+
load_settings_toml()
16+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
17+
aio_key = getenv("ADAFRUIT_AIO_KEY")
18+
19+
aio_auth_header = {"X-AIO-KEY": aio_key}
20+
aio_base_url = f"https://io.adafruit.com/api/v2/{aio_username}"
1421

1522

1623
def aio_post(path, **kwargs):

examples/ble_broadcastnet_bridge.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
"""This example bridges from BLE to Adafruit IO on a CircuitPython device that
55
supports both WiFi and BLE. (The first chip is the ESP32-S3.)"""
6-
from secrets import secrets # pylint: disable=no-name-in-module
7-
import ssl
6+
from os import getenv
87
import time
8+
import adafruit_connection_manager
99
import adafruit_requests as requests
1010
from adafruit_ble.advertising.standard import ManufacturerDataField
1111
import adafruit_ble
1212
import board
13-
import socketpool
1413
import wifi
1514
import adafruit_ble_broadcastnet
1615

@@ -24,16 +23,24 @@
2423
print("No status pixel due to missing library")
2524
neopixel = None
2625

27-
aio_auth_header = {"X-AIO-KEY": secrets["aio_key"]}
28-
aio_base_url = "https://io.adafruit.com/api/v2/" + secrets["aio_username"]
26+
# Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml
27+
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
28+
ssid = getenv("CIRCUITPY_WIFI_SSID")
29+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
30+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
31+
aio_key = getenv("ADAFRUIT_AIO_KEY")
2932

30-
print("Connecting to %s" % secrets["ssid"])
31-
wifi.radio.connect(secrets["ssid"], secrets["password"])
32-
print("Connected to %s!" % secrets["ssid"])
33-
print("My IP address is", wifi.radio.ipv4_address)
33+
aio_auth_header = {"X-AIO-KEY": aio_key}
34+
aio_base_url = f"https://io.adafruit.com/api/v2/{aio_username}"
3435

35-
socket = socketpool.SocketPool(wifi.radio)
36-
https = requests.Session(socket, ssl.create_default_context())
36+
print(f"Connecting to {ssid}")
37+
wifi.radio.connect(ssid, password)
38+
print(f"Connected to {ssid}!")
39+
print(f"My IP address is {wifi.radio.ipv4_address}")
40+
41+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
42+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
43+
requests = requests.Session(pool, ssl_context)
3744

3845
status_pixel = None
3946
if neopixel and hasattr(board, "NEOPIXEL"):
@@ -42,12 +49,12 @@
4249

4350
def aio_post(path, **kwargs):
4451
kwargs["headers"] = aio_auth_header
45-
return https.post(aio_base_url + path, **kwargs)
52+
return requests.post(aio_base_url + path, **kwargs)
4653

4754

4855
def aio_get(path, **kwargs):
4956
kwargs["headers"] = aio_auth_header
50-
return https.get(aio_base_url + path, **kwargs)
57+
return requests.get(aio_base_url + path, **kwargs)
5158

5259

5360
# Disable outer names check because we frequently collide.

0 commit comments

Comments
 (0)