-
Notifications
You must be signed in to change notification settings - Fork 74
/
mqtt_esp8266_light.ino
551 lines (474 loc) · 13.5 KB
/
mqtt_esp8266_light.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
/*
* ESP8266 MQTT Lights for Home Assistant.
*
* Created DIY lights for Home Assistant using MQTT and JSON.
* This project supports single-color, RGB, and RGBW lights.
*
* Copy the included `config-sample.h` file to `config.h` and update
* accordingly for your setup.
*
* See https://github.com/corbanmailloux/esp-mqtt-rgb-led for more information.
*/
// Set configuration options for LED type, pins, WiFi, and MQTT in the following file:
#include "config.h"
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
// http://pubsubclient.knolleary.net/
#include <PubSubClient.h>
const bool rgb = (CONFIG_STRIP == RGB) || (CONFIG_STRIP == RGBW);
const bool includeWhite = (CONFIG_STRIP == BRIGHTNESS) || (CONFIG_STRIP == RGBW);
const int BUFFER_SIZE = JSON_OBJECT_SIZE(20);
// Maintained state for reporting to HA
byte red = 255;
byte green = 255;
byte blue = 255;
byte white = 255;
byte brightness = 255;
// Real values to write to the LEDs (ex. including brightness and state)
byte realRed = 0;
byte realGreen = 0;
byte realBlue = 0;
byte realWhite = 0;
bool stateOn = false;
// Globals for fade/transitions
bool startFade = false;
unsigned long lastLoop = 0;
int transitionTime = 0;
bool inFade = false;
int loopCount = 0;
int stepR, stepG, stepB, stepW;
int redVal, grnVal, bluVal, whtVal;
// Globals for flash
bool flash = false;
bool startFlash = false;
int flashLength = 0;
unsigned long flashStartTime = 0;
byte flashRed = red;
byte flashGreen = green;
byte flashBlue = blue;
byte flashWhite = white;
byte flashBrightness = brightness;
// Globals for colorfade
bool colorfade = false;
int currentColor = 0;
// {red, grn, blu, wht}
const byte colors[][4] = {
{255, 0, 0, 0},
{0, 255, 0, 0},
{0, 0, 255, 0},
{255, 80, 0, 0},
{163, 0, 255, 0},
{0, 255, 255, 0},
{255, 255, 0, 0}
};
const int numColors = 7;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
if (rgb) {
pinMode(CONFIG_PIN_RED, OUTPUT);
pinMode(CONFIG_PIN_GREEN, OUTPUT);
pinMode(CONFIG_PIN_BLUE, OUTPUT);
}
if (includeWhite) {
pinMode(CONFIG_PIN_WHITE, OUTPUT);
}
// Set the BUILTIN_LED based on the CONFIG_BUILTIN_LED_MODE
switch (CONFIG_BUILTIN_LED_MODE) {
case 0:
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, LOW);
break;
case 1:
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
break;
default: // Other options (like -1) are ignored.
break;
}
analogWriteRange(255);
if (CONFIG_DEBUG) {
Serial.begin(115200);
}
setup_wifi();
client.setServer(CONFIG_MQTT_HOST, CONFIG_MQTT_PORT);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(CONFIG_WIFI_SSID);
WiFi.mode(WIFI_STA); // Disable the built-in WiFi access point.
WiFi.begin(CONFIG_WIFI_SSID, CONFIG_WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/*
SAMPLE PAYLOAD (BRIGHTNESS):
{
"brightness": 120,
"flash": 2,
"transition": 5,
"state": "ON"
}
SAMPLE PAYLOAD (RGBW):
{
"brightness": 120,
"color": {
"r": 255,
"g": 100,
"b": 100
},
"white_value": 255,
"flash": 2,
"transition": 5,
"state": "ON",
"effect": "colorfade_fast"
}
*/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char message[length + 1];
for (int i = 0; i < length; i++) {
message[i] = (char)payload[i];
}
message[length] = '\0';
Serial.println(message);
if (!processJson(message)) {
return;
}
if (stateOn) {
// Update lights
realRed = map(red, 0, 255, 0, brightness);
realGreen = map(green, 0, 255, 0, brightness);
realBlue = map(blue, 0, 255, 0, brightness);
realWhite = map(white, 0, 255, 0, brightness);
}
else {
realRed = 0;
realGreen = 0;
realBlue = 0;
realWhite = 0;
}
startFade = true;
inFade = false; // Kill the current fade
sendState();
}
bool processJson(char* message) {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(message);
if (!root.success()) {
Serial.println("parseObject() failed");
return false;
}
if (root.containsKey("state")) {
if (strcmp(root["state"], CONFIG_MQTT_PAYLOAD_ON) == 0) {
stateOn = true;
}
else if (strcmp(root["state"], CONFIG_MQTT_PAYLOAD_OFF) == 0) {
stateOn = false;
}
}
// If "flash" is included, treat RGB and brightness differently
if (root.containsKey("flash") ||
(root.containsKey("effect") && strcmp(root["effect"], "flash") == 0)) {
if (root.containsKey("flash")) {
flashLength = (int)root["flash"] * 1000;
}
else {
flashLength = CONFIG_DEFAULT_FLASH_LENGTH * 1000;
}
if (root.containsKey("brightness")) {
flashBrightness = root["brightness"];
}
else {
flashBrightness = brightness;
}
if (rgb && root.containsKey("color")) {
flashRed = root["color"]["r"];
flashGreen = root["color"]["g"];
flashBlue = root["color"]["b"];
}
else {
flashRed = red;
flashGreen = green;
flashBlue = blue;
}
if (includeWhite && root.containsKey("white_value")) {
flashWhite = root["white_value"];
}
else {
flashWhite = white;
}
flashRed = map(flashRed, 0, 255, 0, flashBrightness);
flashGreen = map(flashGreen, 0, 255, 0, flashBrightness);
flashBlue = map(flashBlue, 0, 255, 0, flashBrightness);
flashWhite = map(flashWhite, 0, 255, 0, flashBrightness);
flash = true;
startFlash = true;
}
else if (rgb && root.containsKey("effect") &&
(strcmp(root["effect"], "colorfade_slow") == 0 || strcmp(root["effect"], "colorfade_fast") == 0)) {
flash = false;
colorfade = true;
currentColor = 0;
if (strcmp(root["effect"], "colorfade_slow") == 0) {
transitionTime = CONFIG_COLORFADE_TIME_SLOW;
}
else {
transitionTime = CONFIG_COLORFADE_TIME_FAST;
}
}
else if (colorfade && !root.containsKey("color") && root.containsKey("brightness")) {
// Adjust brightness during colorfade
// (will be applied when fading to the next color)
brightness = root["brightness"];
}
else { // No effect
flash = false;
colorfade = false;
if (rgb && root.containsKey("color")) {
red = root["color"]["r"];
green = root["color"]["g"];
blue = root["color"]["b"];
}
if (includeWhite && root.containsKey("white_value")) {
white = root["white_value"];
}
if (root.containsKey("brightness")) {
brightness = root["brightness"];
}
if (root.containsKey("transition")) {
transitionTime = root["transition"];
}
else {
transitionTime = 0;
}
}
return true;
}
void sendState() {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? CONFIG_MQTT_PAYLOAD_ON : CONFIG_MQTT_PAYLOAD_OFF;
if (rgb) {
JsonObject& color = root.createNestedObject("color");
color["r"] = red;
color["g"] = green;
color["b"] = blue;
}
root["brightness"] = brightness;
if (includeWhite) {
root["white_value"] = white;
}
if (rgb && colorfade) {
if (transitionTime == CONFIG_COLORFADE_TIME_SLOW) {
root["effect"] = "colorfade_slow";
}
else {
root["effect"] = "colorfade_fast";
}
}
else {
root["effect"] = "null";
}
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
client.publish(CONFIG_MQTT_TOPIC_STATE, buffer, true);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CONFIG_MQTT_CLIENT_ID, CONFIG_MQTT_USER, CONFIG_MQTT_PASS)) {
Serial.println("connected");
client.subscribe(CONFIG_MQTT_TOPIC_SET);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setColor(int inR, int inG, int inB, int inW) {
if (CONFIG_INVERT_LED_LOGIC) {
inR = (255 - inR);
inG = (255 - inG);
inB = (255 - inB);
inW = (255 - inW);
}
if (rgb) {
analogWrite(CONFIG_PIN_RED, inR);
analogWrite(CONFIG_PIN_GREEN, inG);
analogWrite(CONFIG_PIN_BLUE, inB);
}
if (includeWhite) {
analogWrite(CONFIG_PIN_WHITE, inW);
}
if (CONFIG_DEBUG) {
Serial.print("Setting LEDs: {");
if (rgb) {
Serial.print("r: ");
Serial.print(inR);
Serial.print(" , g: ");
Serial.print(inG);
Serial.print(" , b: ");
Serial.print(inB);
}
if (includeWhite) {
if (rgb) {
Serial.print(", ");
}
Serial.print("w: ");
Serial.print(inW);
}
Serial.println("}");
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (flash) {
if (startFlash) {
startFlash = false;
flashStartTime = millis();
}
if ((millis() - flashStartTime) <= flashLength) {
if ((millis() - flashStartTime) % 1000 <= 500) {
setColor(flashRed, flashGreen, flashBlue, flashWhite);
}
else {
setColor(0, 0, 0, 0);
// If you'd prefer the flashing to happen "on top of"
// the current color, uncomment the next line.
// setColor(realRed, realGreen, realBlue, realWhite);
}
}
else {
flash = false;
setColor(realRed, realGreen, realBlue, realWhite);
}
}
else if (rgb && colorfade && !inFade) {
realRed = map(colors[currentColor][0], 0, 255, 0, brightness);
realGreen = map(colors[currentColor][1], 0, 255, 0, brightness);
realBlue = map(colors[currentColor][2], 0, 255, 0, brightness);
realWhite = map(colors[currentColor][3], 0, 255, 0, brightness);
currentColor = (currentColor + 1) % numColors;
startFade = true;
}
if (startFade) {
// If we don't want to fade, skip it.
if (transitionTime == 0) {
setColor(realRed, realGreen, realBlue, realWhite);
redVal = realRed;
grnVal = realGreen;
bluVal = realBlue;
whtVal = realWhite;
startFade = false;
}
else {
loopCount = 0;
stepR = calculateStep(redVal, realRed);
stepG = calculateStep(grnVal, realGreen);
stepB = calculateStep(bluVal, realBlue);
stepW = calculateStep(whtVal, realWhite);
inFade = true;
}
}
if (inFade) {
startFade = false;
unsigned long now = millis();
if (now - lastLoop > transitionTime) {
if (loopCount <= 1020) {
lastLoop = now;
redVal = calculateVal(stepR, redVal, loopCount);
grnVal = calculateVal(stepG, grnVal, loopCount);
bluVal = calculateVal(stepB, bluVal, loopCount);
whtVal = calculateVal(stepW, whtVal, loopCount);
setColor(redVal, grnVal, bluVal, whtVal); // Write current values to LED pins
Serial.print("Loop count: ");
Serial.println(loopCount);
loopCount++;
}
else {
inFade = false;
}
}
}
}
// From https://www.arduino.cc/en/Tutorial/ColorCrossfader
/* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
*
* The program works like this:
* Imagine a crossfade that moves the red LED from 0-10,
* the green from 0-5, and the blue from 10 to 7, in
* ten steps.
* We'd want to count the 10 steps and increase or
* decrease color values in evenly stepped increments.
* Imagine a + indicates raising a value by 1, and a -
* equals lowering it. Our 10 step fade would look like:
*
* 1 2 3 4 5 6 7 8 9 10
* R + + + + + + + + + +
* G + + + + +
* B - - -
*
* The red rises from 0 to 10 in ten steps, the green from
* 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
*
* In the real program, the color percentages are converted to
* 0-255 values, and there are 1020 steps (255*4).
*
* To figure out how big a step there should be between one up- or
* down-tick of one of the LED values, we call calculateStep(),
* which calculates the absolute gap between the start and end values,
* and then divides that gap by 1020 to determine the size of the step
* between adjustments in the value.
*/
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
/* The next function is calculateVal. When the loop value, i,
* reaches the step size appropriate for one of the
* colors, it increases or decreases the value of that color by 1.
* (R, G, and B are each calculated separately.)
*/
int calculateVal(int step, int val, int i) {
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255) {
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}