Skip to content

Commit 1be4557

Browse files
brentrubrentru
authored andcommitted
add two new examples showing blocking vs nonblocking message loops, refactor adafruitiowifi example
1 parent a799f54 commit 1be4557

File tree

3 files changed

+194
-1
lines changed

3 files changed

+194
-1
lines changed

examples/minimqtt_adafruitio_wifi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def on_message(client, topic, message):
7979
photocell_val = 0
8080
while True:
8181
# Poll the message queue
82-
mqtt_client.wait_for_msg()
82+
mqtt_client.loop()
8383

8484
print('Sending photocell value: %d'%photocell_val)
8585
mqtt_client.publish(aio_publish_feed, photocell_val)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# CircuitPython MiniMQTT Library
2+
# Adafruit IO SSL/TLS Example for WiFi (ESP32SPI)
3+
import time
4+
import board
5+
import busio
6+
from digitalio import DigitalInOut
7+
from adafruit_esp32spi import adafruit_esp32spi
8+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
9+
from adafruit_minimqtt import MQTT
10+
11+
### WiFi ###
12+
13+
# Get wifi details and more from a secrets.py file
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+
# If you are using a board with pre-defined ESP32 Pins:
21+
esp32_cs = DigitalInOut(board.ESP_CS)
22+
esp32_ready = DigitalInOut(board.ESP_BUSY)
23+
esp32_reset = DigitalInOut(board.ESP_RESET)
24+
25+
# If you have an externally connected ESP32:
26+
# esp32_cs = DigitalInOut(board.D9)
27+
# esp32_ready = DigitalInOut(board.D10)
28+
# esp32_reset = DigitalInOut(board.D5)
29+
30+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
31+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
32+
33+
### Adafruit IO Setup ###
34+
35+
# Setup a feed named `testfeed` for publishing.
36+
default_topic = secrets['user']+'/feeds/testfeed'
37+
38+
### Code ###
39+
40+
def connect_wifi():
41+
print("Connecting to %s..."%secrets['ssid'])
42+
while not esp.is_connected:
43+
try:
44+
esp.connect_AP(secrets['ssid'], secrets['password'])
45+
except RuntimeError as e:
46+
print("could not connect to AP, retrying: ",e)
47+
continue
48+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
49+
print("IP: ", esp.pretty_ip(esp.ip_address))
50+
51+
# Define callback methods which are called when events occur
52+
# pylint: disable=unused-argument
53+
def connected(client, userdata, flags, rc):
54+
# This function will be called when the client is connected
55+
# successfully to the broker.
56+
print('Connected to MQTT broker! Listening for topic changes on %s'%default_topic)
57+
# Subscribe to all changes on the default_topic feed.
58+
client.subscribe(default_topic)
59+
60+
def disconnected(client, userdata, rc):
61+
# This method is called when the client is disconnected
62+
print('Disconnected from MQTT Broker!')
63+
64+
def message(client, topic, message):
65+
"""Method callled when a client's subscribed feed has a new
66+
value.
67+
:param str topic: The topic of the feed with a new value.
68+
:param str message: The new value
69+
"""
70+
print('New message on topic {0}: {1}'.format(topic, message))
71+
72+
# Connect to WiFi
73+
connect_wifi()
74+
75+
# Initialize a MiniMQTT Client
76+
mqtt_client = MQTT(socket,
77+
broker = secrets['broker'],
78+
username = secrets['user'],
79+
password = secrets['pass'],
80+
esp = esp)
81+
82+
# Setup the callback methods above
83+
mqtt_client.on_connect = connected
84+
mqtt_client.on_disconnect = disconnected
85+
mqtt_client.on_message = message
86+
87+
# Connect the client to the MQTT broker.
88+
mqtt_client.connect()
89+
90+
# Start a blocking message loop
91+
# If you only want to listen to incoming messages,
92+
# you'll want to loop_forever as it handles network reconnections
93+
# No code below this line will execute.
94+
mqtt_client.loop_forever()
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# CircuitPython MiniMQTT Library
2+
# Adafruit IO SSL/TLS Example for WiFi (ESP32SPI)
3+
import time
4+
import board
5+
import busio
6+
from digitalio import DigitalInOut
7+
from adafruit_esp32spi import adafruit_esp32spi
8+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
9+
from adafruit_minimqtt import MQTT
10+
11+
### WiFi ###
12+
13+
# Get wifi details and more from a secrets.py file
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+
# If you are using a board with pre-defined ESP32 Pins:
21+
esp32_cs = DigitalInOut(board.ESP_CS)
22+
esp32_ready = DigitalInOut(board.ESP_BUSY)
23+
esp32_reset = DigitalInOut(board.ESP_RESET)
24+
25+
# If you have an externally connected ESP32:
26+
# esp32_cs = DigitalInOut(board.D9)
27+
# esp32_ready = DigitalInOut(board.D10)
28+
# esp32_reset = DigitalInOut(board.D5)
29+
30+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
31+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
32+
33+
### Adafruit IO Setup ###
34+
35+
# Setup a feed named `testfeed` for publishing.
36+
default_topic = secrets['user']+'/feeds/testfeed'
37+
38+
### Code ###
39+
40+
def connect_wifi():
41+
print("Connecting to %s..."%secrets['ssid'])
42+
while not esp.is_connected:
43+
try:
44+
esp.connect_AP(secrets['ssid'], secrets['password'])
45+
except RuntimeError as e:
46+
print("could not connect to AP, retrying: ",e)
47+
continue
48+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
49+
print("IP: ", esp.pretty_ip(esp.ip_address))
50+
51+
# Define callback methods which are called when events occur
52+
# pylint: disable=unused-argument
53+
def connected(client, userdata, flags, rc):
54+
# This function will be called when the client is connected
55+
# successfully to the broker.
56+
print('Connected to MQTT broker! Listening for topic changes on %s'%default_topic)
57+
# Subscribe to all changes on the default_topic feed.
58+
client.subscribe(default_topic)
59+
60+
def disconnected(client, userdata, rc):
61+
# This method is called when the client is disconnected
62+
print('Disconnected from MQTT Broker!')
63+
64+
def message(client, topic, message):
65+
"""Method callled when a client's subscribed feed has a new
66+
value.
67+
:param str topic: The topic of the feed with a new value.
68+
:param str message: The new value
69+
"""
70+
print('New message on topic {0}: {1}'.format(topic, message))
71+
72+
# Connect to WiFi
73+
connect_wifi()
74+
75+
# Initialize a MiniMQTT Client
76+
mqtt_client = MQTT(socket,
77+
broker = secrets['broker'],
78+
username = secrets['user'],
79+
password = secrets['pass'],
80+
esp = esp)
81+
82+
# Setup the callback methods above
83+
mqtt_client.on_connect = connected
84+
mqtt_client.on_disconnect = disconnected
85+
mqtt_client.on_message = message
86+
87+
# Connect the client to the MQTT broker.
88+
mqtt_client.connect()
89+
90+
photocell_val = 0
91+
while True:
92+
# Poll the message queue
93+
mqtt_client.loop()
94+
95+
# Send a new message
96+
print('Sending photocell value: %d'%photocell_val)
97+
mqtt_client.publish(default_topic, photocell_val)
98+
photocell_val += 1
99+
time.sleep(0.5)

0 commit comments

Comments
 (0)