-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmqtt_led_client.py
43 lines (35 loc) · 1.03 KB
/
mqtt_led_client.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 python
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
def on_connect(client, userdata, rc):
#print ("Connected with rc: " + str(rc))
client.subscribe("kwf/demo/led")
def on_message(client, userdata, msg):
#print ("Topic: "+ msg.topic+"\nMessage: "+str(msg.payload))
if "green" in msg.payload:
#print(" Green on!")
GPIO.output(11, True)
else:
#print(" Green off!")
GPIO.output(11, False)
if "yellow" in msg.payload:
#print(" Yellow on!")
GPIO.output(12, True)
else:
#print(" Yellow off!")
GPIO.output(12, False)
if "red" in msg.payload:
#print(" Red on!")
GPIO.output(13, True)
else:
#print(" Red off!")
GPIO.output(13, False)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("test.mosquitto.org", 1883, 60)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
client.loop_forever()