Created
February 24, 2019 07:44
-
-
Save wwj718/1d22f96880f27a43495af0b78ac545a8 to your computer and use it in GitHub Desktop.
This file contains 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
''' | |
IoT: Internet of Things | |
server : iot.codelab.club | |
# tool: | |
pip3 install hbmqtt # 0.9.5 | |
hbmqtt_pub --url mqtt://guest:test@iot.codelab.club -t "/scratch3_sub" -m "hello from hbmqtt_pub" | |
hbmqtt_sub --url mqtt://guest:test@iot.codelab.club -t "/scratch3_pub" | |
''' | |
import logging | |
import asyncio | |
from hbmqtt.client import MQTTClient, ClientException | |
from hbmqtt.mqtt.constants import QOS_0 | |
username = "guest" | |
password = "test" | |
mqtt_url = "mqtt://{}:{}@iot.codelab.club".format(username, password) | |
async def uptime_coro(): | |
C = MQTTClient() # clientid? | |
await C.connect(mqtt_url) | |
# Subscribe to '$SYS/broker/uptime' with QOS=1 | |
# Subscribe to '$SYS/broker/load/#' with QOS=2 | |
await C.subscribe([ | |
('/scratch3_pub', QOS_0), | |
]) | |
try: | |
while True: | |
message = await C.deliver_message() | |
topic = message.topic | |
payload = message.data.decode() | |
print("%s => %s" % (topic, payload)) | |
await C.unsubscribe(['/scratch3_sub']) | |
await C.disconnect() | |
except ClientException as ce: | |
logging.error("Client exception: %s" % ce) | |
if __name__ == '__main__': | |
asyncio.get_event_loop().run_until_complete(uptime_coro()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment