Skip to content

Commit eec28fe

Browse files
authored
Merge pull request #108 from adafruit/update_simpletest_esp32s2
Update for settings.toml (CPY8) and SSL.
2 parents 2788c12 + 46d93c2 commit eec28fe

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py

+19-6
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,19 @@
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(
31+
"WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, please add them there!"
32+
)
2233
raise
2334

2435
# Set your Adafruit IO Username and Key in secrets.py
@@ -27,9 +38,10 @@
2738
aio_username = secrets["aio_username"]
2839
aio_key = secrets["aio_key"]
2940

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

3446

3547
# Define callback functions which will be called when certain events happen.
@@ -74,11 +86,12 @@ def message(client, feed_id, payload):
7486
# Initialize a new MQTT Client object
7587
mqtt_client = MQTT.MQTT(
7688
broker="io.adafruit.com",
77-
port=1883,
89+
port=8883,
7890
username=secrets["aio_username"],
7991
password=secrets["aio_key"],
8092
socket_pool=pool,
8193
ssl_context=ssl.create_default_context(),
94+
is_ssl=True,
8295
)
8396

8497
# Initialize an Adafruit IO MQTT Client

0 commit comments

Comments
 (0)