-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled-pwm-pubnub-server.py
420 lines (302 loc) · 9.1 KB
/
led-pwm-pubnub-server.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*-
from pubnub import Pubnub
import RPi.GPIO as GPIO
import time
import sys
import math
import threading
import datetime
import subprocess
import re
import os
#Global constants
SECONDS_IN_ONE_MINUTE = 60
SECONDS_IN_ONE_HOUR = 60 * SECONDS_IN_ONE_MINUTE
#General settings
debug = False
#Model
gpio_bcm_pin = 25
#PWM driver
p = None
speed_step_percentage = 10
max_speed_percentage = 70
current_speed_percentage = 0
speed_step_inc_pause = 1
speed_step_dec_pause = 1
autostop_timer = None
is_running = False
is_changing_speed = False
last_started_at = None
last_stopped_at = None
max_exercise_time = SECONDS_IN_ONE_HOUR
def pump_init():
GPIO.setup(gpio_bcm_pin, GPIO.OUT)
GPIO.output(gpio_bcm_pin, GPIO.HIGH)
#PWM driver
#global p
#p = GPIO.PWM(gpio_bcm_pin, 50)
def pump_start():
global is_running, last_started_at
if (is_running):
print('pump is already started')
return
print('starting water pump...')
#Relay driver
GPIO.output(gpio_bcm_pin, GPIO.LOW)
#PWM driver
#p.start(0)
#pump_change_speed_linear(max_speed_percentage)
last_started_at = datetime.datetime.utcnow()
is_running = True
def pump_stop():
global is_running, last_stopped_at
if (is_running == False):
print('pump is already stopped')
return
#Relay driver
GPIO.output(gpio_bcm_pin, GPIO.HIGH)
#PWM driver
#pump_change_speed_linear(0)
#p.stop()
print('water pump stopped')
last_stopped_at = datetime.datetime.utcnow()
is_running = False
def pump_change_speed_linear(new_speed_percentage):
#Relay driver
print('water pump driver is not PWM, is on/off')
return
#PWM driver
if (is_running == False):
print('cannot change pump speed because it is stopped')
return
if (new_speed_percentage == current_speed_percentage):
print('pump speed is already ' + str(new_speed_percentage) + '%')
return
global is_changing_speed
if (is_changing_speed):
print('pump is already changing speed')
return
is_changing_speed = True
if (new_speed_percentage < current_speed_percentage):
pump_dec_speed_linear(new_speed_percentage)
else:
pump_inc_speed_linear(new_speed_percentage)
is_changing_speed = False
def pump_inc_speed_linear(target_speed_percentage):
for new_speed_percentage in range(current_speed_percentage + speed_step_percentage, target_speed_percentage, speed_step_percentage):
pump_set_speed(new_speed_percentage)
time.sleep(speed_step_inc_pause)
pump_set_speed(target_speed_percentage)
def pump_dec_speed_linear(target_speed_percentage):
for new_speed_percentage in range(current_speed_percentage - speed_step_percentage, target_speed_percentage, -speed_step_percentage):
pump_set_speed(new_speed_percentage)
time.sleep(speed_step_dec_pause)
pump_set_speed(target_speed_percentage)
def pump_set_speed(percentage):
p.ChangeDutyCycle(percentage)
print('water pump speed set to ' + str(percentage) + '%')
global current_speed_percentage
current_speed_percentage = percentage
def autostop_timer_activate():
global autostop_timer
if (autostop_timer != None):
print('autostop timer already activated')
return
def callback():
print('automatically stopping water pump...')
pump_stop()
print('activating autostop timer to fire in ' + str(max_exercise_time) + 's...')
autostop_timer = threading.Timer(max_exercise_time, callback)
autostop_timer.start()
def autostop_timer_deactivate():
global autostop_timer
if (autostop_timer == None):
print('autostop timer already deactivated')
return
autostop_timer.cancel()
autostop_timer = None
print('autostop timer deactivated')
#Controllers
def start():
print('manually starting water pump...')
#TODO: move to model
autostop_timer_activate()
pump_start()
def set_speed(percentage):
print('manually changing water pump speed to ' + str(percentage) + '...')
pump_change_speed_linear(percentage)
def stop():
print('manually stopping water pump...')
pump_stop()
#TODO: move to model
autostop_timer_deactivate()
def status():
last_start = None
if (last_started_at != None):
last_start = last_started_at.isoformat()
last_stop = None
if (last_stopped_at != None):
last_stop = last_stopped_at.isoformat()
pubnub.publish(
channel = 'status',
message = {
'resource': 'water_pump',
'operation': 'status',
'params': {
'is_running': is_running,
'max_exercise_time': max_exercise_time,
'last_started_at': last_start,
'last_stopped_at': last_stop
}
}
)
def pi_status():
dir_path = os.path.dirname(os.path.realpath(__file__))
script_path = dir_path + "/pi-temps.sh"
try:
bytes_output = subprocess.check_output(script_path)
string_output = bytes_output.decode("utf-8")
lines = string_output.splitlines()
cpu_temp_string = lines[0].strip()
gpu_temp_string = lines[1].strip()
cpu_freq_string = lines[2].strip()
cpu_temp_match = re.search('\d+\.\d', cpu_temp_string)
gpu_temp_match = re.search('\d+\.\d', gpu_temp_string)
cpu_freq_match = re.search('\d+', cpu_freq_string)
cpu_temp = cpu_temp_match.group()
gpu_temp = gpu_temp_match.group()
cpu_freq = cpu_freq_match.group()
pubnub.publish(
channel = 'status',
message = {
'resource': 'pi',
'operation': 'status',
'params': {
'cpu_temp': cpu_temp,
'gpu_temp': gpu_temp,
'cpu_freq': cpu_freq
}
}
)
except FileNotFoundError:
print('cannot find "' + script_path + '"')
except subprocess.CalledProcessError:
print('execution error "' + script_path + '"')
def pi_shutdown():
try:
bytes_output = subprocess.check_output(["sudo", "shutdown", "--poweroff"])
string_output = bytes_output.decode("utf-8")
pubnub.publish(
channel = 'status',
message = {
'resource': 'pi',
'operation': 'shutdown',
'params': {
'wall_message': string_output
}
}
)
except FileNotFoundError:
print('cannot find shutdonwn binary')
except subprocess.CalledProcessError:
print('shutdown execution error')
def heartbeat():
pubnub.publish(
channel = 'status',
message = {
'resource': 'heartbeat',
'operation': 'status',
'params': 'good'
}
)
dispatcher = {
'water_pump': {
'start': start,
'set_speed': set_speed,
'stop': stop,
'status': status
},
'heartbeat': {
'status': heartbeat
},
'pi': {
'status': pi_status,
'shutdown': pi_shutdown
}
}
#Routing
def route(request):
resource = request['resource']
operation = request['operation']
params = request['params']
if resource not in dispatcher:
print('Cannot find resource "' + resource + '"')
return
if operation not in dispatcher[resource]:
print('Cannot find operation "' + operation + '" for resource "' + resource + '"')
return
controller = dispatcher[resource][operation]
if (params != None):
controller(params)
else:
controller()
#Setup PubNub
pubnub = Pubnub(
publish_key = 'pub-c-202f92c7-77aa-4abd-a588-edea3cbb4ee5',
subscribe_key = 'sub-c-d379b9a0-573c-11e6-b1c5-0619f8945a4f',
daemon = True
)
channel = 'control'
keep_alive_timer = None
def keep_alive_callback(time):
print(time)
def keep_alive():
pubnub.time(keep_alive_callback)
schedule_next_keep_alive()
def schedule_next_keep_alive():
keep_alive_timer = threading.Timer(30, keep_alive)
keep_alive_timer.start()
def callback(message, channel):
if (debug):
print('[' + channel + ']: ' + str(message))
route(message)
def error(message):
print("ERROR: " + str(message))
def connect(message):
print("CONNECTED")
#keep_alive()
def reconnect(message):
print("RECONNECTED")
def disconnect(message):
print("DISCONNECTED")
pubnub.subscribe(
channels = channel,
callback = callback,
error = error,
connect = connect,
reconnect = reconnect,
disconnect = disconnect
)
def teardownPubNub():
pubnub.unsubscribe(
channel = channel
)
#GPIO
def setupGPIO():
GPIO.setmode(GPIO.BCM)
def teardownGPIO():
GPIO.cleanup()
#Main
def main():
setupGPIO()
#model init
pump_init()
try:
while True:
time.sleep(5000)
except KeyboardInterrupt:
pass
teardownGPIO()
teardownPubNub()
sys.exit(0)
main()