|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# pylint: disable=logging-fstring-interpolation |
| 4 | + |
| 5 | +""" |
| 6 | +Demonstrate on how to use user_data for various callbacks. |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | +import socket |
| 11 | +import ssl |
| 12 | +import sys |
| 13 | + |
| 14 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 15 | + |
| 16 | + |
| 17 | +# pylint: disable=unused-argument |
| 18 | +def on_connect(mqtt_client, user_data, flags, ret_code): |
| 19 | + """ |
| 20 | + connect callback |
| 21 | + """ |
| 22 | + logger = logging.getLogger(__name__) |
| 23 | + logger.debug("Connected to MQTT Broker!") |
| 24 | + logger.debug(f"Flags: {flags}\n RC: {ret_code}") |
| 25 | + |
| 26 | + |
| 27 | +# pylint: disable=unused-argument |
| 28 | +def on_subscribe(mqtt_client, user_data, topic, granted_qos): |
| 29 | + """ |
| 30 | + subscribe callback |
| 31 | + """ |
| 32 | + logger = logging.getLogger(__name__) |
| 33 | + logger.debug(f"Subscribed to {topic} with QOS level {granted_qos}") |
| 34 | + |
| 35 | + |
| 36 | +def on_message(client, topic, message): |
| 37 | + """ |
| 38 | + received message callback |
| 39 | + """ |
| 40 | + logger = logging.getLogger(__name__) |
| 41 | + logger.debug(f"New message on topic {topic}: {message}") |
| 42 | + |
| 43 | + messages = client.user_data |
| 44 | + if not messages.get(topic): |
| 45 | + messages[topic] = [] |
| 46 | + messages[topic].append(message) |
| 47 | + |
| 48 | + |
| 49 | +# pylint: disable=too-many-statements,too-many-locals |
| 50 | +def main(): |
| 51 | + """ |
| 52 | + Main loop. |
| 53 | + """ |
| 54 | + |
| 55 | + logging.basicConfig() |
| 56 | + logger = logging.getLogger(__name__) |
| 57 | + logger.setLevel(logging.DEBUG) |
| 58 | + |
| 59 | + # dictionary/map of topic to list of messages |
| 60 | + messages = {} |
| 61 | + |
| 62 | + # connect to MQTT broker |
| 63 | + mqtt = MQTT.MQTT( |
| 64 | + broker="172.40.0.3", |
| 65 | + port=1883, |
| 66 | + socket_pool=socket, |
| 67 | + ssl_context=ssl.create_default_context(), |
| 68 | + user_data=messages, |
| 69 | + ) |
| 70 | + |
| 71 | + mqtt.on_connect = on_connect |
| 72 | + mqtt.on_subscribe = on_subscribe |
| 73 | + mqtt.on_message = on_message |
| 74 | + |
| 75 | + logger.info("Connecting to MQTT broker") |
| 76 | + mqtt.connect() |
| 77 | + logger.info("Subscribing") |
| 78 | + mqtt.subscribe("foo/#", qos=0) |
| 79 | + mqtt.add_topic_callback("foo/bar", on_message) |
| 80 | + |
| 81 | + i = 0 |
| 82 | + while True: |
| 83 | + i += 1 |
| 84 | + logger.debug(f"Loop {i}") |
| 85 | + # Make sure to stay connected to the broker e.g. in case of keep alive. |
| 86 | + mqtt.loop(1) |
| 87 | + |
| 88 | + for topic, msg_list in messages.items(): |
| 89 | + logger.info(f"Got {len(msg_list)} messages from topic {topic}") |
| 90 | + for msg_cnt, msg in enumerate(msg_list): |
| 91 | + logger.debug(f"#{msg_cnt}: {msg}") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + try: |
| 96 | + main() |
| 97 | + except KeyboardInterrupt: |
| 98 | + sys.exit(0) |
0 commit comments