forked from jasoncoon/esp8266-fastled-webserver
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathesp8266-fastled-mqtt.ino
executable file
·398 lines (314 loc) · 10.1 KB
/
esp8266-fastled-mqtt.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
/*
ESP8266 + FastLED + MQTT: https://github.com/jasoncoon/esp8266-fastled-webserver
Copyright (C) 2015 Jason Coon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// TODO Flickering LED's ... https://github.com/FastLED/FastLED/issues/394 or https://github.com/FastLED/FastLED/issues/306
#define FASTLED_ALLOW_INTERRUPTS 0
#define FASTLED_ESP8266_RAW_PIN_ORDER
//#define FASTLED_INTERRUPT_RETRY_COUNT 0
#include "FastLED.h"
FASTLED_USING_NAMESPACE
extern "C" {
#include "user_interface.h"
}
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h> // Only use it with SSL on MQTT Broker
#include <EEPROM.h>
#include <PubSubClient.h>
#include "GradientPalettes.h"
#include "Settings.h"
CRGB leds[NUM_LEDS];
uint8_t patternIndex = 0;
const uint8_t brightnessCount = 5;
uint8_t brightnessMap[brightnessCount] = { 16, 32, 64, 128, 255 };
int brightnessIndex = 0;
uint8_t brightness = brightnessMap[brightnessIndex];
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
// ten seconds per color palette makes a good demo
// 20-120 is better for deployment
#define SECONDS_PER_PALETTE 10
///////////////////////////////////////////////////////////////////////
// Forward declarations of an array of cpt-city gradient palettes, and
// a count of how many there are. The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;
// Current palette number from the 'playlist' of color palettes
uint8_t gCurrentPaletteNumber = 0;
CRGBPalette16 gCurrentPalette( CRGB::Black);
CRGBPalette16 gTargetPalette( gGradientPalettes[0] );
uint8_t currentPatternIndex = 0; // Index number of which pattern is current
bool autoplayEnabled = false;
uint8_t autoPlayDurationSeconds = 10;
unsigned int autoPlayTimeout = 0;
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
CRGB solidColor = CRGB::Black;
uint8_t power = 1;
// Mqtt Vars
WiFiClientSecure espClient;
PubSubClient client(espClient);
typedef void (*Pattern)();
typedef Pattern PatternList[];
typedef struct {
Pattern pattern;
String name;
} PatternAndName;
typedef PatternAndName PatternAndNameList[];
// List of patterns to cycle through. Each is defined as a separate function below.
#include "PatternLogics.h"
PatternAndNameList patterns = {
{ colorwaves, "Color Waves" },
{ palettetest, "Palette Test" },
{ pride, "Pride" },
{ rainbow, "Rainbow" },
{ rainbowWithGlitter, "Rainbow With Glitter" },
{ confetti, "Confetti" },
{ sinelon, "Sinelon" },
{ juggle, "Juggle" },
{ bpm, "BPM" },
{ fire, "Fire" },
{ showSolidColor, "Solid Color" },
};
const uint8_t patternCount = ARRAY_SIZE(patterns);
#include "Inits.h"
void setup(void) {
Serial.begin(115200);
delay(100);
//Serial.setDebugOutput(true);
EEPROM.begin(512);
loadSettings();
initFastLED();
logSys();
initWlan();
// Only to validate certs if u have problems ...
// verifytls();
//Mqtt Init
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
autoPlayTimeout = millis() + (autoPlayDurationSeconds * 1000);
}
// Format is: command:value
// value has to be a number, except rgb commands
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
char tmp[length + 1];
strncpy(tmp, (char*)payload, length);
tmp[length] = '\0';
String data(tmp);
Serial.printf("Received Data from Topic: %s", data.c_str());
Serial.println();
if ( data.length() > 0) {
if (data.startsWith("rgb(")) {
data.replace("rgb(","");
String r = getValue(data, ',', 0);
String g = getValue(data, ',', 1);
String b = getValue(data, ',', 2);
b.replace("hallo","");
Serial.printf("Received R: %s G: %s B: %s", r.c_str(), g.c_str(), b.c_str());
Serial.println();
if (r.length() > 0 && g.length() > 0 && b.length() > 0) {
setSolidColor(r.toInt(), g.toInt(), b.toInt());
}
}else {
String command = getValue(data, ':', 0);
String value = getValue(data, ':', 1);
if (command.length() > 0) {
if (command.equals("power")) {
if (isValidNumber(value)) {
setPower(value.toInt());
}
} else if (command.equals("solidcolor")) {
String r = getValue(data, ':', 2);
String g = getValue(data, ':', 4);
String b = getValue(data, ':', 6);
Serial.printf("Received R: %s G: %s B: %s", r.c_str(), g.c_str(), b.c_str());
Serial.println();
if (r.length() > 0 && g.length() > 0 && b.length() > 0) {
setSolidColor(r.toInt(), g.toInt(), b.toInt());
}
} else if (command.equals("pattern")) {
if (isValidNumber(value)) {
setPattern(value.toInt());
}
} else if (command.equals("brightness")) {
if (isValidNumber(value)) {
setBrightness(value.toInt());
}
} else if (command.equals("brightnessAdjust")) {
if (isValidNumber(value)) {
adjustBrightness(value.toInt() == 0 ? false : true);
}
} else if (command.equals("patternAdjust")) {
if (isValidNumber(value)) {
adjustPattern(value.toInt() == 0 ? false : true);
}
}
}
}
}
Serial.println("Finished Topic Data ...");
}
void loop(void) {
yield(); // Avoid crashes on ESP8266
// Add entropy to random number generator; we use a lot of it.
random16_add_entropy(random(65535));
if (!client.connected()) {
reconnectMqtt();
}
client.loop();
if (power == 0) {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(15);
return;
}
// EVERY_N_SECONDS(10) {
// Serial.print( F("Heap: ") ); Serial.println(system_get_free_heap_size());
// }
EVERY_N_MILLISECONDS( 20 ) {
gHue++; // slowly cycle the "base color" through the rainbow
}
// change to a new cpt-city gradient palette
EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
gCurrentPaletteNumber = addmod8( gCurrentPaletteNumber, 1, gGradientPaletteCount);
gTargetPalette = gGradientPalettes[ gCurrentPaletteNumber ];
}
// slowly blend the current cpt-city gradient palette to the next
EVERY_N_MILLISECONDS(40) {
nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 16);
}
if (autoplayEnabled && millis() > autoPlayTimeout) {
adjustPattern(true);
autoPlayTimeout = millis() + (autoPlayDurationSeconds * 1000);
}
// Call the current pattern function once, updating the 'leds' array
patterns[currentPatternIndex].pattern();
FastLED.show();
// insert a delay to keep the framerate modest
delay(1000 / FRAMES_PER_SECOND);
}
void reconnectMqtt() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect(mqtt_clientid, mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe(mqtt_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setPower(uint8_t value)
{
power = value == 0 ? 0 : 1;
EEPROM.write(5, power);
EEPROM.commit();
}
void setSolidColor(CRGB color)
{
setSolidColor(color.r, color.g, color.b);
}
void setSolidColor(uint8_t r, uint8_t g, uint8_t b)
{
solidColor = CRGB(r, g, b);
EEPROM.write(2, r);
EEPROM.write(3, g);
EEPROM.write(4, b);
setPattern(patternCount - 1);
}
// increase or decrease the current pattern number, and wrap around at theends
void adjustPattern(bool up)
{
if (up)
currentPatternIndex++;
else
currentPatternIndex--;
// wrap around at the ends
if (currentPatternIndex < 0)
currentPatternIndex = patternCount - 1;
if (currentPatternIndex >= patternCount)
currentPatternIndex = 0;
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
void setPattern(int value)
{
// don't wrap around at the ends
if (value < 0)
value = 0;
else if (value >= patternCount)
value = patternCount - 1;
currentPatternIndex = value;
EEPROM.write(1, currentPatternIndex);
EEPROM.commit();
}
// adjust the brightness, and wrap around at the ends
void adjustBrightness(bool up)
{
if (up)
brightnessIndex++;
else
brightnessIndex--;
// wrap around at the ends
if (brightnessIndex < 0)
brightnessIndex = brightnessCount - 1;
else if (brightnessIndex >= brightnessCount)
brightnessIndex = 0;
brightness = brightnessMap[brightnessIndex];
FastLED.setBrightness(brightness);
EEPROM.write(0, brightness);
EEPROM.commit();
}
void setBrightness(int value)
{
// don't wrap around at the ends
if (value > 255)
value = 255;
else if (value < 0) value = 0;
brightness = value;
FastLED.setBrightness(brightness);
EEPROM.write(0, brightness);
EEPROM.commit();
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
boolean isValidNumber(String str) {
// TODO replace with regex check
bool result = false;
for (byte i = 0; i < str.length(); i++)
{
if (isDigit(str.charAt(i))) {
result = true;
} else {
result = false;
break;
}
}
return result;
}