Skip to content

Commit

Permalink
Merge pull request #108 from adafruit/update_simpletest_esp32s2
Browse files Browse the repository at this point in the history
Update for settings.toml (CPY8) and SSL.
  • Loading branch information
FoamyGuy authored Oct 8, 2023
2 parents 2788c12 + 46d93c2 commit eec28fe
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions examples/adafruit_io_mqtt/adafruit_io_simpletest_esp32s2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
from random import randint

import os
import ssl
import socketpool
import wifi
Expand All @@ -16,9 +17,19 @@
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
if os.getenv("AIO_USERNAME") and os.getenv("AIO_KEY"):
secrets = {
"aio_username": os.getenv("AIO_USERNAME"),
"aio_key": os.getenv("AIO_KEY"),
"ssid": os.getenv("CIRCUITPY_WIFI_SSID"),
"password": os.getenv("CIRCUITPY_WIFI_PASSWORD"),
}
else:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
print(
"WiFi + Adafruit IO secrets are kept in secrets.py or settings.toml, please add them there!"
)
raise

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

print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
if not wifi.radio.connected:
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])


# Define callback functions which will be called when certain events happen.
Expand Down Expand Up @@ -74,11 +86,12 @@ def message(client, feed_id, payload):
# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(
broker="io.adafruit.com",
port=1883,
port=8883,
username=secrets["aio_username"],
password=secrets["aio_key"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
is_ssl=True,
)

# Initialize an Adafruit IO MQTT Client
Expand Down

0 comments on commit eec28fe

Please sign in to comment.