Skip to content

Commit a8b1af9

Browse files
author
brentru
committed
add new example
1 parent dda9157 commit a8b1af9

File tree

3 files changed

+71
-79
lines changed

3 files changed

+71
-79
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55
# Original Work Copyright (c) 2016 Paul Sokolovsky, uMQTT
66
# Modified Work Copyright (c) 2019 Bradley Beach, esp32spi_mqtt
7+
# Modified Work Copyright (c) 2012-2019 Roger Light and others, Paho MQTT Python
78
#
89
# Permission is hereby granted, free of charge, to any person obtaining a copy
910
# of this software and associated documentation files (the "Software"), to deal
@@ -257,10 +258,10 @@ def _handle_on_message(self, client, topic, message):
257258
matched = False
258259
if topic is not None:
259260
for callback in self._on_message_filtered.iter_match(topic):
260-
callback(client, topic, message) # on_msg with callback
261+
callback(client, topic, message) # on_msg with callback
261262
matched = True
262263

263-
if not matched and self.on_message: # regular on_message
264+
if not matched and self.on_message: # regular on_message
264265
self.on_message(client, topic, message)
265266

266267
# pylint: disable=too-many-branches, too-many-statements, too-many-locals

adafruit_minimqtt/matcher.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@
1313
* Author(s): Yotch (https://github.com/yoch)
1414
"""
1515

16-
class MQTTMatcher(object):
16+
17+
class MQTTMatcher:
1718
"""Intended to manage topic filters including wildcards.
1819
19-
Internally, MQTTMatcher use a prefix tree (trie) to store
20-
values associated with filters, and has an iter_match()
21-
method to iterate efficiently over all filters that match
22-
some topic name."""
20+
Internally, MQTTMatcher use a prefix tree (trie) to store
21+
values associated with filters, and has an iter_match()
22+
method to iterate efficiently over all filters that match
23+
some topic name.
24+
"""
25+
26+
# pylint: disable=too-few-public-methods
27+
class Node:
28+
"""Individual node on the MQTT prefix tree.
29+
"""
2330

24-
class Node(object):
25-
__slots__ = '_children', '_content'
31+
__slots__ = "children", "content"
2632

2733
def __init__(self):
28-
self._children = {}
29-
self._content = None
34+
self.children = {}
35+
self.content = None
3036

3137
def __init__(self):
3238
self._root = self.Node()
@@ -35,19 +41,19 @@ def __setitem__(self, key, value):
3541
"""Add a topic filter :key to the prefix tree
3642
and associate it to :value"""
3743
node = self._root
38-
for sym in key.split('/'):
39-
node = node._children.setdefault(sym, self.Node())
40-
node._content = value
44+
for sym in key.split("/"):
45+
node = node.children.setdefault(sym, self.Node())
46+
node.content = value
4147

4248
def __getitem__(self, key):
4349
"""Retrieve the value associated with some topic filter :key"""
4450
try:
4551
node = self._root
46-
for sym in key.split('/'):
47-
node = node._children[sym]
48-
if node._content is None:
52+
for sym in key.split("/"):
53+
node = node.children[sym]
54+
if node.content is None:
4955
raise KeyError(key)
50-
return node._content
56+
return node.content
5157
except KeyError:
5258
raise KeyError(key)
5359

@@ -56,38 +62,39 @@ def __delitem__(self, key):
5662
lst = []
5763
try:
5864
parent, node = None, self._root
59-
for k in key.split('/'):
60-
parent, node = node, node._children[k]
61-
lst.append((parent, k, node))
62-
# TODO
63-
node._content = None
65+
for k in key.split("/"):
66+
parent, node = node, node.children[k]
67+
lst.append((parent, k, node))
68+
node.content = None
6469
except KeyError:
6570
raise KeyError(key)
6671
else: # cleanup
6772
for parent, k, node in reversed(lst):
68-
if node._children or node._content is not None:
69-
break
70-
del parent._children[k]
73+
if node.children or node.content is not None:
74+
break
75+
del parent.children[k]
7176

7277
def iter_match(self, topic):
73-
"""Return an iterator on all values associated with filters
78+
"""Return an iterator on all values associated with filters
7479
that match the :topic"""
75-
lst = topic.split('/')
76-
normal = not topic.startswith('$')
80+
lst = topic.split("/")
81+
normal = not topic.startswith("$")
82+
7783
def rec(node, i=0):
7884
if i == len(lst):
79-
if node._content is not None:
80-
yield node._content
85+
if node.content is not None:
86+
yield node.content
8187
else:
8288
part = lst[i]
83-
if part in node._children:
84-
for content in rec(node._children[part], i + 1):
89+
if part in node.children:
90+
for content in rec(node.children[part], i + 1):
8591
yield content
86-
if '+' in node._children and (normal or i > 0):
87-
for content in rec(node._children['+'], i + 1):
92+
if "+" in node.children and (normal or i > 0):
93+
for content in rec(node.children["+"], i + 1):
8894
yield content
89-
if '#' in node._children and (normal or i > 0):
90-
content = node._children['#']._content
95+
if "#" in node.children and (normal or i > 0):
96+
content = node.children["#"].content
9197
if content is not None:
9298
yield content
93-
return rec(self._root)
99+
100+
return rec(self._root)

examples/minimqtt_pub_sub_multiple_callbacks.py renamed to examples/minimqtt_pub_sub_blocking_topic_callbacks.py

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717
print("WiFi secrets are kept in secrets.py, please add them there!")
1818
raise
1919

20-
esp32_cs = DigitalInOut(board.D13)
21-
esp32_ready = DigitalInOut(board.D11)
22-
esp32_reset = DigitalInOut(board.D12)
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)
2329

2430
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2531
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
@@ -38,30 +44,18 @@
3844
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
3945
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
4046

41-
### Topic Setup ###
42-
43-
# MQTT Topic
44-
# Use this topic if you'd like to connect to a standard MQTT broker
45-
mqtt_topic = "test/topic"
46-
47-
# Adafruit IO-style Topic
48-
# Use this topic if you'd like to connect to io.adafruit.com
49-
# mqtt_topic = 'aio_user/feeds/temperature'
50-
5147
### Code ###
5248

5349
# Define callback methods which are called when events occur
5450
# pylint: disable=unused-argument, redefined-outer-name
55-
def connect(client, userdata, flags, rc):
51+
def connected(client, userdata, flags, rc):
5652
# This function will be called when the client is connected
5753
# successfully to the broker.
5854
print("Connected to MQTT Broker!")
59-
print("Flags: {0}\n RC: {1}".format(flags, rc))
6055

6156

62-
def disconnect(client, userdata, rc):
63-
# This method is called when the client disconnects
64-
# from the broker.
57+
def disconnected(client, userdata, rc):
58+
# This method is called when the client is disconnected
6559
print("Disconnected from MQTT Broker!")
6660

6761

@@ -75,19 +69,15 @@ def unsubscribe(client, userdata, topic, pid):
7569
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
7670

7771

78-
def publish(client, userdata, topic, pid):
79-
# This method is called when the client publishes data to a feed.
80-
print("Published to {0} with PID {1}".format(topic, pid))
81-
8272
def on_battery_msg(client, topic, message):
83-
print("Battery Level: {}v".format(message))
73+
# Method called when device/batteryLife has a new value
74+
print("Battery level: {}v".format(message))
75+
76+
# client.remove_topic_callback("device/batteryLevel")
77+
8478

8579
def on_message(client, topic, message):
86-
"""Method callled when a client's subscribed feed has a new
87-
value.
88-
:param str topic: The topic of the feed with a new value.
89-
:param str message: The new value
90-
"""
80+
# Method callled when a client's subscribed feed has a new value.
9181
print("New message on topic {0}: {1}".format(topic, message))
9282

9383

@@ -96,32 +86,28 @@ def on_message(client, topic, message):
9686
wifi.connect()
9787
print("Connected!")
9888

99-
# Initialize MQTT interface with the esp interface
10089
MQTT.set_socket(socket, esp)
10190

10291
# Set up a MiniMQTT Client
103-
client = MQTT.MQTT(broker=secrets["broker"], port=1883)
92+
client = MQTT.MQTT(broker=secrets["broker"], port=secrets["broker_port"])
10493

105-
# Connect callback handlers to client
106-
client.on_connect = connect
107-
client.on_disconnect = disconnect
94+
# Setup the callback methods above
95+
client.on_connect = connected
96+
client.on_disconnect = disconnected
10897
client.on_subscribe = subscribe
10998
client.on_unsubscribe = unsubscribe
110-
client.on_publish = publish
111-
11299
client.on_message = on_message
113-
client.add_topic_callback("sensors/batteryLevel", on_battery_msg)
100+
client.add_topic_callback("device/batteryLevel", on_battery_msg)
114101

115102
# Connect the client to the MQTT broker.
116103
print("Connecting to MQTT broker...")
117104
client.connect()
118105

119-
# Subscribe to all notifications on the sensors/
120-
client.subscribe("sensors/#", 1)
106+
# Subscribe to all notifications on the device/ topic
107+
client.subscribe("device/#", 1)
121108

122109
# Start a blocking message loop...
123110
# NOTE: NO code below this loop will execute
124-
# NOTE: Network reconnection is handled within this loop
125111
while True:
126112
try:
127113
client.loop()
@@ -131,5 +117,3 @@ def on_message(client, topic, message):
131117
client.reconnect()
132118
continue
133119
time.sleep(1)
134-
135-

0 commit comments

Comments
 (0)