-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_subscribe.py
43 lines (34 loc) · 950 Bytes
/
mqtt_subscribe.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
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
MOTOR = "MOTOR_CONTROL"
AUTO = "AUTONOMOUS"
FORWARD = "FORWARD"
BACKWARD = "BACKWARD"
LEFT = "LEFT"
RIGHT = "RIGHT"
STOP = "STOP"
# This is the Subscriber
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(MOTOR)
def on_message(client, userdata, msg):
if msg.payload.decode() == AUTO:
print("Autonomous mode")
elif msg.payload.decode() == FORWARD:
print("Forward")
elif msg.payload.decode() == BACKWARD:
print("Backward")
elif msg.payload.decode() == LEFT:
print("Left")
elif msg.payload.decode() == RIGHT:
print("Right")
elif msg.payload.decode() == STOP:
print("Stop")
else:
print ("Invalid Key")
#client.disconnect()
client = mqtt.Client()
client.connect("localhost",1883,60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()