Skip to content

Commit 4661204

Browse files
committed
updating pub sub examples
1 parent 1ad8085 commit 4661204

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

examples/esp32spi/minimqtt_pub_sub_blocking_esp32spi.py

Lines changed: 15 additions & 13 deletions
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,15 @@
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

55+
5856
# Define callback methods which are called when events occur
5957
# pylint: disable=unused-argument, redefined-outer-name
6058
def connected(client, userdata, flags, rc):
@@ -81,15 +79,15 @@ def message(client, topic, message):
8179

8280
# Connect to WiFi
8381
print("Connecting to WiFi...")
84-
wifi.connect()
82+
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
8583
print("Connected!")
8684

8785
# Initialize MQTT interface with the esp interface
8886
MQTT.set_socket(socket, esp)
8987

9088
# Set up a MiniMQTT Client
9189
mqtt_client = MQTT.MQTT(
92-
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
90+
broker=os.getenv("broker"), username=aio_username, password=aio_key
9391
)
9492

9593
# Setup the callback methods above
@@ -109,7 +107,11 @@ def message(client, topic, message):
109107
mqtt_client.loop()
110108
except (ValueError, RuntimeError) as e:
111109
print("Failed to get data, retrying\n", e)
112-
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+
)
113115
mqtt_client.reconnect()
114116
continue
115117
time.sleep(1)

examples/esp32spi/minimqtt_pub_sub_nonblocking_esp32spi.py

Lines changed: 10 additions & 12 deletions
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,12 @@
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"
52+
5553

5654
### Code ###
5755
# Define callback methods which are called when events occur
@@ -80,15 +78,15 @@ def message(client, topic, message):
8078

8179
# Connect to WiFi
8280
print("Connecting to WiFi...")
83-
wifi.connect()
81+
esp.connect_AP(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
8482
print("Connected!")
8583

8684
# Initialize MQTT interface with the esp interface
8785
MQTT.set_socket(socket, esp)
8886

8987
# Set up a MiniMQTT Client
9088
mqtt_client = MQTT.MQTT(
91-
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
89+
broker=os.getenv("broker"), username=aio_username, password=aio_key
9290
)
9391

9492
# Setup the callback methods above

0 commit comments

Comments
 (0)