Skip to content

Commit 98c94a2

Browse files
committed
Updating examples to use settings.toml
Updating the esp32spi and native networking examples to use settings.toml
1 parent 9d4615b commit 98c94a2

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

examples/esp32spi/minimqtt_adafruitio_esp32spi.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
import os
45
import time
56
import board
67
import busio
78
from digitalio import DigitalInOut
89
import neopixel
910
from adafruit_esp32spi import adafruit_esp32spi
10-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

15-
### WiFi ###
15+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
16+
# with your WiFi credentials. Add your Adafruit IO username and key as well.
17+
# DO NOT share that file or commit it into Git or other source control.
1618

17-
# Get wifi details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
22-
raise
19+
aio_username = os.getenv('aio_username')
20+
aio_key = os.getenv('aio_key')
2321

2422
# If you are using a board with pre-defined ESP32 Pins:
2523
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -46,15 +44,14 @@
4644
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4745
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4846
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
5047

5148
### Feeds ###
5249

5350
# Setup a feed named 'photocell' for publishing to a feed
54-
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
51+
photocell_feed = aio_username + "/feeds/photocell"
5552

5653
# Setup a feed named 'onoff' for subscribing to changes
57-
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
54+
onoff_feed = aio_username + "/feeds/onoff"
5855

5956
### Code ###
6057

@@ -81,7 +78,7 @@ def message(client, topic, message):
8178

8279
# Connect to WiFi
8380
print("Connecting to WiFi...")
84-
wifi.connect()
81+
esp.connect_AP(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
8582
print("Connected!")
8683

8784
# Initialize MQTT interface with the esp interface
@@ -90,8 +87,8 @@ def message(client, topic, message):
9087
# Set up a MiniMQTT Client
9188
mqtt_client = MQTT.MQTT(
9289
broker="io.adafruit.com",
93-
username=secrets["aio_username"],
94-
password=secrets["aio_key"],
90+
username=aio_username,
91+
password=aio_key,
9592
)
9693

9794
# Setup the callback methods above

examples/native_networking/minimqtt_adafruitio_native_networking.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
import os
45
import time
56
import ssl
67
import socketpool
78
import wifi
89
import adafruit_minimqtt.adafruit_minimqtt as MQTT
910

10-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
11-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
11+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
12+
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
1213
# source control.
13-
# pylint: disable=no-name-in-module,wrong-import-order
14-
try:
15-
from secrets import secrets
16-
except ImportError:
17-
print("WiFi secrets are kept in secrets.py, please add them there!")
18-
raise
19-
20-
# Set your Adafruit IO Username and Key in secrets.py
14+
15+
# Set your Adafruit IO Username, Key and Port in settings.toml
2116
# (visit io.adafruit.com if you need to create an account,
2217
# or if you need your Adafruit IO key.)
23-
aio_username = secrets["aio_username"]
24-
aio_key = secrets["aio_key"]
18+
aio_username = os.getenv('aio_username')
19+
aio_key = os.getenv('aio_key')
20+
aio_port = os.getenv('port')
2521

26-
print("Connecting to %s" % secrets["ssid"])
27-
wifi.radio.connect(secrets["ssid"], secrets["password"])
28-
print("Connected to %s!" % secrets["ssid"])
22+
print("Connecting to %s" % os.getenv('CIRCUITPY_WIFI_SSID'))
23+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
24+
print("Connected to %s!" % os.getenv('CIRCUITPY_WIFI_SSID'))
2925
### Feeds ###
3026

3127
# Setup a feed named 'photocell' for publishing to a feed
32-
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
28+
photocell_feed = aio_username + "/feeds/photocell"
3329

3430
# Setup a feed named 'onoff' for subscribing to changes
35-
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
31+
onoff_feed = aio_username + "/feeds/onoff"
3632

3733
### Code ###
3834

@@ -63,9 +59,9 @@ def message(client, topic, message):
6359
# Set up a MiniMQTT Client
6460
mqtt_client = MQTT.MQTT(
6561
broker="io.adafruit.com",
66-
port=secrets["port"],
67-
username=secrets["aio_username"],
68-
password=secrets["aio_key"],
62+
port=aio_port,
63+
username=aio_username,
64+
password=aio_key,
6965
socket_pool=pool,
7066
ssl_context=ssl.create_default_context(),
7167
)

0 commit comments

Comments
 (0)