|
4 | 4 | # Adafruit IO HTTP API - Group Interactions
|
5 | 5 | # Documentation: https://io.adafruit.com/api/docs/#groups
|
6 | 6 | # adafruit_circuitpython_adafruitio with an esp32spi_socket
|
| 7 | +from os import getenv |
7 | 8 | import adafruit_datetime as datetime
|
8 | 9 | import board
|
9 | 10 | import busio
|
|
13 | 14 | import adafruit_requests
|
14 | 15 | from adafruit_io.adafruit_io import IO_HTTP
|
15 | 16 |
|
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") |
38 | 23 |
|
39 | 24 | # If you are using a board with pre-defined ESP32 Pins:
|
40 | 25 | esp32_cs = DigitalInOut(board.ESP_CS)
|
|
52 | 37 | print("Connecting to AP...")
|
53 | 38 | while not esp.is_connected:
|
54 | 39 | try:
|
55 |
| - esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 40 | + esp.connect_AP(ssid, password) |
56 | 41 | except RuntimeError as e:
|
57 | 42 | print("could not connect to AP, retrying: ", e)
|
58 | 43 | continue
|
59 | 44 | print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
|
60 | 45 |
|
61 | 46 | # 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) |
63 | 48 | # import wifi
|
64 | 49 | # esp = wifi.radio
|
65 | 50 |
|
|
71 | 56 | # If you are testing on python with blinka, use real requests below and comment out above:
|
72 | 57 | # import os, datetime, requests as real_requests
|
73 | 58 | # 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 |
| -# } |
78 | 59 | # requests = real_requests.Session()
|
79 | 60 |
|
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 |
| - |
87 | 61 | # Initialize an Adafruit IO HTTP API object
|
88 | 62 | io = IO_HTTP(aio_username, aio_key, requests)
|
89 | 63 |
|
|
0 commit comments