|
3 | 3 |
|
4 | 4 | """This example bridges from BLE to Adafruit IO on a CircuitPython device that |
5 | 5 | 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 |
8 | 7 | import time |
| 8 | +import adafruit_connection_manager |
9 | 9 | import adafruit_requests as requests |
10 | 10 | from adafruit_ble.advertising.standard import ManufacturerDataField |
11 | 11 | import adafruit_ble |
12 | 12 | import board |
13 | | -import socketpool |
14 | 13 | import wifi |
15 | 14 | import adafruit_ble_broadcastnet |
16 | 15 |
|
|
24 | 23 | print("No status pixel due to missing library") |
25 | 24 | neopixel = None |
26 | 25 |
|
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") |
29 | 32 |
|
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}" |
34 | 35 |
|
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) |
37 | 44 |
|
38 | 45 | status_pixel = None |
39 | 46 | if neopixel and hasattr(board, "NEOPIXEL"): |
|
42 | 49 |
|
43 | 50 | def aio_post(path, **kwargs): |
44 | 51 | kwargs["headers"] = aio_auth_header |
45 | | - return https.post(aio_base_url + path, **kwargs) |
| 52 | + return requests.post(aio_base_url + path, **kwargs) |
46 | 53 |
|
47 | 54 |
|
48 | 55 | def aio_get(path, **kwargs): |
49 | 56 | kwargs["headers"] = aio_auth_header |
50 | | - return https.get(aio_base_url + path, **kwargs) |
| 57 | + return requests.get(aio_base_url + path, **kwargs) |
51 | 58 |
|
52 | 59 |
|
53 | 60 | # Disable outer names check because we frequently collide. |
|
0 commit comments