-
Notifications
You must be signed in to change notification settings - Fork 0
/
LFSub1.py
78 lines (60 loc) · 1.76 KB
/
LFSub1.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
from time import sleep
from audio.speakText import speakText
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
redPin = 13
greenPin = 19
bluePin = 26
GPIO.setup(redPin,GPIO.OUT)
GPIO.setup(greenPin,GPIO.OUT)
GPIO.setup(bluePin,GPIO.OUT)
FAN_PIN2 = 16
GPIO.setup(FAN_PIN2, GPIO.OUT)
GPIO.output(FAN_PIN2, True)
def turnOff():
GPIO.output(redPin, GPIO.HIGH)
GPIO.output(greenPin, GPIO.HIGH)
GPIO.output(bluePin, GPIO.HIGH)
def white():
GPIO.output(redPin, GPIO.LOW)
GPIO.output(greenPin, GPIO.LOW)
GPIO.output(bluePin, GPIO.LOW)
MQTTBROKER = 'test.mosquitto.org'
PORT = 1883
def on_connect(client, userdata, flags, rc):
print('Connected with result code ' + str(rc))
client.subscribe('home/light')
def on_disconnect(client, userdata, rc):
print('Disconnect with result code' + str(rc))
def on_message(client, userdata, msg):
txt = str(msg.payload)
txt = txt.split("'")[1]
print("txt: ", txt)
if 'Record Voice' in txt:
txt=txt[len('Record Voice'):]
print(txt)
elif txt == "Turn On":
GPIO.output(FAN_PIN2, False)
white()
elif txt == 'Light On':
white()
elif txt == 'Light Off':
turnOff()
elif txt == 'Fan On':
GPIO.output(FAN_PIN2, False)
elif txt == 'Fan Off':
GPIO.output(FAN_PIN2, True)
elif txt == 'Byebye':
turnOff()
GPIO.output(FAN_PIN2, True)
speakText(txt)
print(MQTTBROKER + ': <' + msg.topic + '> :' + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect(MQTTBROKER, PORT)
client.loop_forever()