1
|
import paho.mqtt.client as mqtt
|
2
|
|
3
|
|
4
|
# The callback for when the client receives a CONNACK response from the server.
|
5
|
def on_connect(client, userdata, flags, rc, properties=None):
|
6
|
print("Connected with result code " + str(rc))
|
7
|
# Subscribing in on_connect() means that if we lose the connection and
|
8
|
# reconnect then subscriptions will be renewed.
|
9
|
client.subscribe("$SYS/#")
|
10
|
|
11
|
|
12
|
# The callback for when a PUBLISH message is received from the server.
|
13
|
def on_message(client, userdata, message):
|
14
|
print(message.topic + " " + str(message.payload))
|
15
|
|
16
|
|
17
|
client = mqtt.Client()
|
18
|
client.on_connect = on_connect
|
19
|
client.on_message = on_message
|
20
|
|
21
|
client.connect("localhost", 1883, 60)
|
22
|
|
23
|
# Blocking call that processes network traffic, dispatches callbacks and
|
24
|
# handles reconnecting.
|
25
|
# Other loop*() functions are available that give a threaded interface and a
|
26
|
# manual interface.
|
27
|
client.loop_forever()
|