Skip to content

Commit 69005e1

Browse files
committed
Update for settings.toml (CPY8) and SSL.
Tested on Adafruit Feather ESP32-S2 Reverse TFT with CPY 8.2.4
1 parent 0772f3b commit 69005e1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from random import randint
55

6+
import os
67
import ssl
78
import socketpool
89
import wifi
@@ -16,9 +17,17 @@
1617
# source control.
1718
# pylint: disable=no-name-in-module,wrong-import-order
1819
try:
19-
from secrets import secrets
20+
if(os.getenv("AIO_USERNAME") and os.getenv("AIO_KEY")):
21+
secrets = {
22+
"aio_username": os.getenv("AIO_USERNAME"),
23+
"aio_key": os.getenv("AIO_KEY"),
24+
"ssid": os.getenv("CIRCUITPY_WIFI_SSID"),
25+
"password": os.getenv("CIRCUITPY_WIFI_PASSWORD"),
26+
}
27+
else:
28+
from secrets import secrets
2029
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
30+
print("WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, please add them there!")
2231
raise
2332

2433
# Set your Adafruit IO Username and Key in secrets.py
@@ -27,9 +36,10 @@
2736
aio_username = secrets["aio_username"]
2837
aio_key = secrets["aio_key"]
2938

30-
print("Connecting to %s" % secrets["ssid"])
31-
wifi.radio.connect(secrets["ssid"], secrets["password"])
32-
print("Connected to %s!" % secrets["ssid"])
39+
if not wifi.radio.connected:
40+
print("Connecting to %s" % secrets["ssid"])
41+
wifi.radio.connect(secrets["ssid"], secrets["password"])
42+
print("Connected to %s!" % secrets["ssid"])
3343

3444

3545
# Define callback functions which will be called when certain events happen.
@@ -74,11 +84,12 @@ def message(client, feed_id, payload):
7484
# Initialize a new MQTT Client object
7585
mqtt_client = MQTT.MQTT(
7686
broker="io.adafruit.com",
77-
port=1883,
87+
port=8883,
7888
username=secrets["aio_username"],
7989
password=secrets["aio_key"],
8090
socket_pool=pool,
8191
ssl_context=ssl.create_default_context(),
92+
is_ssl=True
8293
)
8394

8495
# Initialize an Adafruit IO MQTT Client

0 commit comments

Comments
 (0)