-
Notifications
You must be signed in to change notification settings - Fork 3
/
ESP12E_E131_udp_FIE.ino
675 lines (559 loc) · 19.2 KB
/
ESP12E_E131_udp_FIE.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* E131_udp.c
*
* Created on: 08.01.2017
* Author: Thomas
*/
#define _UDP WiFiUDP /* define meta var in case code needs to handle Ethernet as well */
_UDP Udp, UdpMaster; /* UDP handle */
// ------------------------------------------------------------------------------------- battery_getval
// LiPo batteries are sensitive if voltage is dropping a certain value
// in addition the LED stripe will need a certain voltage - espacially when showing blue
// read voltage from analog in and convert to Volt and %
void battery_getval(){
int iVoltLevel = 0;
if (millis() - batteryTimer > 100)
{
uiVoltAnalog = analogRead(ANALOG_PIN);
fVoltLipo = uiVoltAnalog * config.batDividerRatio * config.batAnalogRef / 1000 / 1024 ;
// Lipo: 1S full 4,2; empty 3,3V
// Lipo: 2S full 8,34; empty 6,6V
/*
iVoltLevel = (int) (( (fVoltLipo * 1000) - LIPO_mV_EMPTY) / (LIPO_mV_FULL - LIPO_mV_EMPTY) *100 );
if( iVoltLevel < 0) // check if level is below limit
uiVoltLevel = 0;
else
uiVoltLevel = iVoltLevel;
*/
// LED Stripe level: RED 0-7,5; // YELLOW 7,5 - 7,9V // GREEN 7,9V
battery_critical = false; // used to disable wiFi
if( fVoltLipo < 3.0 ) // no measurement attached to A0
uiVoltLevel = 99;
else if (fVoltLipo < 7.0) {
uiVoltLevel = 0;
battery_critical = true;
}
else if (fVoltLipo < 7.5)
uiVoltLevel = 0;
else if (fVoltLipo < 7.9)
uiVoltLevel = 50;
else
uiVoltLevel = 100;
/*
uiVoltLevel =0; // RED - BLUE LED too low
if( fVoltLipo > 7.5) // YELLOW - blue LED still ok
uiVoltLevel = 50;
if( fVoltLipo > 7.9) // GREEN - all LED bright
uiVoltLevel = 100;
*/
// Spot 1+2 are using a power from DMX device, no battery connected
if (config.deviceNum >1 && config.deviceNum <4)
uiVoltLevel = 100;
batteryTimer = millis();
}
}
// ------------------------------------------------------------------------------------- e131_set_rgb_all
// every module can send e131 in multicast mode
// this is for test purposes only, when the program for the light show is not available
// one device has 12 channels (DMX_SET)
// on actor=device has three modules with one LED stripe each
void e131_send_rgb_all(const uint8_t dRed,const uint8_t dGreen,const uint8_t dBlue){
e131.clearSendBuffer();
e131.setSourceName("ACN Tester - Thomas Hoeser");
for(int x=0; x<4; x++) // send package 3 times, less than 150ms
{
for (int i=(0*DMX_SET); i<(4*DMX_SET); i +=12) // first 4 devices to not have add on modules
{
e131.setRGB(i,dRed,dGreen,dBlue);
}
for (int i=(5*DMX_SET); i<(12*DMX_SET); i +=3) // first 4 devices to not have add on modules
{
e131.setRGB(i,dRed,dGreen,dBlue);
}
if(DMXsequence>0xFF)
DMXsequence = 1;
e131.setSequenceNumber(DMXsequence++);
e131.sendPacket(universeTX);
delay(40); // wait for 40ms to send next package
}
}
// ------------------------------------------------------------------------------------- e131_send_rgb_set
// send R-G-B
void e131_send_rgb_set(const uint8_t DmxSet, const uint8_t dRed,const uint8_t dGreen,const uint8_t dBlue){
if(DMXsequence>0xFF)
DMXsequence = 1;
e131.setSequenceNumber(DMXsequence++);
e131.setSourceName("ACN Tester - Thomas Hoeser");
e131.setRGB(DmxSet*DMX_SET,dRed,dGreen,dBlue);
e131.sendPacket(universeTX);
delay (100);
}
// ------------------------------------------------------------------------------------- e131_send_rgb_set_ext
void e131_send_rgb_set_ext(const uint8_t DmxSet, const uint8_t dRed,const uint8_t dGreen,const uint8_t dBlue){
uint8_t dmxStartCh = DmxSet * DMX_SET;
testmode = false;
MessageString = " TX: Set:";
MessageString += (String) DmxSet;
MessageString += " |Channel:";
MessageString += (String) (dmxStartCh);
MessageString += "| ";
MessageString += (String) (dRed);
MessageString += "-";
MessageString += (String) (dGreen);
MessageString += "-";
MessageString += (String) (dBlue);
NodeServer_text_Multicast(MessageString);
DEBUGGING(MessageString);
e131.clearSendBuffer();
e131.setSourceName("ACN Tester - Thomas Hoeser");
for(int x=0; x<4; x++) // send package 3 times, less than 150ms
{
e131.setSequenceNumber(DMXsequence++);
if(DMXsequence>0xFF)
DMXsequence = 1;
e131.setRGB(dmxStartCh ,dRed, dGreen, dBlue);
if(DmxSet>3){
e131.setRGB(dmxStartCh+3, dRed, dGreen, dBlue);
e131.setRGB(dmxStartCh+6, dRed, dGreen, dBlue);
}
e131.sendPacket(universeTX);
delay(40); // wait for 40ms to send next package
}
}
// ------------------------------------------------------------------------------------- e131_test_rgb
void e131_test_rgb(){
e131.setSequenceNumber(DMXsequence++);
e131_send_rgb_all(255,0,0); delay (500);
e131_send_rgb_all(0,255,0); delay (500);
e131_send_rgb_all(0,0,255); delay (500);
e131_send_rgb_all(0,0,0) ; delay (500);
}
// ------------------------------------------------------------------------------------- reconnect_e131
void reconnect_e131(){
DEBUGGING_L("\ne131> reconnect ");
e131.stopUdp();
e131.connectMulticast(config.universe);
}
// ------------------------------------------------------------------------------------- handle_udp
// read udp package and write DMX value to LED output (analog,P9813.DMX)
void handle_e131() {
/* Parse a packet and update pixels */
if(e131.parsePacket())
{
udpActive = true;
testmode = false;
if(SoftAPup == true)
WifiStopAP();
esp_level = 70;
udpPacketCount ++;
powerOffTimer = tempTimer = lastTimeUdp = millis();
if (config.DMXoutputOn == false)
digitalWrite(LED_STATUS, HIGH); // set blue status LED
if (e131.universe == universe && e131.ddStartCode == 0) { // use only e131 packages from the right universe
esp_status = "E1.31";
esp_level = 100;
// read DMX values into temp variable
uiDMXRed = e131.data[DMX_RED ];
uiDMXGreen = e131.data[DMX_GREEN ];
uiDMXBlue = e131.data[DMX_BLUE ];
uiDMXRed2 = e131.data[DMX_RED2 ];
// when device is started, the blue status LED will flash
// if 1st DMX channel is used, indicate e131 submission has started
// than the flashing of the blue status LED will stop
if((e131.data[0]) != 0)
e131Active = true;
// apply gamma factor on DMX values
// every modules can reduce the brightness -> Page_DMX.h
if(dmxGammaPerc <100)
{
uiDMXRed = uiDMXRed * dmxGammaPerc / 100 ; // 255 * 100 / 100 = 255; 25 * 200 / 100 = 50
uiDMXGreen = uiDMXGreen * dmxGammaPerc / 100 ;
uiDMXBlue = uiDMXBlue * dmxGammaPerc / 100 ;
uiDMXRed2 = uiDMXRed2 * dmxGammaPerc / 100 ;
}
if (uiVoltLevel < 50) // battery too low
{
Led_Off();
}
else
{
// set output for analog and P9813
analogWrite(pLED_RED, uiDMXRed);
analogWrite(pLED_GREEN, uiDMXGreen);
analogWrite(pLED_BLUE, uiDMXBlue);
analogWrite(pLED_RED2, uiDMXRed2);
P9813_show( 0, uiDMXRed, uiDMXGreen, uiDMXBlue);
// send data to DMX output if required
if (config.DMXoutputOn == true)
{
// int i=0;
for (int thisChannel = DMX_RED, i=0; thisChannel <= (DMX_RED+4); thisChannel++, i++)
{
dmx.write(thisChannel+1, e131.data[thisChannel]);
dmx.write(i+1, e131.data[thisChannel]);
}
dmx.update();
delay(10);
} // DMX output
} // battery too low
yield(); // grant time to background process like wifi handler
} // if (e131.universe == UNIVERSE)
// every 2 seconds the modules will send a status to a node server
if (tempTimer - aliveTimer > 2000)
{
battery_getval();
WifiSignal();
yield();
NodeServer_ping_Multicast();
aliveTimer = tempTimer;
yield();
}
// blink blue LED to indicate idle mode with active RF connection
if (tempTimer - aliveE131Timer > 2000)
{
if(e131Active == false) // do not blink if submission has started
{
digitalWrite(LED_STATUS, LOW);
delay(50);
aliveE131Timer = tempTimer;
}
}
} // if(e131.parsePacket()) {
// when no udp packages are received for a given time, switch off all LEDs
// during a show people might not notice a device which is not switched on
// but it would be very obvious if the LED are on and all other are off
else {
if (tempTimer - powerOffTimer > uiDxmPowerOffTime && uiDxmPowerOffTime > 0 )
{
// DEBUGGING("udp> timeout exceeded -> auto power off");
if (udpActive == true) // status True = 1
{
udpActive = false;
e131Active = false;
Led_Off();
battery_getval();
WifiSignal();
yield(); // grant time to background process like wifi handler
MessageString = " no udp packet since 2 sec";
esp_status = "timeout";
NodeServer_text_Multicast(MessageString);
tempTimer = powerOffTimer = flashTimer = millis(); // reset timer
yield(); // grant time to background process like wifi handler
} // if (udpStatus == 1)
} // if (tempTimer - powerOffTimer > 2000)
if (millis() - lastTimeUdp > 2000)
{
udpActive = false;
e131Active = false;
Led_Off();
battery_getval();
WifiSignal();
MessageString = " idle - reconnect E1.31";
esp_status = "CONNECT";
// esp_level = 30;
NodeServer_text_Multicast(MessageString);
reconnect_e131();
delay(100);
lastTimeUdp = millis();
yield();
}
} // no packet received
}
// ------------------------------------------------------------------------------------- DMXInit
// set paramaters to read correct channels later on
void DMX_LED_off() {
/*
if (config.DMXoutputOn == true)
{
for (int thisChannel = DMX_RED; thisChannel <= (DMX_RED+4); thisChannel++)
dmx.write(thisChannel+1, 0);
dmx.update();
}
*/
if (config.DMXoutputOn == true)
{
dmx.write(1, 0 );
dmx.write(2, 0 );
dmx.write(3, 0 );
dmx.update();
}
}
// ------------------------------------------------------------------------------------- DMXInit
// set paramaters to read correct channels later on
void DMX_LED_set(int iRed, int iGreen, int iBlue) {
if (config.DMXoutputOn == true)
{
dmx.write(1, iRed );
dmx.write(2, iGreen );
dmx.write(3, iBlue );
dmx.update();
}
}
// ------------------------------------------------------------------------------------- DMXInit
// set paramaters to read correct channels later on
void DMX_Init() {
dmxCh = config.deviceNum * DMX_SET + config.moduleNum *3;
dmxStartChannel = 1 + dmxCh;
DMX_RED = 0 + dmxCh;
DMX_GREEN = 1 + dmxCh;
DMX_BLUE = 2 + dmxCh;
DMX_RED2 = 3 + dmxCh;
uiDxmPowerOffTime = config.DxmPowerOffTime *1000;
if (config.DMXoutputOn == true)
{
DEBUGGING_L("DMX> device ");
DEBUGGING(config.deviceNum);
DEBUGGING_L("DMX> module ");
DEBUGGING(config.moduleNum);
DEBUGGING_L("DMX> channel ");
DEBUGGING(dmxCh);
DEBUGGING_L("DMX> Start channel ");
DEBUGGING(dmxStartChannel);
}
} // DMXInit()
// ------------------------------------------------------------------------------------- NodeServer_init_Multicast
// establish udp in order to send messages to a node server
void NodeServer_init_Multicast() {
// IPAddress ipMulti = IPAddress(192,168,178,255);
ipMulti = WiFi.localIP(); // get my IP address
ipMulti[3] = 255; // set IP to broadcast address
retval = UdpMaster.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
if (Serial) {
Serial.printf("udp> Master Multicast IP %s, UDP port %d -> ", WiFi.localIP().toString().c_str(), 45131);
DEBUGGING(ipMulti);
/*
if (retval > 0)
DEBUGGING(" ok");
else
DEBUGGING(" ERROR: no socket available");
*/
}
}
// ------------------------------------------------------------------------------------- NodeServer_ping_Multicast
// module will send x seconds teh RGB values to the node server
void NodeServer_text_Multicast_json(String sMessage) {
/*
* {"name":"Ucl-No7-Wheel","systemInfo":{"FWver":"v3.12","state":"BOOT","bat":{"volt":0.00,"perc":0},"rssi":{"db":0,"perc":0
* }},"dmx":{"set":11,"mod":0,"uni":2,"out":"off","color":{"r":0,"g":0,"b":0}},"msg":"ALIVE"}
*/
// https://jsonformatter.curiousconcept.com/
String udpString;
int iLedRed = 0;
int iLedGreen = 0;
int iLedBlue = 0;
if(udpActive == true) {
iLedRed = e131.data[DMX_RED ];
iLedGreen = e131.data[DMX_GREEN ];
iLedBlue = e131.data[DMX_BLUE ];
}
udpString = "{\"name\":\"";
udpString += config.DeviceName;
udpString += "\",\"systemInfo\":{\"FWver\":\"";
udpString += fwVers;
udpString += "\",\"state\":\"";
udpString += esp_status;
udpString += "\",\"perc\":\"";
udpString += esp_level;
udpString += "\",\"ip\":\"";
udpString += esp_ip_str;
udpString += "\",\"bat\":{\"volt\":";
udpString += fVoltLipo;
udpString += ",\"perc\":";
udpString += uiVoltLevel;
udpString += "},\"rssi\":{\"db\":";
udpString += rssi_value;
udpString += ",\"perc\":";
udpString += rssi_percent;
udpString += "}},\"dmx\":{\"set\":";
udpString += config.deviceNum;
udpString += ",\"mod\":";
udpString += config.moduleNum;
udpString += ",\"uni\":";
udpString += config.universe;
udpString += ",\"serial\":\"";
if(config.DMXoutputOn == true)
udpString += "on";
else
udpString += "off";
udpString += "\",\"data\":\"";
if(udpActive == true)
udpString += "on";
else
udpString += "off";
udpString += "\",\"color\":{\"r\":";
udpString += iLedRed;
udpString += ",\"g\":";
udpString += iLedGreen;
udpString += ",\"b\":";
udpString += iLedBlue;
udpString += "}},\"msg\":\"";
udpString += sMessage;
udpString += "\"}";
UdpMaster.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
UdpMaster.println(udpString);
UdpMaster.endPacket();
if (Serial) {
DEBUGGING_L("udp>>");
DEBUGGING (udpString);
}
} // end of NodeServer_text_Multicast_json
// ------------------------------------------------------------------------------------- NodeServer_ping_Multicast
// module will send x seconds teh RGB values to the node server
void NodeServer_text_Multicast_json_crash(String sMessage) {
String udpString;
int iLedRed = 0;
int iLedGreen = 0;
int iLedBlue = 0;
// battery_getval();
root["name"] = config.DeviceName;
JsonObject& systemInfo= root.createNestedObject("systemInfo");
systemInfo["FWver"] = fwVers;
systemInfo["state"] = esp_status;
JsonObject& bat = systemInfo.createNestedObject("bat");
bat["volt"] = fVoltLipo;
bat["perc"] = uiVoltLevel;
JsonObject& rssi = systemInfo.createNestedObject("rssi");
rssi["db"] = rssi_value;
rssi["perc"] = rssi_percent;
JsonObject& dmx = root.createNestedObject("dmx");
dmx["set"] = config.deviceNum;
dmx["mod"] = config.moduleNum;
dmx["uni"] = config.universe;
if(config.DMXoutputOn == true)
dmx["out"] = "on";
else
dmx["out"] = "off";
if(udpActive == true) {
iLedRed = e131.data[DMX_RED ];
iLedGreen = e131.data[DMX_GREEN ];
iLedBlue = e131.data[DMX_BLUE ];
}
JsonObject& color = dmx.createNestedObject("color");
color ["r"] = iLedRed;
color ["g"] = iLedGreen;
color ["b"] = iLedBlue;
root["msg"] = sMessage;
// root.printTo(Serial);
// Serial.println();
// Serial.println("JSON (pretty)");
// root.prettyPrintTo(Serial);
// Serial.println("JSON");
// root.printTo(udpString);
// Serial.println(udpString);
// udp.beginPacket(remoteIp, remotePort);
// json.printTo(udp);
// udp.println();
// udp.endPacket();
yield();
UdpMaster.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
root.printTo(UdpMaster);
UdpMaster.println();
//UdpMaster.println(udpString);
UdpMaster.endPacket();
yield();
if (Serial) {
DEBUGGING_L("udp>>");
root.printTo(Serial);
DEBUGGING ("<<udp");
}
} // end of NodeServer_text_Multicast_json
// ------------------------------------------------------------------------------------- setup
// add header to message for node server
// this header will enable the node server interface to group the modules
void NodeServer_text_Multicast_flat(String sMessage) {
String udpString = (String) config.DeviceName;
udpString += " |set: ";
udpString += (String) config.deviceNum;
udpString += " |mod: ";
udpString += (String) config.moduleNum;
udpString += " |uni: ";
udpString += (String) config.universe;
udpString += " |bat: ";
udpString += fVoltLipo;
udpString += "V";
udpString += " / ";
udpString += uiVoltLevel;
udpString += "%";
udpString += " |rssi: ";
udpString += rssi_percent;
udpString += "%";
udpString += " / ";
udpString += rssi_value;
udpString += "db";
udpString += " |ver: ";
udpString += fwVers;
if (config.DMXoutputOn == true)
udpString += " |DMX:on ";
else
udpString += " |DMX:- ";
udpString += " |msg: ";
udpString += sMessage;
UdpMaster.beginPacketMulticast(ipMulti, portMulti,WiFi.localIP());
UdpMaster.println(udpString);
UdpMaster.endPacket();
if (Serial) {
DEBUGGING_L("udp>>");
DEBUGGING (udpString);
}
} // end of NodeServer_text_Multicast_flat
// ------------------------------------------------------------------------------------- OLED_status_DMX
void OLED_status_DMX(){
String oledString;
OLED_clear();
oledString = "WiFi: ";
oledString += rssi_percent;
oledString += "%";
oledString += " / ";
oledString += rssi_value;
oledString += "db";
OLED_print(0,0,oledString);
OLED_Wlan_bar(100,20,rssi_percent);
oledString = "";
oledString += esp_status;
OLED_print(0,40,oledString);
if(udpActive == true) {
iLedRed = e131.data[DMX_RED ];
iLedGreen = e131.data[DMX_GREEN ];
iLedBlue = e131.data[DMX_BLUE ];
}
OLED_print(0,20,"DMX: ");
OLED_print(30,20,(String) iLedRed );
OLED_print(60,20,(String) iLedGreen);
OLED_print(90,20,(String) iLedBlue );
}
// ------------------------------------------------------------------------------------- NodeServer_ping_Multicast
void NodeServer_text_Multicast(String sMessage) {
if(node_json == true)
NodeServer_text_Multicast_json(sMessage);
else
NodeServer_text_Multicast_flat(sMessage);
OLED_status_DMX();
}
// ------------------------------------------------------------------------------------- NodeServer_ping_Multicast
// module will send x seconds teh RGB values to the node server
void NodeServer_ping_Multicast() {
iLedRed = e131.data[DMX_RED ];
iLedGreen = e131.data[DMX_GREEN ];
iLedBlue = e131.data[DMX_BLUE ];
iLedRed2 = e131.data[DMX_RED2 ];
String MessageString = "E131 | R : ";
MessageString += (String) iLedRed;
MessageString += " | G : ";
MessageString += (String) iLedGreen;
MessageString += " | B : ";
MessageString += (String) iLedBlue;
NodeServer_text_Multicast(MessageString);
}
void udp_send_init() {
pinMode(Button2, INPUT);
pinMode(Button3, INPUT);
}
void udp_send_test() {
MessageString = " TX test RGB";
NodeServer_text_Multicast(MessageString);
DEBUGGING(MessageString);
e131_send_rgb_all(255,0,0); delay (500);
e131_send_rgb_all(0,255,0); delay (500);
e131_send_rgb_all(0,0,255); delay (500);
e131_send_rgb_all(0,0,0) ; delay (500);
}