-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT-esp32.ino
396 lines (362 loc) · 12.4 KB
/
MQTT-esp32.ino
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
#include <FastLED.h>
#include <WiFiClientSecure.h>
#include <MQTT.h>
#include "creditentials.h"
#include "LedsZones.h"
#define NUM_LEDS 209
#define NUM_ZONE 5
#define ALIM 26
#define LED 27
#pragma region Masks
#define STATE_SWITCH 0b00000001
#define STATE_MODE 0b00000010
#define STATE_BEHAVIOR 0b00000100
#define RECV_MODE 0b00000001
#define RECV_BEHAVIOR 0b00000010
#define RECV_CHANGE_COLOR 0b00000100
#define RECV_ZONE 0b00111000
#define RECV_SLOT 0b11000000
#pragma endregion Masks
#define ZONE (buf[0] & RECV_ZONE) >> 3
#define SIGN(x) (x > 0) - (x < 0)
union rawFloat
{
float value;
uint8_t raw[4];
};
LedsZones ledsZones(NUM_ZONE, NUM_LEDS);
CRGB leds[NUM_LEDS];
bool leds_diff = false;
unsigned long timerLed = 0;
CRGB color1 = CRGB::Blue;
CRGB color2 = CRGB::Red;
CRGB color1value = color1;
CRGB color2value = color2;
CRGB color1valuebis = color1;
CRGB color2valuebis = color2;
bool bisColor = false;
const char letsEncyptCA[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw\n"
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n"
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw\n"
"WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\n"
"RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n"
"AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP\n"
"R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx\n"
"sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm\n"
"NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg\n"
"Z3Vms/EY96Jc5lP/Ooi2R6X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N/lxPFin+\n"
"HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv\n"
"MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX\n"
"nLRbwHOoq7hHwg==\n"
"-----END CERTIFICATE-----\n";
WiFiClientSecure net;
MQTTClient client(256);
bool pong = false;
bool alimSwitch = false;
bool alimSwitchReal = false;
bool sceneMode = false;
bool inGame = false;
unsigned long timerLastInfo;
void setup()
{
//Serial.begin(115200);
FastLED.addLeds<WS2812B, LED, GRB>(leds, NUM_LEDS) // Led strip on pin 27
.setCorrection(TypicalLEDStrip);
for (int i = 0; i < NUM_LEDS; i++)
{ // Setting zones
if (i <= 42)
ledsZones.setZone(i, 2); // Left lasers
else if (i > 167)
ledsZones.setZone(i, 3); // Right Lasers
else if (i > 83 && i <= 125)
ledsZones.setZone(i, 4); // Center Light
else if ((i > 42 && i <= 83))
ledsZones.setZone(i, 1); // Ring Light
else if ((i > 125 && i <= 167))
ledsZones.setZone(i, 0); // Back Lasers
}
pinMode(ALIM, OUTPUT);
WiFi.disconnect();
WiFi.begin(ssid, pass);
net.setCACert(letsEncyptCA);
client.begin(host, 8883, net);
client.onMessageAdvanced(messageReceived);
}
void loop()
{
if (leds_diff && millis() - timerLed > 10)
{
timerLed = millis();
leds_diff = ledsZones.computeAnimations(); // Check again for diff and apply the filter
alimSwitch = sceneMode;
for (int i = 0; i < NUM_LEDS; i++)
{
if (leds[i] != (CRGB)(CRGB::Black))
alimSwitch = true;
if (leds[i] != (leds[i] = ledsZones.getColor(i)))
leds_diff = true;
}
FastLED.show(); // Apply
}
else if (sceneMode && millis() - timerLastInfo > 300000)
{ // 5 minutes after the last info, disable BS mode if still enabled
sceneMode = false;
}
else if (!(alimSwitchReal || alimSwitch) && !sceneMode)
delay(1000);
if (!sceneMode && alimSwitch != alimSwitchReal)
{
alimSwitch ? digitalWrite(ALIM, HIGH) : digitalWrite(ALIM, LOW);
alimSwitchReal = alimSwitch;
}
if (!client.connected())
{
while (WiFi.status() != WL_CONNECTED)
yield();
while (!client.connect("deskleds", user, mqttPass))
delay(1000);
client.subscribe("/led", 1);
client.subscribe("/led/ping", 0);
client.subscribe("/led/stream", 0);
}
if (pong)
{
client.publish("/led/ping", "pong");
pong = false;
}
client.loop();
}
/* Description of the recieved message:
"ping" for a ping, otherwise:
1st byte:
Bitfield
+---------+-------------+----------------+-----------------------------+---------------------+----------------------------------------+
| Bit | 0 mode | 1 behavior | 2 change color ? | 3,4,5 affected zone | 6,7 Depends on bit 2 |
+---------+-------------+----------------+-----------------------------+---------------------+----------------------------------------+
| | 0: Normal \| 0: Uniform | Always true | 0: Back Lasers | In case # => slot | Else => Chroma |
| | /| 1: LED by LED >---------------------------->| 1: Ring Light | 0: Color 1 | 0: No Chroma event |
| Meaning | ----+----------------+-----------------------------+ 2: Left Lasers | 1: Color 2 | 1: RGB |
| | 1: BS mode \| 0: Out-game \| 0: Change Color (vanilla) # | 3: Right Lasers | 2: Color 1 bis | 2: Gradient |
| | /| 1: In-game /| 1: Don't (or via chroma) > | 4: Center Light | 3: Color 2 bis | |
+---------+-------------+----------------+-----------------------------+ 5: Color bis +----------------------------------------+
| 6: Interscope left |
| 7: Interscope right |
+---------------------+
For now normal mode implies changing color
If it changes a color (0bXXXXXX0 or 0b00XX011):
2nd, 3rd and 4th bytes are R, G and B
Else:
2nd byte is the value of the change (only available in BS mode):
0: Turns the light group off.
1: Changes the lights to 'color 1', and turns the lights on.
2: Changes the lights to 'color 1', and flashes brightly before returning to normal. duration ≅ 0.2s
3: Changes the lights to 'color 1', and flashes brightly before fading to black. duration ≅ 1s
4: Unused.
5: Changes the lights to 'color 2', and turns the lights on.
6: Changes the lights to 'color 2', and flashes brightly before returning to normal.
7: Changes the lights to 'color 2', and flashes brightly before fading to black.
When the change group is Color bis:
0: Uses main colors
1: Uses secondary colors
if chroma event:
RGB:
3rd, 4th and 5th bytes are R, G and B
Gradient:
3rd is easing ID please read `LedsZones.h` for ID <=> name correspondance
4th, 5th, 6th and 7th are duration (float, sign bit tailing = littleEndian)
8th, 9th, 10th are the starting color
11th, 12th, 13rd are the ending color
*/
int bsMode(const char *buf)
{
int bytesRead = 0;
if (!(buf[0] & RECV_BEHAVIOR) != !inGame)
{
inGame = buf[0] & RECV_BEHAVIOR;
for (int i = 0; i < NUM_ZONE; i++)
{
ledsZones.setColor(i, inGame ? CRGB::Black : i % 2 ? color1
: color2);
ledsZones.setAnim(i, 3, 4); // cubic is cool
ledsZones.setDuration(i, inGame ? 1 : 5);
}
}
if ((buf[0] & (0b00000101)) == 1)
{ // If we're changing a color
CRGB color = colorParsing(buf + 1);
switch ((buf[0] & RECV_SLOT) >> 6)
{
case 0:
color1value = color;
color1 = bisColor ? color1valuebis : color1value;
break;
case 1:
color2value = color;
color2 = bisColor ? color2valuebis : color2value;
break;
case 2:
color1valuebis = color;
color1 = bisColor ? color1valuebis : color1value;
break;
case 3:
color2valuebis = color;
color2 = bisColor ? color2valuebis : color2value;
break;
}
if (!inGame)
{
for (int i = 0; i < NUM_ZONE; i++)
{
ledsZones.setColor(i, i % 2 ? color1 : color2);
ledsZones.setAnim(i, 3, 4); // cubic is cool
ledsZones.setDuration(i, 3);
}
}
bytesRead = 4;
}
else
{
byte chroma = buf[0] >> 6;
bytesRead = 2;
if (inGame)
{
if (ZONE == 5)
{
bisColor = buf[1];
color1 = bisColor ? color1valuebis : color1value;
color2 = bisColor ? color2valuebis : color2value;
}
else
{
CRGB color;
rawFloat duration;
byte anim;
// Take care of the vanilla behavior
switch (buf[1])
{ // Both the stock animations could be longer:
// the meaningfull part of the customs animations occupy only the 2 first thirds
case 0:
color = CRGB::Black;
duration.value = 0;
anim = 0;
break;
case 1:
color = color1;
duration.value = 0;
anim = 0;
break;
case 2:
color = color1;
duration.value = 0.6;
anim = 1;
break;
case 3:
color = color1;
duration.value = 2;
anim = 2;
break;
case 5:
color = color2;
duration.value = 0;
anim = 0;
break;
case 6:
color = color2;
duration.value = 0.6;
anim = 1;
break;
case 7:
color = color2;
duration.value = 2;
anim = 2;
break;
}
// parse the chroma events
if (chroma == 2)
{ // Both color and animation specifications here are using an other overload than vanilla + rgb
ledsZones.setAnim(ZONE, 3, buf[2]);
memcpy(duration.raw, buf + 3, 4);
ledsZones.setColor(ZONE, colorParsing(buf + 7), colorParsing(buf + 10));
bytesRead = 13;
}
else
{
ledsZones.setColor(ZONE, chroma ? colorParsing(buf + 2) : color);
ledsZones.setAnim(ZONE, anim);
bytesRead = chroma ? 5 : 2;
}
ledsZones.setDuration(ZONE, duration.value);
}
}
}
return bytesRead;
}
CRGB colorParsing(const char *buf)
{
/**
@brief parse color from buf
@param buf needs to be the color with the 1st byte beeing the red value
@return color CRGB object beeing the color parsed
*/
return CRGB(buf[0], buf[1], buf[2]);
}
int messageParsing(const char *buf)
{
sceneMode = buf[0] & RECV_MODE;
leds_diff = true;
if (buf[0] & RECV_MODE)
return bsMode(buf);
else
return normalMode(buf); // Number of bytes read
}
int normalMode(const char *buf)
{
if (buf[0] & 2)
{ // LED-by-LED
ledsZones.setColor(ZONE, colorParsing(buf + 1));
ledsZones.setAnim(ZONE, 3, 22);
ledsZones.setDuration(ZONE, 5);
}
else
{ // Uniform
for (int i = 0; i < NUM_ZONE; i++){
ledsZones.setColor(i, colorParsing(buf + 1));
ledsZones.setAnim(i, 3, 22);
ledsZones.setDuration(i, 5);
}
}
return 4;
}
void messageReceived(MQTTClient *client, char topic[], char bytes[], int length)
{
if (!strcmp(topic, "/led/ping") && !strcmp(bytes, "ping"))
{
pong = true;
}
else if (!strcmp(topic, "/led") || !strcmp(topic, "/led/stream"))
{
int startAt = 0;
while (length > startAt)
{
startAt += messageParsing(bytes + startAt);
}
}
timerLastInfo = millis();
}