Skip to content

Commit 0794d69

Browse files
committed
Updating examples for settings dot toml
Retested examples using a Feather ESP32-S2 for native and a Metro M7 for esp32spi
1 parent eca0484 commit 0794d69

5 files changed

+83
-74
lines changed

examples/esp32spi/minimqtt_simpletest_esp32spi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
### Code ###
5959

60+
6061
# Define callback methods which are called when events occur
6162
# pylint: disable=unused-argument, redefined-outer-name
6263
def connect(mqtt_client, userdata, flags, rc):

examples/minimqtt_simpletest.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,57 @@
22
# SPDX-License-Identifier: MIT
33

44
import os
5-
import ssl
6-
import socketpool
7-
import wifi
5+
import board
6+
import busio
7+
from digitalio import DigitalInOut
8+
from adafruit_esp32spi import adafruit_esp32spi
9+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
810
import adafruit_minimqtt.adafruit_minimqtt as MQTT
911

1012
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
11-
# with your WiFi credentials. DO NOT share that file or commit it into Git or other
12-
# source control.
13+
# with your WiFi credentials. Add your Adafruit IO username and key as well.
14+
# DO NOT share that file or commit it into Git or other source control.
1315

14-
# Set your Adafruit IO Username and Key in settings.toml
15-
# (visit io.adafruit.com if you need to create an account,
16-
# or if you need your Adafruit IO key.)
1716
aio_username = os.getenv("aio_username")
1817
aio_key = os.getenv("aio_key")
1918

20-
print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID"))
21-
wifi.radio.connect(
22-
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
23-
)
24-
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
19+
# If you are using a board with pre-defined ESP32 Pins:
20+
esp32_cs = DigitalInOut(board.ESP_CS)
21+
esp32_ready = DigitalInOut(board.ESP_BUSY)
22+
esp32_reset = DigitalInOut(board.ESP_RESET)
23+
24+
# If you have an externally connected ESP32:
25+
# esp32_cs = DigitalInOut(board.D9)
26+
# esp32_ready = DigitalInOut(board.D10)
27+
# esp32_reset = DigitalInOut(board.D5)
28+
29+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
30+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
31+
32+
print("Connecting to AP...")
33+
while not esp.is_connected:
34+
try:
35+
esp.connect_AP(
36+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
37+
)
38+
except RuntimeError as e:
39+
print("could not connect to AP, retrying: ", e)
40+
continue
41+
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
2542

2643
### Topic Setup ###
2744

2845
# MQTT Topic
2946
# Use this topic if you'd like to connect to a standard MQTT broker
30-
mqtt_topic = "test/topic"
47+
# mqtt_topic = "test/topic"
3148

3249
# Adafruit IO-style Topic
3350
# Use this topic if you'd like to connect to io.adafruit.com
34-
# mqtt_topic = aio_username + '/feeds/temperature'
35-
51+
mqtt_topic = aio_username + "/feeds/temperature"
3652

3753
### Code ###
54+
55+
3856
# Define callback methods which are called when events occur
3957
# pylint: disable=unused-argument, redefined-outer-name
4058
def connect(mqtt_client, userdata, flags, rc):
@@ -66,21 +84,17 @@ def publish(mqtt_client, userdata, topic, pid):
6684

6785

6886
def message(client, topic, message):
69-
# Method called when a client's subscribed feed has a new value.
7087
print("New message on topic {0}: {1}".format(topic, message))
7188

7289

73-
# Create a socket pool
74-
pool = socketpool.SocketPool(wifi.radio)
90+
socket.set_interface(esp)
91+
MQTT.set_socket(socket, esp)
7592

7693
# Set up a MiniMQTT Client
7794
mqtt_client = MQTT.MQTT(
7895
broker="io.adafruit.com",
79-
port=8883,
8096
username=aio_username,
8197
password=aio_key,
82-
socket_pool=pool,
83-
ssl_context=ssl.create_default_context(),
8498
)
8599

86100
# Connect callback handlers to mqtt_client

examples/native_networking/minimqtt_adafruitio_native_networking.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
aio_username = os.getenv("aio_username")
1919
aio_key = os.getenv("aio_key")
2020

21-
print("Connecting to %s" % os.getenv("CIRCUITPY_WIFI_SSID"))
21+
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
2222
wifi.radio.connect(
2323
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
2424
)
25-
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
25+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
2626
### Feeds ###
2727

2828
# Setup a feed named 'photocell' for publishing to a feed
@@ -39,7 +39,7 @@
3939
def connected(client, userdata, flags, rc):
4040
# This function will be called when the client is connected
4141
# successfully to the broker.
42-
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
42+
print(f"Connected to Adafruit IO! Listening for topic changes on {onoff_feed}")
4343
# Subscribe to all changes on the onoff_feed.
4444
client.subscribe(onoff_feed)
4545

@@ -52,7 +52,7 @@ def disconnected(client, userdata, rc):
5252
def message(client, topic, message):
5353
# This method is called when a topic the client is subscribed to
5454
# has a new message.
55-
print("New message on topic {0}: {1}".format(topic, message))
55+
print(f"New message on topic {topic}: {message}")
5656

5757

5858
# Create a socket pool
@@ -61,9 +61,9 @@ def message(client, topic, message):
6161
# Set up a MiniMQTT Client
6262
mqtt_client = MQTT.MQTT(
6363
broker="io.adafruit.com",
64-
port=8883,
65-
username=os.getenv("CIRCUITPY_WIFI_SSID"),
66-
password=os.getenv("CIRCUITPY_WIFI_PASSWORD"),
64+
port=1883,
65+
username=aio_username,
66+
password=aio_key,
6767
socket_pool=pool,
6868
ssl_context=ssl.create_default_context(),
6969
)
@@ -83,7 +83,7 @@ def message(client, topic, message):
8383
mqtt_client.loop()
8484

8585
# Send a new message
86-
print("Sending photocell value: %d..." % photocell_val)
86+
print(f"Sending photocell value: {photocell_val}...")
8787
mqtt_client.publish(photocell_feed, photocell_val)
8888
print("Sent!")
8989
photocell_val += 1

examples/native_networking/minimqtt_pub_sub_blocking_native_networking.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +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")
2520

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

3027
### Adafruit IO Setup ###
3128

3229
# Setup a feed named `testfeed` for publishing.
33-
default_topic = secrets["aio_username"] + "/feeds/testfeed"
30+
default_topic = aio_username + "/feeds/testfeed"
31+
3432

3533
### Code ###
3634
# Define callback methods which are called when events occur
@@ -62,10 +60,10 @@ def message(client, topic, message):
6260

6361
# Set up a MiniMQTT Client
6462
mqtt_client = MQTT.MQTT(
65-
broker=secrets["broker"],
66-
port=secrets["port"],
67-
username=secrets["aio_username"],
68-
password=secrets["aio_key"],
63+
broker="io.adafruit.com",
64+
port=1883,
65+
username=aio_username,
66+
password=aio_key,
6967
socket_pool=pool,
7068
ssl_context=ssl.create_default_context(),
7169
)

examples/native_networking/minimqtt_pub_sub_blocking_topic_callbacks_native_networking.py

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
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")
2520

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

3027
### Code ###
3128

29+
3230
# Define callback methods which are called when events occur
3331
# pylint: disable=unused-argument, redefined-outer-name
3432
def connected(client, userdata, flags, rc):
@@ -56,7 +54,7 @@ def on_battery_msg(client, topic, message):
5654
# Method called when device/batteryLife has a new value
5755
print("Battery level: {}v".format(message))
5856

59-
# client.remove_topic_callback(secrets["aio_username"] + "/feeds/device.batterylevel")
57+
# client.remove_topic_callback(aio_username + "/feeds/device.batterylevel")
6058

6159

6260
def on_message(client, topic, message):
@@ -69,10 +67,10 @@ def on_message(client, topic, message):
6967

7068
# Set up a MiniMQTT Client
7169
client = MQTT.MQTT(
72-
broker=secrets["broker"],
73-
port=secrets["port"],
74-
username=secrets["aio_username"],
75-
password=secrets["aio_key"],
70+
broker="io.adafruit.com",
71+
port=1883,
72+
username=aio_username,
73+
password=aio_key,
7674
socket_pool=pool,
7775
ssl_context=ssl.create_default_context(),
7876
)
@@ -83,16 +81,14 @@ def on_message(client, topic, message):
8381
client.on_subscribe = subscribe
8482
client.on_unsubscribe = unsubscribe
8583
client.on_message = on_message
86-
client.add_topic_callback(
87-
secrets["aio_username"] + "/feeds/device.batterylevel", on_battery_msg
88-
)
84+
client.add_topic_callback(aio_username + "/feeds/device.batterylevel", on_battery_msg)
8985

9086
# Connect the client to the MQTT broker.
9187
print("Connecting to MQTT broker...")
9288
client.connect()
9389

9490
# Subscribe to all notifications on the device group
95-
client.subscribe(secrets["aio_username"] + "/groups/device", 1)
91+
client.subscribe(aio_username + "/groups/device", 1)
9692

9793
# Start a blocking message loop...
9894
# NOTE: NO code below this loop will execute

0 commit comments

Comments
 (0)