Skip to content

Commit 7b63446

Browse files
authored
Merge pull request #162 from adafruit/settings_dot_toml
Settings dot toml
2 parents 40b9096 + 6b96ab3 commit 7b63446

7 files changed

+140
-144
lines changed

examples/esp32spi/minimqtt_adafruitio_esp32spi.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
import os
45
import time
56
import board
67
import busio
78
from digitalio import DigitalInOut
89
import neopixel
910
from adafruit_esp32spi import adafruit_esp32spi
10-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

15-
### WiFi ###
15+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
16+
# with your WiFi credentials. Add your Adafruit IO username and key as well.
17+
# DO NOT share that file or commit it into Git or other source control.
1618

17-
# Get wifi details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
22-
raise
19+
aio_username = os.getenv("aio_username")
20+
aio_key = os.getenv("aio_key")
2321

2422
# If you are using a board with pre-defined ESP32 Pins:
2523
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -46,15 +44,14 @@
4644
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4745
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4846
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
5047

5148
### Feeds ###
5249

5350
# Setup a feed named 'photocell' for publishing to a feed
54-
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
51+
photocell_feed = aio_username + "/feeds/photocell"
5552

5653
# Setup a feed named 'onoff' for subscribing to changes
57-
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
54+
onoff_feed = aio_username + "/feeds/onoff"
5855

5956
### Code ###
6057

@@ -82,7 +79,7 @@ def message(client, topic, message):
8279

8380
# Connect to WiFi
8481
print("Connecting to WiFi...")
85-
wifi.connect()
82+
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
8683
print("Connected!")
8784

8885
# Initialize MQTT interface with the esp interface
@@ -91,8 +88,8 @@ def message(client, topic, message):
9188
# Set up a MiniMQTT Client
9289
mqtt_client = MQTT.MQTT(
9390
broker="io.adafruit.com",
94-
username=secrets["aio_username"],
95-
password=secrets["aio_key"],
91+
username=aio_username,
92+
password=aio_key,
9693
)
9794

9895
# Setup the callback methods above

examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
import os
45
import time
56
import board
67
import busio
78
from digitalio import DigitalInOut
89
import neopixel
910
from adafruit_esp32spi import adafruit_esp32spi
10-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

15-
### WiFi ###
15+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
16+
# with your WiFi credentials. Add your Adafruit IO username and key as well.
17+
# DO NOT share that file or commit it into Git or other source control.
1618

17-
# Get wifi details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
22-
raise
19+
aio_username = os.getenv("aio_username")
20+
aio_key = os.getenv("aio_key")
2321

2422
# If you are using a board with pre-defined ESP32 Pins:
2523
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -46,12 +44,11 @@
4644
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4745
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4846
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
5047

5148
### Adafruit IO Setup ###
5249

5350
# Setup a feed named `testfeed` for publishing.
54-
default_topic = secrets["user"] + "/feeds/testfeed"
51+
default_topic = aio_username + "/feeds/testfeed"
5552

5653
### Code ###
5754

@@ -82,15 +79,15 @@ def message(client, topic, message):
8279

8380
# Connect to WiFi
8481
print("Connecting to WiFi...")
85-
wifi.connect()
82+
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
8683
print("Connected!")
8784

8885
# Initialize MQTT interface with the esp interface
8986
MQTT.set_socket(socket, esp)
9087

9188
# Set up a MiniMQTT Client
9289
mqtt_client = MQTT.MQTT(
93-
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
90+
broker="io.adafruit.com", username=aio_username, password=aio_key
9491
)
9592

9693
# Setup the callback methods above
@@ -110,7 +107,11 @@ def message(client, topic, message):
110107
mqtt_client.loop()
111108
except (ValueError, RuntimeError) as e:
112109
print("Failed to get data, retrying\n", e)
113-
wifi.reset()
110+
esp.reset()
111+
time.sleep(1)
112+
esp.connect_AP(
113+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
114+
)
114115
mqtt_client.reconnect()
115116
continue
116117
time.sleep(1)

examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4+
import os
45
import time
56
import board
67
import busio
78
from digitalio import DigitalInOut
89
import neopixel
910
from adafruit_esp32spi import adafruit_esp32spi
10-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
1111
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

15-
### WiFi ###
15+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
16+
# with your WiFi credentials. Add your Adafruit IO username and key as well.
17+
# DO NOT share that file or commit it into Git or other source control.
1618

17-
# Get wifi details and more from a secrets.py file
18-
try:
19-
from secrets import secrets
20-
except ImportError:
21-
print("WiFi secrets are kept in secrets.py, please add them there!")
22-
raise
19+
aio_username = os.getenv("aio_username")
20+
aio_key = os.getenv("aio_key")
2321

2422
# If you are using a board with pre-defined ESP32 Pins:
2523
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -46,12 +44,11 @@
4644
# GREEN_LED = PWMOut.PWMOut(esp, 27)
4745
# BLUE_LED = PWMOut.PWMOut(esp, 25)
4846
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
49-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
5047

5148
### Adafruit IO Setup ###
5249

5350
# Setup a feed named `testfeed` for publishing.
54-
default_topic = secrets["user"] + "/feeds/testfeed"
51+
default_topic = aio_username + "/feeds/testfeed"
5552

5653

5754
### Code ###
@@ -60,7 +57,7 @@
6057
def connected(client, userdata, flags, rc):
6158
# This function will be called when the client is connected
6259
# successfully to the broker.
63-
print("Connected to MQTT broker! Listening for topic changes on %s" % default_topic)
60+
print(f"Connected to MQTT broker! Listening for topic changes on {default_topic}")
6461
# Subscribe to all changes on the default_topic feed.
6562
client.subscribe(default_topic)
6663

@@ -76,20 +73,20 @@ def message(client, topic, message):
7673
:param str topic: The topic of the feed with a new value.
7774
:param str message: The new value
7875
"""
79-
print("New message on topic {0}: {1}".format(topic, message))
76+
print(f"New message on topic {topic}: {message}")
8077

8178

8279
# Connect to WiFi
8380
print("Connecting to WiFi...")
84-
wifi.connect()
81+
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
8582
print("Connected!")
8683

8784
# Initialize MQTT interface with the esp interface
8885
MQTT.set_socket(socket, esp)
8986

9087
# Set up a MiniMQTT Client
9188
mqtt_client = MQTT.MQTT(
92-
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
89+
broker="io.adafruit.com", username=aio_username, password=aio_key
9390
)
9491

9592
# Setup the callback methods above
@@ -106,7 +103,7 @@ def message(client, topic, message):
106103
mqtt_client.loop()
107104

108105
# Send a new message
109-
print("Sending photocell value: %d" % photocell_val)
106+
print(f"Sending photocell value: {photocell_val}")
110107
mqtt_client.publish(default_topic, photocell_val)
111108
photocell_val += 1
112109
time.sleep(0.5)

examples/minimqtt_simpletest.py

+45-33
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
import ssl
5-
import socketpool
6-
import wifi
4+
import os
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
710
import adafruit_minimqtt.adafruit_minimqtt as MQTT
811

9-
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
10-
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
11-
# source control.
12-
# pylint: disable=no-name-in-module,wrong-import-order
13-
try:
14-
from secrets import secrets
15-
except ImportError:
16-
print("WiFi secrets are kept in secrets.py, please add them there!")
17-
raise
18-
19-
# Set your Adafruit IO Username and Key in secrets.py
20-
# (visit io.adafruit.com if you need to create an account,
21-
# or if you need your Adafruit IO key.)
22-
aio_username = secrets["aio_username"]
23-
aio_key = secrets["aio_key"]
24-
25-
print("Connecting to %s" % secrets["ssid"])
26-
wifi.radio.connect(secrets["ssid"], secrets["password"])
27-
print("Connected to %s!" % secrets["ssid"])
12+
# Add settings.toml to your filesystem CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD keys
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.
15+
16+
aio_username = os.getenv("aio_username")
17+
aio_key = os.getenv("aio_key")
18+
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)
2842

2943
### Topic Setup ###
3044

3145
# MQTT Topic
3246
# Use this topic if you'd like to connect to a standard MQTT broker
33-
mqtt_topic = "test/topic"
47+
# mqtt_topic = "test/topic"
3448

3549
# Adafruit IO-style Topic
3650
# Use this topic if you'd like to connect to io.adafruit.com
37-
# mqtt_topic = secrets["aio_username"] + '/feeds/temperature'
51+
mqtt_topic = aio_username + "/feeds/temperature"
3852

3953

4054
### Code ###
55+
56+
4157
# Define callback methods which are called when events occur
4258
# pylint: disable=unused-argument, redefined-outer-name
4359
def connect(mqtt_client, userdata, flags, rc):
@@ -69,21 +85,17 @@ def publish(mqtt_client, userdata, topic, pid):
6985

7086

7187
def message(client, topic, message):
72-
# Method called when a client's subscribed feed has a new value.
7388
print("New message on topic {0}: {1}".format(topic, message))
7489

7590

76-
# Create a socket pool
77-
pool = socketpool.SocketPool(wifi.radio)
91+
socket.set_interface(esp)
92+
MQTT.set_socket(socket, esp)
7893

7994
# Set up a MiniMQTT Client
8095
mqtt_client = MQTT.MQTT(
81-
broker=secrets["broker"],
82-
port=secrets["port"],
83-
username=secrets["aio_username"],
84-
password=secrets["aio_key"],
85-
socket_pool=pool,
86-
ssl_context=ssl.create_default_context(),
96+
broker="io.adafruit.com",
97+
username=aio_username,
98+
password=aio_key,
8799
)
88100

89101
# Connect callback handlers to mqtt_client

0 commit comments

Comments
 (0)