Skip to content

Update for settings.toml (CPY8) and SSL. #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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