Skip to content

Commit

Permalink
Merge pull request #32 from justmobilize/remove-secrets-usage
Browse files Browse the repository at this point in the history
Remove secrets usage
  • Loading branch information
FoamyGuy authored Mar 5, 2025
2 parents 5358856 + dbd3761 commit a8a97af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To install in a virtual environment in your current project:
Usage Example
=============

Add a secrets.py file and then run ``ble_broadcastnet_blinka_bridge.py``.
Add a settings.toml file and then run ``ble_broadcastnet_blinka_bridge.py``.

Documentation
=============
Expand Down
13 changes: 10 additions & 3 deletions examples/ble_broadcastnet_blinka_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
# SPDX-License-Identifier: MIT

"""This example bridges from BLE to Adafruit IO on a Raspberry Pi."""
from secrets import secrets # pylint: disable=no-name-in-module
from os import getenv
import time
import requests
from adafruit_ble.advertising.standard import ManufacturerDataField
from adafruit_blinka import load_settings_toml
import adafruit_ble
import adafruit_ble_broadcastnet

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

aio_auth_header = {"X-AIO-KEY": aio_key}
aio_base_url = f"https://io.adafruit.com/api/v2/{aio_username}"


def aio_post(path, **kwargs):
Expand Down
33 changes: 20 additions & 13 deletions examples/ble_broadcastnet_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

"""This example bridges from BLE to Adafruit IO on a CircuitPython device that
supports both WiFi and BLE. (The first chip is the ESP32-S3.)"""
from secrets import secrets # pylint: disable=no-name-in-module
import ssl
from os import getenv
import time
import adafruit_connection_manager
import adafruit_requests as requests
from adafruit_ble.advertising.standard import ManufacturerDataField
import adafruit_ble
import board
import socketpool
import wifi
import adafruit_ble_broadcastnet

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

aio_auth_header = {"X-AIO-KEY": secrets["aio_key"]}
aio_base_url = "https://io.adafruit.com/api/v2/" + secrets["aio_username"]
# 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")

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)
aio_auth_header = {"X-AIO-KEY": aio_key}
aio_base_url = f"https://io.adafruit.com/api/v2/{aio_username}"

socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())
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 = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = requests.Session(pool, ssl_context)

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

def aio_post(path, **kwargs):
kwargs["headers"] = aio_auth_header
return https.post(aio_base_url + path, **kwargs)
return requests.post(aio_base_url + path, **kwargs)


def aio_get(path, **kwargs):
kwargs["headers"] = aio_auth_header
return https.get(aio_base_url + path, **kwargs)
return requests.get(aio_base_url + path, **kwargs)


# Disable outer names check because we frequently collide.
Expand Down

0 comments on commit a8a97af

Please sign in to comment.