-
Notifications
You must be signed in to change notification settings - Fork 1.1k
umqtt.robust: let reconnect() call the connect() method of the top class #669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ian-llewellyn
wants to merge
1
commit into
micropython:master
Choose a base branch
from
ian-llewellyn:umqtt.robust.connect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import umqtt.robust | ||
import time | ||
|
||
# Last will and testament (LWT) is commonly used to signal a device as being | ||
# online or offline. This example builds on umqtt.robust to provide a more | ||
# reliable connection with the MQTT broker and signal its status as being | ||
# either Online or Offline. This feature adds to code size and isn't required | ||
# in all circumstances, so hasn't been included by default. | ||
|
||
|
||
class MyMQTTClient(umqtt.robust.MQTTClient): | ||
def connect(self, clean_session=True): | ||
self.set_last_will(b"tele/test/LWT", b"Offline", retain=True) | ||
try: | ||
return super().connect(clean_session) | ||
finally: | ||
self.publish(b"tele/test/LWT", b"Online", retain=True) | ||
|
||
|
||
# Change the server to test on your MQTT broker | ||
c = MyMQTTClient("test_client", "localhost", keepalive=5) | ||
c.DEBUG = True | ||
|
||
c.connect() | ||
|
||
# wait_msg() only returns when a message is received, so this example | ||
# highlights the LWT feature. In practical applications, the broker keeps | ||
# the connection alive only if there is traffic from the client (ping(), etc.) | ||
c.wait_msg() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import umqtt.robust | ||
import time | ||
|
||
# A common expectation of the robust client is that it should re-subscribe to | ||
# topics after a reconnect(). This feature adds to code size and isn't required | ||
# in all circumstances, so hasn't been included by default. | ||
|
||
# You can easily inherit from umqtt.robust.MQTTClient to add this feature... | ||
|
||
|
||
class MyMQTTClient(umqtt.robust.MQTTClient): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.topics = [] | ||
|
||
def connect(self, clean_session=True): | ||
if not super().connect(clean_session): | ||
# Session was not restored - need to resubscribe | ||
for topic in self.topics: | ||
self.subscribe(topic) | ||
|
||
return False # Session was not restored | ||
|
||
return True # Session was restored | ||
|
||
def subscribe(self, topic): | ||
print("Subscribing to", topic) | ||
super().subscribe(topic) | ||
if topic not in self.topics: | ||
self.topics.append(topic) | ||
|
||
|
||
# Change the server to test on your MQTT broker | ||
c = MyMQTTClient("test_client", "localhost", keepalive=5) | ||
c.DEBUG = True | ||
|
||
c.set_callback(print) | ||
|
||
c.connect() | ||
c.subscribe(b"test/topic/a") | ||
|
||
c.publish(b"test/topic/a", b"message 1") | ||
c.wait_msg() | ||
|
||
# Connection breaks once keepalive expires | ||
time.sleep(8) | ||
|
||
c.publish(b"test/topic/a", b"message 2") # publish() doesn't detect OSError, message 2 is lost | ||
c.check_msg() # check_msg() detects OSError and will reconnect() | ||
|
||
c.publish(b"test/topic/a", b"message 3") | ||
c.wait_msg() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.