-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_rgb.py
377 lines (355 loc) · 9.71 KB
/
mqtt_rgb.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
#! /usr/bin/env python3
import paho.mqtt.client as mqtt
import time
import board
import neopixel
import socket
import random
import subprocess
from datetime import datetime, timedelta
morse = {
"a": [1,2],
"b": [2,1,1,1],
"c": [2,1,2,1],
"d": [2,1,1],
"e": [1],
"f": [1,1,2,1],
"g": [2,2,1],
"h": [1,1,1,1],
"i": [1,1],
"j": [1,2,2,2],
"k": [2,1,2],
"l": [1,2,1,1],
"m": [2,2],
"n": [2,1],
"o": [2,2,2],
"p": [1,2,2,1],
"q": [2,2,1,2],
"r": [1,2,1],
"s": [1,1,1],
"t": [2],
"u": [1,1,2],
"v": [1,1,1,2],
"w": [1,2,2],
"x": [2,1,1,2],
"y": [2,1,2,2],
"z": [2,2,1,1],
" ": [0],
"1": [1,2,2,2,2],
"2": [1,1,2,2,2],
"3": [1,1,1,2,2],
"4": [1,1,1,1,2],
"5": [1,1,1,1,1],
"6": [2,1,1,1,1],
"7": [2,2,1,1,1],
"8": [2,2,2,1,1],
"9": [2,2,2,2,1],
"0": [2,2,2,2,2]
}
myip = subprocess.check_output(['hostname', '-I'])
try:
import keyboard
print("keyboard module present")
except:
keyboard = None
print("keyboard module not present. Please run sudo pip3 install keyboard")
############# CONFIG #############
uot = 0.05
pixel_pin = board.D18
num_pixels = 8
ORDER = neopixel.GRB
rainbow_cycle_delay = 0.001
mqtt_enabled = True
delay = 0.0333
##################################
modes = ["0","1","2","3","4","5","6","7"]
myname = socket.gethostname()
current_colors = []
current_color = (0,0,0)
wait_till = time.time()
key_down = False
last_time_update = datetime.now()
if myname == "addresspi":
num_pixels = 32
rainbow_cycle_delay = 0.001
elif myname == "canvaspi":
num_pixels = 50
rainbow_cycle_delay = 0.001
elif myname == "clockpi":
num_pixels = 60
rainbow_cycle_delay = 0.001
elif myname == "windowpi":
num_pixels = 16
rainbow_cycle_delay = 0.001
elif myname == "namepi":
num_pixels = 8
rainbow_cycle_delay = 0.05
mqtt_enabled = False
for i in range(0,num_pixels):
current_colors.append((0,0,0))
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=1.0, auto_write=False, pixel_order=ORDER
)
running = False
if mqtt_enabled is True:
client = mqtt.Client()
print("mqtt enabled")
else:
client = None
print("mqtt disabled")
mode = 3
# modes: 0-fill, 1-single pixel, 2-range, 3-gradient, 4-rainbow chase, 5-random pixels, 6-change random, 7-change swipe
j = 0
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def all_same():
check = True
for i in range(0,num_pixels):
if current_colors[i] != current_color:
check = False
return check
def fill(color):
current_color = color
for i in range(0,num_pixels):
current_colors[i] = color
pixels.fill(color)
pixels.show()
def rainbow_cycle(wait):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
pixels[i] = wheel(pixel_index & 255)
pixels.show()
time.sleep(wait)
def on_message(client, userdata, message):
global running
global mode
global delay
result = str(message.payload.decode("utf-8"))
print("Received: "+result)
client.publish('pi/' + myname + '/received', "Received: "+result)
if result == "stop":
running = False
args = result.split(':')
if args[0] == "setdelay":
delay = float(args[1])
return
mode = int(args[0])
if mode <= 2:
r = int(args[1])
g = int(args[2])
b = int(args[3])
if mode == 0:
fill((r,g,b))
elif mode == 1:
i = int(args[4])
c = (r,g,b)
pixels[i] = c
current_colors[i] = c
pixels.show()
elif mode == 2:
s = int(args[4])
e = int(args[5])
while s <= e:
c = (r,g,b)
pixels[s] = c
current_colors[s] = c
s = s + 1
pixels.show()
def on_key_down():
global running
global mode
global key_down
global current_color
if keyboard is None:
return
if key_down is True:
check = False
for m in modes:
if keyboard.is_pressed(m):
check = True
key_down = check
return
do_stuff = False
for m in modes:
if keyboard.is_pressed(m):
do_stuff = True
mode = int(m)
if do_stuff is False:
return
if mode == 0:
fill(current_color)
else:
current_color = wheel(random.randint(1,8) * 32 - 1)
fill(current_color)
key_down = True
def translate_name_address(a):
if num_pixels > 8:
return a
if a == 0:
return 5
if a == 1:
return 6
if a == 2:
return 7
if a == 3:
return 4
if a == 4:
return 3
if a == 5:
return 2
if a == 6:
return 1
return 0
def all_on():
for i in range(8):
pixels[i] = (255,255,255)
pixels.show()
def all_off():
for i in range(8):
pixels[i] = (0,0,0)
pixels.show()
def dit():
all_on()
time.sleep(uot)
all_off()
time.sleep(uot)
def da():
all_on()
time.sleep(uot*3)
all_off()
time.sleep(uot)
def pause():
time.sleep(1)
def do_letter(letter):
l = letter.lower()
if l not in morse.keys():
pause()
code = morse[l]
for c in code:
if c == 0:
pause()
elif c == 1:
dit()
elif c == 2:
da()
pause()
def do_message(message):
for l in message:
do_letter(l)
if __name__ == "__main__":
if mqtt_enabled is True:
try:
client.on_message = on_message
client.connect('192.168.2.200')
except:
client = None
if client is not None:
topic = 'pi/' + myname + '/commands'
print('subscribing to '+topic)
client.subscribe(topic)
client.loop_start()
running = True
while running is True:
now = time.time()
dtnow = datetime.now()
if myname == "namepi" and dtnow.minute in [0,15,30,45] and dtnow > last_time_update + timedelta(minutes=14):
last_time_update = dtnow
timestr = dtnow.strftime("%H %M")
do_message(timestr)
continue
on_key_down()
if now < wait_till:
continue
if mode <= 2:
wait_till = time.time() + 5.0
#time.sleep(5)
#mosquittoMessage("mqtt_rgb "+str(myip).split(' ')[0].replace("b'","")+" alive at "+datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
elif mode == 3 or mode == 4:
if j + 1 > 255:
j = 0
#mosquittoMessage("mqtt_rgb "+str(myip).split(' ')[0].replace("b'","")+" alive at "+datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
else:
j = j + 1
if mode == 3:
fill(wheel(j))
wait_till = time.time() + 0.066
continue
#time.sleep(0.066)
elif mode == 4:
rainbow_cycle(rainbow_cycle_delay)
elif mode == 5:
r = random.randint(1,8) * 32 - 1
a = random.randint(0, num_pixels-1)
pixels[a] = wheel(r)
pixels.show()
wait_till = time.time() + 1.0
continue
#time.sleep(1)
elif mode == 6:
if all_same() is True:
original_color = current_color
current_color = wheel(random.randint(1,8) * 32 - 1)
while current_color == original_color:
current_color = wheel(random.randint(1,8) * 32 - 1)
#time.sleep(random.randint(5,30))
wait_till = time.time() + random.randint(5,30)
continue
a = random.randint(0, num_pixels-1)
while current_colors[a] == current_color:
a = random.randint(0, num_pixels-1)
pixels[a] = current_color
current_colors[a] = current_color
pixels.show()
#time.sleep(0.25)
wait_till = time.time() + 0.25
continue
elif mode == 7:
if all_same() is True:
original_color = current_color
current_color = wheel(random.randint(1,8) * 32 - 1)
while current_color == original_color:
current_color = wheel(random.randint(1,8) * 32 - 1)
#time.sleep(random.randint(5,30))
wait_till = time.time() + random.randint(5,30)
continue
a = 0
while current_colors[a] == current_color:
a = a + 1
if a >= num_pixels:
a = 0
x = translate_name_address(a)
pixels[x] = current_color
current_colors[a] = current_color
pixels.show()
#time.sleep(0.1)
wait_till = time.time() + 0.1
continue
elif mode == 8:
last = num_pixels - 1
current_colors[last] = current_colors[0]
pixels[last] = current_colors[last]
for i in range(last):
current_colors[i] = current_colors[i+1]
pixels[i] = current_colors[i]
pixels.show()
time.sleep(delay)
client.loop_stop()
client.disconnect()