-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiveMessage.py
More file actions
39 lines (33 loc) · 1.04 KB
/
Copy pathreceiveMessage.py
File metadata and controls
39 lines (33 loc) · 1.04 KB
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
33
34
35
36
37
38
39
import paho.mqtt.client as mqtt
import time
#HOST = "127.0.0.1"
PORT = 1883
TOPIC = "TEST"
HOST = input("Enter Brokder IP Address >> ")
#PORT = int(input("Enter Port Number >> "))
#TOPIC = input("Topic to Subscribe >> ")
def client_loop():
topic = TOPIC
client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
client = mqtt.Client(client_id)
client.username_pw_set("admin", "123456")
client.on_connect = on_connect
client.on_message = on_message
client.connect(HOST, PORT, 60)
'''
try:
client.connect(HOST, PORT, 60)
except:
HOST = input("Enter Brokder IP Address >> ")
PORT = int(input("Enter Port Number >> "))
topic = TOPIC
client_loop()
'''
client.loop_forever()
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(TOPIC)
def on_message(client, userdata, msg):
print(msg.payload.decode("utf-8"))
if __name__ == '__main__':
client_loop()