Skip to content
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

Remove secrets usage #32

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 8 additions & 3 deletions examples/ble_broadcastnet_blinka_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
# 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
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 your environment
# (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.)
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
aio_key = getenv("ADAFRUIT_AIO_KEY")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is meant for raspberry pi, so settings.toml is not going to automatically make it's way into environment variables in that environment.

We could instruct users to export the variables needed before running the script, but that would need to be done each time, so then we could point them to .bashrc or similar, but I'm not sure this is really the best option.

Maybe the script should be modified to parse the toml file and set environment variables from before accessing them. That way the user doesn't need to do anything manual and the experience is mostly the same as on MCU.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FoamyGuy how about this (it's what I do for mine):

import tomllib

# 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.)
with open("settings.toml", "rb") as toml_file:
    settings = tomllib.load(toml_file)
aio_username = settings("ADAFRUIT_AIO_USERNAME")
aio_key = settings("ADAFRUIT_AIO_KEY")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, however until we raise the minimum supported python version of the libraries and Blinka I think we should have code that is compatible with both tomllib and toml. And I think have a note in the error message if it fails to import either that they can install it from pip.

I'll make a note to discuss minimum supported version in the weeds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FoamyGuy, do we need to add toml to Blink first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If/when we add settings.toml loading to Blinka then yes I think we should add it as a req for any versions prior to it becoming built-in.

If the code is going to live in the example script like the one in this repo then I don't think we need to add it as a dependency for Blinka yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FoamyGuy so this should now be:

from adafruit_blinka import load_settings_toml
...
# 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")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that looks correct to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed here: dbd3761


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