-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan-control.py
288 lines (254 loc) · 6.96 KB
/
fan-control.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from gpiozero import *
import paho.mqtt.subscribe as subscribe
import paho.mqtt.client as mqtt
import time
remote_power = OutputDevice(10) # master controller
fan_low = OutputDevice(17) # gpio4 - new 17
fan_med = OutputDevice(11) # gpio17 - new 27
fan_high = OutputDevice(22) # gpio18 - new 22
fan_off = OutputDevice(18) # gpio27 - new 18
light = OutputDevice(4) # gpio22 - new 4
dip1 = OutputDevice(23) # gpio5 - new 23
dip2 = OutputDevice(24) # gpio6 - new 24
dip3 = OutputDevice(25) # gpio12 - new 25
dip4 = OutputDevice(8) # gpio13 - new 08
remote_power.off()
fan_low.off()
fan_med.off()
fan_high.off()
light.off()
dip1.off()
dip2.off()
dip3.off()
dip4.off()
# Used for debugging #
'''fan_low = 'fan_low'
fan_med = 'fan_med'
fan_high = 'fan_high'
fan_off = 'fan_off'
light = 'light'
dip1 = 'dip1'
dip2 = 'dip2'
dip3 = 'dip3'
dip4 = 'dip4' '''
#dip_cleared = none
#fan_select
room = []
# living room dip4
# office dip1 dip2
# master none
# wardrobe dip1
# Assisted by: https://www.eclipse.org/paho/clients/python/docs/
# Also youtube video: https://www.youtube.com/watch?v=QAaXNt0oqSI
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
if rc==0:
print("connected OK")
else:
print("Bad connection, returned code=",rc)
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("home/ceilingfans")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
# This prints topic and message payload
print(msg.topic+" "+str(msg.payload))
# This prints message payload with stripped formatting
print(str(str(msg.payload).replace("b", "")).replace("'", ""))
# This saves stripped payload message as a variable
message = (str(str(msg.payload).replace("b", "")).replace("'", ""))
remote_power.on()
# living room
if message == "living room light":
print("living room light")
dip4.on()
light.on()
time.sleep(.5)
light.off()
time.sleep(.5)
dip4.off()
elif message == "living room fan low":
print("living room fan low")
dip4.on()
fan_low.on()
time.sleep(1)
fan_low.off()
time.sleep(1)
dip4.off()
elif message == "living room fan med":
print("living room fan med")
dip4.on()
fan_med.on()
time.sleep(.5)
fan_med.off()
time.sleep(.5)
dip4.off()
elif message == "living room fan high":
print("living room fan high")
dip4.on()
fan_high.on()
time.sleep(.5)
fan_high.off()
time.sleep(.5)
dip4.off()
elif message == "living room fan off":
print("living room fan off")
dip4.on()
fan_off.on()
time.sleep(.5)
fan_off.off()
time.sleep(.5)
dip4.off()
# Master Room
elif message == "master light":
print("master light")
dip1.off()
dip2.off()
dip3.off()
dip4.off()
light.on()
time.sleep(.5)
light.off()
time.sleep(.5)
elif message == "master fan low":
print("master fan low")
fan_low.on()
time.sleep(.5)
fan_low.off()
time.sleep(.5)
elif message == "master fan med":
print("master fan med")
fan_med.on()
time.sleep(.5)
fan_med.off()
time.sleep(.5)
elif message == "master fan high":
print("master fan high")
fan_high.on()
time.sleep(.5)
fan_high.off()
time.sleep(.5)
elif message == "master fan off":
print("master fan off")
fan_off.on()
time.sleep(.5)
fan_off.off()
time.sleep(.5)
# Closet Room
elif message == "closet room light":
print("closet room light")
dip1.on()
light.on()
time.sleep(.5)
light.off()
time.sleep(.5)
dip1.off()
elif message == "closet room fan low":
print("closet room fan low")
dip1.on()
fan_low.on()
time.sleep(.5)
fan_low.off()
time.sleep(.5)
dip1.off()
elif message == "closet room fan med":
print("closet room fan med")
dip1.on()
fan_med.on()
time.sleep(.5)
fan_med.off()
time.sleep(.5)
dip1.off()
elif message == "closet room fan high":
print("closet room fan high")
dip1.on()
fan_high.on()
time.sleep(.5)
fan_high.off()
time.sleep(.5)
dip1.off()
elif message == "closet room fan off":
print("closet room fan off")
dip1.on()
fan_off.on()
time.sleep(.5)
fan_off.off()
time.sleep(.5)
dip1.off()
# Office
elif message == "office light":
print("office light")
dip1.on()
dip2.on()
light.on()
time.sleep(.5)
light.off()
time.sleep(.5)
dip1.off()
dip2.off()
elif message == "office fan low":
print("office fan low")
dip1.on()
dip2.on()
fan_low.on()
time.sleep(.5)
fan_low.off()
time.sleep(.5)
dip1.off()
dip2.off()
elif message == "office fan med":
print("office fan med")
dip1.on()
dip2.on()
fan_med.on()
time.sleep(.5)
fan_med.off()
time.sleep(.5)
dip1.off()
dip2.off()
elif message == "office fan high":
print("office fan high")
dip1.on()
dip2.on()
fan_high.on()
time.sleep(.5)
fan_high.off()
time.sleep(.5)
dip1.off()
dip2.off()
elif message == "office fan off":
print("office fan off")
dip1.on()
dip2.on()
fan_off.on()
time.sleep(.5)
fan_off.off()
time.sleep(.5)
dip1.off()
dip2.off()
else:
print("Unknown Message")
remote_power.off()
#def message_action():
# message1 = on_message
# print("message action print " +str(message1))
# if message1 == "pizza":
# print("Prepare cooking")
# else:
# print("standby")
def on_log(client, userdata, level, buf):
print("log: "+buf)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
#client.message_action = message_action
client.on_log = on_log
client.connect("mqtt_broker_ipaddress_goes_here", 1883, 60)
client.username_pw_set("mqtt_username", "mqtt_password")
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
# Call one of the loop*() functions to maintain network traffic flow with the broker
client.loop_forever()
#client.username_pw_set("mqtt_user", "PASSWORD")