@@ -91,9 +91,9 @@ class MMQTTException(Exception):
91
91
92
92
def set_socket (sock , iface = None ):
93
93
"""Helper to set the global socket and optionally set the global network interface.
94
+
94
95
:param sock: socket object.
95
96
:param iface: internet interface object
96
-
97
97
"""
98
98
global _the_sock # pylint: disable=invalid-name, global-statement
99
99
_the_sock = sock
@@ -105,6 +105,7 @@ def set_socket(sock, iface=None):
105
105
106
106
class MQTT :
107
107
"""MQTT Client for CircuitPython
108
+
108
109
:param str broker: MQTT Broker URL or IP Address.
109
110
:param int port: Optional port definition, defaults to 8883.
110
111
:param str username: Username for broker authentication.
@@ -114,7 +115,6 @@ class MQTT:
114
115
:param bool is_ssl: Sets a secure or insecure connection with the broker.
115
116
:param bool log: Attaches a logger to the MQTT client, defaults to logging level INFO.
116
117
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
117
-
118
118
"""
119
119
120
120
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
@@ -201,11 +201,11 @@ def deinit(self):
201
201
202
202
def will_set (self , topic = None , payload = None , qos = 0 , retain = False ):
203
203
"""Sets the last will and testament properties. MUST be called before connect().
204
+
204
205
:param str topic: MQTT Broker topic.
205
206
:param str payload: Last will disconnection payload.
206
207
:param int qos: Quality of Service level.
207
208
:param bool retain: Specifies if the payload is to be retained when it is published.
208
-
209
209
"""
210
210
if self .logger is not None :
211
211
self .logger .debug ("Setting last will properties" )
@@ -225,18 +225,18 @@ def will_set(self, topic=None, payload=None, qos=0, retain=False):
225
225
226
226
def add_topic_callback (self , mqtt_topic , callback_method ):
227
227
"""Registers a callback_method for a specific MQTT topic.
228
+
228
229
:param str mqtt_topic: MQTT topic.
229
230
:param str callback_method: Name of callback method.
230
-
231
231
"""
232
232
if mqtt_topic is None or callback_method is None :
233
233
raise ValueError ("MQTT topic and callback method must both be defined." )
234
234
self ._on_message_filtered [mqtt_topic ] = callback_method
235
235
236
236
def remove_topic_callback (self , mqtt_topic ):
237
237
"""Removes a registered callback method.
238
- :param str mqtt_topic: MQTT topic.
239
238
239
+ :param str mqtt_topic: MQTT topic.
240
240
"""
241
241
if mqtt_topic is None :
242
242
raise ValueError ("MQTT Topic must be defined." )
@@ -271,8 +271,8 @@ def _handle_on_message(self, client, topic, message):
271
271
# pylint: disable=too-many-branches, too-many-statements, too-many-locals
272
272
def connect (self , clean_session = True ):
273
273
"""Initiates connection with the MQTT Broker.
274
- :param bool clean_session: Establishes a persistent session.
275
274
275
+ :param bool clean_session: Establishes a persistent session.
276
276
"""
277
277
self ._sock = _the_sock .socket ()
278
278
self ._sock .settimeout (15 )
@@ -411,6 +411,7 @@ def ping(self):
411
411
# pylint: disable=too-many-branches, too-many-statements
412
412
def publish (self , topic , msg , retain = False , qos = 0 ):
413
413
"""Publishes a message to a topic provided.
414
+
414
415
:param str topic: Unique topic identifier.
415
416
:param str msg: Data to send to the broker.
416
417
:param int msg: Data to send to the broker.
@@ -419,16 +420,19 @@ def publish(self, topic, msg, retain=False, qos=0):
419
420
:param int qos: Quality of Service level for the message.
420
421
421
422
Example of sending an integer, 3, to the broker on topic 'piVal'.
423
+
422
424
.. code-block:: python
423
425
424
426
mqtt_client.publish('topics/piVal', 3)
425
427
426
428
Example of sending a float, 3.14, to the broker on topic 'piVal'.
429
+
427
430
.. code-block:: python
428
431
429
432
mqtt_client.publish('topics/piVal', 3.14)
430
433
431
434
Example of sending a string, 'threepointonefour', to the broker on topic piVal.
435
+
432
436
.. code-block:: python
433
437
434
438
mqtt_client.publish('topics/piVal', 'threepointonefour')
@@ -509,27 +513,32 @@ def publish(self, topic, msg, retain=False, qos=0):
509
513
def subscribe (self , topic , qos = 0 ):
510
514
"""Subscribes to a topic on the MQTT Broker.
511
515
This method can subscribe to one topics or multiple topics.
516
+
512
517
:param str topic: Unique MQTT topic identifier.
513
518
:param int qos: Quality of Service level for the topic, defaults to zero.
514
519
:param tuple topic: Tuple containing topic identifier strings and qos level integers.
515
520
:param list topic: List of tuples containing topic identifier strings and qos.
516
521
517
522
Example of subscribing a topic string.
523
+
518
524
.. code-block:: python
519
525
520
526
mqtt_client.subscribe('topics/ledState')
521
527
522
528
Example of subscribing to a topic and setting the qos level to 1.
529
+
523
530
.. code-block:: python
524
531
525
532
mqtt_client.subscribe('topics/ledState', 1)
526
533
527
534
Example of subscribing to topic string and setting qos level to 1, as a tuple.
535
+
528
536
.. code-block:: python
529
537
530
538
mqtt_client.subscribe(('topics/ledState', 1))
531
539
532
540
Example of subscribing to multiple topics with different qos levels.
541
+
533
542
.. code-block:: python
534
543
535
544
mqtt_client.subscribe([('topics/ledState', 1), ('topics/servoAngle', 0)])
@@ -583,15 +592,18 @@ def subscribe(self, topic, qos=0):
583
592
584
593
def unsubscribe (self , topic ):
585
594
"""Unsubscribes from a MQTT topic.
595
+
586
596
:param str topic: Unique MQTT topic identifier.
587
597
:param list topic: List of tuples containing topic identifier strings.
588
598
589
599
Example of unsubscribing from a topic string.
600
+
590
601
.. code-block:: python
591
602
592
603
mqtt_client.unsubscribe('topics/ledState')
593
604
594
605
Example of unsubscribing from multiple topics.
606
+
595
607
.. code-block:: python
596
608
597
609
mqtt_client.unsubscribe([('topics/ledState'), ('topics/servoAngle')])
@@ -645,6 +657,7 @@ def unsubscribe(self, topic):
645
657
646
658
def reconnect (self , resub_topics = True ):
647
659
"""Attempts to reconnect to the MQTT broker.
660
+
648
661
:param bool resub_topics: Resubscribe to previously subscribed topics.
649
662
"""
650
663
if self .logger is not None :
@@ -672,7 +685,6 @@ def loop_forever(self):
672
685
next major release. Please see examples/minimqtt_pub_sub_blocking.py
673
686
for an example of creating a blocking loop which can handle wireless
674
687
network events.
675
-
676
688
"""
677
689
while True :
678
690
if self ._sock .connected :
@@ -681,7 +693,6 @@ def loop_forever(self):
681
693
def loop (self ):
682
694
"""Non-blocking message loop. Use this method to
683
695
check incoming subscription messages.
684
-
685
696
"""
686
697
if self ._timestamp == 0 :
687
698
self ._timestamp = time .monotonic ()
@@ -744,6 +755,7 @@ def _recv_len(self):
744
755
745
756
def _send_str (self , string ):
746
757
"""Packs and encodes a string to a socket.
758
+
747
759
:param str string: String to write to the socket.
748
760
"""
749
761
self ._sock .send (struct .pack ("!H" , len (string )))
@@ -755,6 +767,7 @@ def _send_str(self, string):
755
767
@staticmethod
756
768
def _check_topic (topic ):
757
769
"""Checks if topic provided is a valid mqtt topic.
770
+
758
771
:param str topic: Topic identifier
759
772
"""
760
773
if topic is None :
@@ -769,6 +782,7 @@ def _check_topic(topic):
769
782
@staticmethod
770
783
def _check_qos (qos_level ):
771
784
"""Validates the quality of service level.
785
+
772
786
:param int qos_level: Desired QoS level.
773
787
"""
774
788
if isinstance (qos_level , int ):
@@ -803,6 +817,7 @@ def mqtt_msg(self):
803
817
@mqtt_msg .setter
804
818
def mqtt_msg (self , msg_size ):
805
819
"""Sets the maximum MQTT message payload size.
820
+
806
821
:param int msg_size: Maximum MQTT payload size.
807
822
"""
808
823
if msg_size < MQTT_MSG_MAX_SZ :
@@ -811,13 +826,15 @@ def mqtt_msg(self, msg_size):
811
826
### Logging ###
812
827
def attach_logger (self , logger_name = "log" ):
813
828
"""Initializes and attaches a logger to the MQTTClient.
829
+
814
830
:param str logger_name: Name of the logger instance
815
831
"""
816
832
self .logger = logging .getLogger (logger_name )
817
833
self .logger .setLevel (logging .INFO )
818
834
819
835
def set_logger_level (self , log_level ):
820
836
"""Sets the level of the logger, if defined during init.
837
+
821
838
:param string log_level: Level of logging to output to the REPL.
822
839
"""
823
840
if self .logger is None :
0 commit comments