Add publishing and subscribing
This commit is contained in:
@@ -17,5 +17,26 @@ mqttc.tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQ
|
|||||||
# Host is just an ip address or hostname of the broker.
|
# Host is just an ip address or hostname of the broker.
|
||||||
mqttc.connect(host, port=1883, keepalive=60, bind_address="")
|
mqttc.connect(host, port=1883, keepalive=60, bind_address="")
|
||||||
|
|
||||||
|
# Topic is a string of the topic to publish to.
|
||||||
|
mqttc.publish(topic, payload=None, qos=0, retain=False)
|
||||||
|
|
||||||
|
# Just subscribe to a topic.
|
||||||
|
mqttc.subscribe(topic, qos=0)
|
||||||
|
|
||||||
|
def on_message(client, userdata, message):
|
||||||
|
'''
|
||||||
|
Do something with the message. Choose based on what the topic is.
|
||||||
|
|
||||||
|
Can also just assign different callback functions for different topics
|
||||||
|
using message_callback_add(sub, callback)
|
||||||
|
'''
|
||||||
|
print("Received message '" + str(message.payload) + "' on topic '"
|
||||||
|
+ message.topic + "' with QoS " + str(message.qos))
|
||||||
|
|
||||||
|
mqttc.on_message = on_message
|
||||||
|
|
||||||
|
# Or for specific callback:
|
||||||
|
# Can use foo/# for all subtopics of foo/, or
|
||||||
|
# +/bar for all topics that include bar.
|
||||||
|
mqttc.message_callback_add(sub, on_message)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user