Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'''
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