kopia lustrzana https://github.com/micropython/micropython-lib
Porównaj commity
3 Commity
793942f74b
...
61ea634065
Autor | SHA1 | Data |
---|---|---|
Ian Cotter-Llewellyn | 61ea634065 | |
Damien George | 45ead11f96 | |
puuu | 1d1f6acf1a |
|
@ -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()
|
|
@ -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()
|
|
@ -1,5 +1,5 @@
|
|||
metadata(
|
||||
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.2"
|
||||
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.3"
|
||||
)
|
||||
|
||||
# Originally written by Paul Sokolovsky.
|
||||
|
|
|
@ -20,7 +20,9 @@ class MQTTClient(simple.MQTTClient):
|
|||
i = 0
|
||||
while 1:
|
||||
try:
|
||||
return super().connect(False)
|
||||
# self.connect will call a subclass definition (if any)
|
||||
# else fall back to parent class definition
|
||||
return self.connect(False)
|
||||
except OSError as e:
|
||||
self.log(True, e)
|
||||
i += 1
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
metadata(version="0.2.0")
|
||||
metadata(version="0.2.1")
|
||||
|
||||
module("ssl.py", opt=3)
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
import tls
|
||||
from tls import (
|
||||
CERT_NONE,
|
||||
CERT_OPTIONAL,
|
||||
CERT_REQUIRED,
|
||||
MBEDTLS_VERSION,
|
||||
PROTOCOL_TLS_CLIENT,
|
||||
PROTOCOL_TLS_SERVER,
|
||||
)
|
||||
from tls import *
|
||||
|
||||
|
||||
class SSLContext:
|
||||
|
|
Ładowanie…
Reference in New Issue