2015-06-30 20:48:15 +00:00
|
|
|
import logging
|
|
|
|
import asyncio
|
|
|
|
|
2015-07-07 19:55:17 +00:00
|
|
|
from hbmqtt.client import MQTTClient
|
|
|
|
|
|
|
|
|
2015-07-06 20:20:05 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# This sample shows how to subscbribe a topic and receive data from incoming messages
|
|
|
|
# It subscribes to '$SYS/broker/uptime' topic and displays the first ten values returned
|
|
|
|
# by the broker.
|
|
|
|
#
|
|
|
|
|
2015-06-30 20:48:15 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
C = MQTTClient()
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2015-07-21 19:41:46 +00:00
|
|
|
def uptime_coro():
|
2015-08-02 14:56:50 +00:00
|
|
|
yield from C.connect('mqtt://test.mosquitto.org:1883/')
|
2015-07-06 20:20:05 +00:00
|
|
|
# Subscribe to '$SYS/broker/uptime' with QOS=1
|
|
|
|
yield from C.subscribe([
|
2015-07-05 20:27:34 +00:00
|
|
|
{'filter': '$SYS/broker/uptime', 'qos': 0x01},
|
2015-07-26 19:21:35 +00:00
|
|
|
{'filter': '$SYS/broker/load/#', 'qos': 0x02},
|
2015-06-30 20:48:15 +00:00
|
|
|
])
|
|
|
|
logger.info("Subscribed")
|
2015-07-26 19:21:35 +00:00
|
|
|
for i in range(1, 100):
|
|
|
|
packet = yield from C.deliver_message()
|
|
|
|
print("%d %s : %s" % (i, packet.variable_header.topic_name, str(packet.payload.data)))
|
|
|
|
yield from C.acknowledge_delivery(packet.variable_header.packet_id)
|
2015-07-05 20:00:15 +00:00
|
|
|
yield from C.unsubscribe(['$SYS/broker/uptime'])
|
|
|
|
logger.info("UnSubscribed")
|
2015-06-30 20:48:15 +00:00
|
|
|
yield from C.disconnect()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
formatter = "[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
2015-07-06 20:20:05 +00:00
|
|
|
logging.basicConfig(level=logging.INFO, format=formatter)
|
2015-07-21 19:41:46 +00:00
|
|
|
asyncio.get_event_loop().run_until_complete(uptime_coro())
|