-
Notifications
You must be signed in to change notification settings - Fork 0
/
HouseLed.ino
575 lines (505 loc) · 17.9 KB
/
HouseLed.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
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h> // Voor de i2c bus
#include <BH1750.h>
#include <i2cdetect.h>
#include <math.h>
#include <Stdio.h>
//#include <Bridge.h>
#include <ESP8266HTTPClient.h>
#include "config.h"
// used example: https://www.electronics-lab.com/project/nodemcu-esp8266-webserver-tutorial/
// NodeMCU: https://www.instructables.com/id/Getting-Started-With-ESP8266LiLon-NodeMCU-V3Flashi/
// Neopixel: https://github.com/adafruit/Adafruit_NeoPixel
// BH1750: https://create.arduino.cc/projecthub/infoelectorials/project-017-arduino-bh1750-light-sensor-project-640075
// HTTP Request: http://domoticx.com/esp8266-wifi-http-get-request-maken-arduinoide/
//Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // Also done in config file
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
WiFiServer server(80);// Set port to 80
String header; // This storees the HTTP request
byte buff[2];
String greenstate = "off";// state of green LED
String redstate = "off";// state of red LED
String bluestate = "off";
String selectLeds;
int typeCommend;
int red = 0;
int green = 0;
int blue = 0;
int ind1; // as index for splitting the url
int ind2;
int ind3;
int ind4;
int ind5;
int ind6;
int cmCounter = 0;
int arrayPref[20];
//int selectedLeds[200];
int movementState = 2;
//int movementIDX = 56;
int cmDelay = 0;
bool luxMeter;
bool redUP = false; // Updown for the fade
bool greenUP = false;
bool blueUP = false;
bool fade = false;
bool christmas = false;
uint32_t stripcolor;
BH1750 lightMeter;
int lightCount=0; // Counter for interval in sending light intensity to Domoticz
//int lightIDX = 99; // Domoticz IDX 48 licht intentiteit beuiten, Licht hal = XX (device idx in domotics
char domoURL[100];
HTTPClient http;
void setup() {
// Setup common:
Serial.begin(115200); // using LoLin NodeMCU at 9600
Serial.println("HouseLed starting up.....");
// Setup wifi
WiFi.begin(ssid, password);
Serial.print("Connecting to ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());// this will display the Ip address of the Pi which should be entered into your browser
server.begin();
// Setup neopixel
Serial.print("Neopixel setup");
strip.begin();
strip.show(); // all pixels to off
Serial.println("...... Done");
// i2c bus
Wire.begin(D2,D1);
// BH1750_Init(Bh1750addr);
// delay(200);
i2cdetect();
luxMeter = lightMeter.begin();
Serial.print(F("BH1750 Init "));
Serial.println(luxMeter);
delay(2000);
// Movement sonsor
pinMode(movement, INPUT);
// attachInterrupt(0, flow, RISING); // see http://arduino.cc/en/Reference/attachInterrupt
// sei(); // Enable interrupts
}
void loop() {
// Webserver is used to get commands from Domoticz
int i;
uint16_t val1=0;
// Check movement input
int value = digitalRead(movement);
if (value == HIGH) {
if (movementState != HIGH) {
movementState = HIGH;
Serial.println("Movement to HIGH");
}
} else {
if (movementState != LOW) {
movementState = LOW;
Serial.println("Movement to LOW");
sprintf(domoURL,"http://%s/json.htm?type=command¶m=switchlight&idx=%i&switchcmd=On", domoIP, movementIDX) ;
http.begin(domoURL);
int httpResponseCode = http.GET(); // Fire and forget: No check on good reception of the call
Serial.println(domoURL);
Serial.println(httpResponseCode);
}
}
if (fade) {
doFade();
delay (500);
}
if (christmas) {
doChristmas();
delay (500);
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// We do something with the received command
if (header.indexOf("GET /off") >= 0) {
Serial.println("all off");
fade=false;
christmas = false;
red = 0;
green = 0;
blue = 0;
typeCommend = 1; //"normal";
} else if (header.indexOf("GET /green/on") >= 0) {
Serial.println("green on");
greenstate = "on";
green=255;
typeCommend = 1; //normal
} else if (header.indexOf("GET /green/off") >= 0) {
Serial.println("green off");
greenstate = "off";
green=0;
typeCommend = 1;
} else if (header.indexOf("GET /red/on") >= 0) {
Serial.println("red on");
redstate = "on";
red=255;
typeCommend = 1;
} else if (header.indexOf("GET /red/off") >= 0) {
Serial.println("red off");
redstate = "off";
red=0;
typeCommend = 1;
} else if (header.indexOf("GET /blue/on") >= 0) {
Serial.println("blue on");
bluestate = "on";
blue=255;
typeCommend = 1;
} else if (header.indexOf("GET /blue/off") >= 0) {
Serial.println("blue off");
bluestate = "off";
blue=0;
typeCommend = 1;
} else if (header.indexOf("GET /fade/on") >= 0) {
Serial.println("fade on");
fade=true;
christmas = false;
typeCommend = 2; //"fade";
} else if (header.indexOf("GET /fade/off") >= 0) {
Serial.println("fade off");
fade=false;
christmas = false;
red = 0;
green = 0;
blue = 0;
typeCommend = 1; //"normal";
} else if (header.indexOf("GET /2512") >= 0) {
Serial.println("Christmas special");
fade=false;
christmas = true;
typeCommend = 2512; //"";
} else if (header.indexOf("GET /color/") >= 0) {
ind1 = header.indexOf("GET /color/");
ind2 = header.indexOf("\n", ind1 + 1);
String readString = header.substring(ind1+1, ind2);
ind1 = readString.indexOf("/");
ind2 = readString.indexOf("/", ind1+1);
ind3 = readString.indexOf("/", ind2+1);
ind4 = readString.indexOf("/", ind3+1);
ind5 = readString.indexOf("/", ind4+1);
red = header.substring(ind2 +2, ind3+1).toInt();
green = header.substring(ind3 +2, ind4+1).toInt();
blue = header.substring(ind4+2, ind5+1).toInt();
Serial.print("color: R ");
Serial.print(red);
Serial.print(" G ");
Serial.print(green);
Serial.print(" B ");
Serial.println(blue);
fade=false;
christmas = false;
typeCommend = 1; //normal
} else if (header.indexOf("GET /pref1/") >= 0) { // Preference1 = 'some' leds on with color red/green/blue
ind1 = header.indexOf("GET /pref1/");
ind2 = header.indexOf("\n", ind1 + 1);
String readString = header.substring(ind1+1, ind2);
ind1 = readString.indexOf("/");
ind2 = readString.indexOf("/", ind1+1);
ind3 = readString.indexOf("/", ind2+1);
ind4 = readString.indexOf("/", ind3+1);
ind5 = readString.indexOf("/", ind4+1);
ind6 = readString.indexOf(" ", ind5+1);
red = header.substring(ind2 +2, ind3+1).toInt();
green = header.substring(ind3 +2, ind4+1).toInt();
blue = header.substring(ind4+2, ind5+1).toInt();
Serial.print("pref1: R ");
Serial.print(red);
Serial.print(" G ");
Serial.print(green);
Serial.print(" B ");
Serial.println(blue);
fade=false;
christmas = false;
typeCommend = 3; //"pref1";
} else if (header.indexOf("GET /select/") >= 0) { // give leds additional leds on with color red/green/blue
ind1 = header.indexOf("GET /select/");
ind2 = header.indexOf("\n", ind1 + 1);
String readString = header.substring(ind1+1, ind2);
ind1 = readString.indexOf("/");
ind2 = readString.indexOf("/", ind1+1);
ind3 = readString.indexOf("/", ind2+1);
ind4 = readString.indexOf("/", ind3+1);
ind5 = readString.indexOf("/", ind4+1);
ind6 = readString.indexOf(" ", ind5+1); // URL ends with a space in de header
red = header.substring(ind2 +2, ind3+1).toInt();
green = header.substring(ind3 +2, ind4+1).toInt();
blue = header.substring(ind4+2, ind5+1).toInt();
selectLeds = header.substring(ind5+2, ind6+1);
Serial.print("select: R ");
Serial.print(red);
Serial.print(" G ");
Serial.print(green);
Serial.print(" B ");
Serial.print(blue);
Serial.print(" Selected: ");
Serial.println(selectLeds);
fade=false;
christmas = false;
typeCommend = 4; //"select";
}
//Serial.println(typeCommend);
switch (typeCommend){
case 1: //normal
strip.clear();
Serial.print("Rood: ");
Serial.print( red );
Serial.print(" - Groen: ");
Serial.print( green );
Serial.print( " - Blauw: ");
Serial.println( blue);
stripcolor = strip.Color(red,green, blue);
strip.fill(stripcolor, 0, 150);
strip.show();
break;
case 2: //fade:
doFade();
break;
case 3: //pref1:
{
strip.clear();
int arrayPref[] = {1,2,34,35,36,37,90,91,92,93,149,150,0}; //End with a 0
for ( int i = 0; i<20; i++ ) { // 20 is size of array
if (arrayPref[i] == 0) {
break;
}
strip.setPixelColor(arrayPref[i]-1, strip.Color(red,green,blue));
}
strip.show();
}
break;
case 4: // select
strip.clear();
ind1 = 0;
while (ind1 >= 0) {
ind2 = selectLeds.indexOf(",", ind1+1);
// Serial.println(selectLeds.substring(ind1, ind2));
strip.setPixelColor(selectLeds.substring(ind1, ind2).toInt(), strip.Color(red,green,blue));
ind1 = ind2+1;
if (ind2 < 0 ) {
ind1 = -4;
}
}
strip.show();
typeCommend = 0;
break;
case 2512: //Special for christmass
// int arrayRed[] = {10,40,40,10,0,0,0,0,0,0};
// int arrayGreen[] = {0,0,0,0,0,10,40,40,10,0};
// int arrayBlue[] = {0,0,0,0,0,0,0,0,0,0};
// for (int i = 0; i< LED_COUNT - 10; i=i+10) {
// for (int j = 0; j<10; j++) {
// strip.setPixelColor(i+j, strip.Color(arrayRed[j],arrayGreen[j],arrayBlue[j]));
// }
// }
// strip.show();
// Serial.println("2512 change");
cmDelay = 0;
christmas = true;
break;
}
// Displayingen a dafault page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
client.println("<body><h1>Roggekamp LED verlichting gang</h1>");
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>green - State " + greenstate + "</p>");
// If the green LED is off, it displays the ON button
if (greenstate == "off") {
client.println("<p><a href=\"/green/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/green/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>red - State " + redstate + "</p>");
// If the green LED is off, it displays the ON button
if (redstate == "off") {
client.println("<p><a href=\"/red/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/red/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 5
client.println("<p>blue - State " + bluestate + "</p>");
// If the green LED is off, it displays the ON button
if (bluestate == "off") {
client.println("<p><a href=\"/blue/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/blue/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>fade</p>");
// If the fade is off, it displays the ON button
if (fade == false) {
client.println("<p><a href=\"/fade/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/fade/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
}
}
}
i2cloop();
delay(200);
}
void doChristmas() {
if (cmDelay < 1 or cmDelay > 10) { // delay counter
cmDelay = 1;
cmCounter++;
if (cmCounter > 9) {
cmCounter=0;
}
int arrayRed[] = {10,40,40,10,0,0,0,0,0,0,10,40,40,10,0,0,0,0,0,0};
int arrayGreen[] = {0,0,0,0,0,10,40,40,10,0,0,0,0,0,0,10,40,40,10,0};
int arrayBlue[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
strip.clear();
for (int i = 0; i< LED_COUNT - 10; i=i+10) {
for (int j = 0; j<10; j++) {
strip.setPixelColor(i+j, strip.Color(arrayRed[j+cmCounter],arrayGreen[j+cmCounter],arrayBlue[j+cmCounter]));
}
}
strip.show();
Serial.println("2512 change");
} else {
cmDelay++;
}
}
void doFade() {
if (redUP){
red++;
if (red > 254) {
redUP = false;
}
}else {
red--;
if (red < 1) {
redUP = true;
}
}
if (greenUP){
green++;
if (green > 254) {
greenUP = false;
green=255;
}
}else {
green--;
if (green < 1) {
greenUP = true;
green = 0;
}
}
if (blueUP){
blue++;
if (blue > 254) {
blueUP = false;
blue = 255;
}
}else {
blue--;
if (blue < 1) {
blueUP = true;
blue = 0;
}
}
Serial.print("Rood: ");
Serial.print( red );
Serial.print(" - Groen: ");
Serial.print( green );
Serial.print( " - Blauw: ");
Serial.println( blue);
stripcolor = strip.Color(red,green, blue);
strip.fill(stripcolor, 0, 150);
strip.show();
}
void Neopixel_off() {
// All leds off
strip.clear();
strip.show();
}
void Neopixel_1() {
// strip.fill(white,0);
// strip.show();
}
static void chase(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels()+4; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i-4, 0); // Erase pixel a few steps back
strip.show();
delay(25);
}
}
void i2cloop() {
if (lightCount == 100 and luxMeter !=0 ) { // If no sesor found, luxMeter=0
lightCount = 0;
float lux = lightMeter.readLightLevel(true);
if (lux > 0) {
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
sprintf(domoURL,"http://%s/json.htm?type=command¶m=udevice&idx=%i&svalue=%0.1f", domoIP, lightIDX, lux) ;
http.begin(domoURL);
int httpResponseCode = http.GET(); // Fire and forget: No check on good reception of the call
Serial.println(domoURL);
Serial.println(httpResponseCode);
// while (client.available()) {
// char c = client.read();
// Serial.print(c);
// }
}
delay(500);
} else {
lightCount++;
}
}
int BH1750_Read(int address) //
{
// int i=0;
// Wire.beginTransmission(address);
// Wire.requestFrom(address, 2);
// while(Wire.available()) //
// {
// buff[i] = Wire.read(); // receive one byte
// i++;
// }
// Wire.endTransmission();
// return i;
}
void BH1750_Init(int address)
{
// Wire.beginTransmission(address);
// Wire.write(0x10);//1lx reolution 120ms
// Wire.endTransmission();
}