-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmqtt_subscriber.py
32 lines (22 loc) · 1.13 KB
/
mqtt_subscriber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import paho.mqtt.client as mqtt
import time
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe([("ep_mqtt/test", 1), ("ep_mqtt/topic2", 1), ("ep_mqtt/topic3", 1)])
def on_message(client, userdata, message):
print("Message received: " + message.topic + " : " + str(message.payload))
if message.topic == 'ep_mqtt/topic2':
with open('/home/pi/mqtt_update.txt', 'a+') as f:
f.write("received topic2")
broker_address = "localhost" # Broker address
port = 1883 # Broker port
# user = "yourUser" #Connection username
# password = "yourPassword" #Connection password
client = mqtt.Client() # create new instance
# client.username_pw_set(user, password=password) #set username and password
client.on_connect = on_connect # attach function to callback
client.on_message = on_message # attach function to callback
client.connect(broker_address, port=port) # connect to broker
client.loop_forever()