This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266_ws2801fx_mqtt.ino
756 lines (593 loc) · 20.9 KB
/
esp8266_ws2801fx_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
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#include <FS.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Ticker.h>
#include <WiFiManager.h>
#include <WS2801FX.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
// * Include settings
#include "settings.h"
#include <PubSubClient.h>
// * Include html content constants
#include "content/index.html.h"
#include "content/main.js.h"
// * Initiate led blinker library
Ticker ticker;
// * Initiate the watchdog
Ticker tickerOSWatch;
// * Initiate HTTP server
ESP8266WebServer webserver(HTTP_PORT);
// * Initiate Led driver
WS2801FX ledstrip = WS2801FX(LED_COUNT, LED_DATA_PIN, LED_CLOCK_PIN, LED_PIXEL_ORDER);
// * Initiate WIFI client
WiFiClient espClient;
// * Initiate MQTT client
PubSubClient mqtt_client(espClient);
// **********************************
// * Watchdog *
// **********************************
void ICACHE_RAM_ATTR osWatch(void)
{
unsigned long t = millis();
unsigned long last_run = abs(t - last_loop);
if(last_run >= (OSWATCH_RESET_TIME * 1000)) {
// save the hit here to eeprom or to rtc memory if needed
ESP.restart(); // normal reboot
//ESP.reset(); // hard reset
}
}
// **********************************
// * WIFI *
// **********************************
// * Gets called when WiFiManager enters configuration mode
void configModeCallback(WiFiManager *myWiFiManager)
{
#ifdef DEBUG_PRINT
Serial.println(F("Entered config mode"));
Serial.println(WiFi.softAPIP());
// * If you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
#endif
// * Entered config mode, make led toggle faster
ticker.attach(0.2, tick);
}
// **********************************
// * Ticker (System LED Blinker) *
// **********************************
// * Blink on-board Led
void tick()
{
// * Toggle state
int state = digitalRead(LED_BUILTIN); // * Get the current state of GPIO1 pin
digitalWrite(LED_BUILTIN, !state); // * Set pin to the opposite state
}
// **********************************
// * Led Controller *
// **********************************
// * Get the current ON or OFF state of the ledstrip
const char* get_on_or_off_state()
{
// * If either stopped or one in brightness, color or mode are zero, return OFF
return ((!ledstrip.isRunning()) || ledstrip.getMode() == uint8_t(0) || ledstrip.getBrightness() == uint8_t(0) || ledstrip.getColor() == uint32_t(0)) ? STATE_OFF : STATE_ON;
}
// * Update the current state of the led controller as a json object
void update_json_output_buffer()
{
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& color = root.createNestedObject("color");
uint32_t hexcolor = ledstrip.getColor();
color["r"] = ((hexcolor >> 16) & 0xFF);
color["g"] = ((hexcolor >> 8) & 0xFF);
color["b"] = (hexcolor & 0xFF);
root["effect"] = ledstrip.getMode();
root["brightness"] = ledstrip.getBrightness();
root["speed"] = ledstrip.getSpeed();
root["state"] = get_on_or_off_state();
root.printTo(LEDSTRIP_JSON_OUTPUT, sizeof(LEDSTRIP_JSON_OUTPUT));
}
// * Process the incoming json message by dispatching each element to it's own library function
bool process_json_input(char* payload)
{
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
if (root.success()) {
// * Process State
if (root.containsKey("state")) {
if (root["state"] == "OFF") {
ledstrip.stop();
// * Do not process anything else when turned OFF
return true;
}
else if (root["state"] == "ON") {
ledstrip.start();
if (ledstrip.getMode() == uint8_t(0) && !root.containsKey("effect")) {
ledstrip.setMode(DEFAULT_BASE_MODE);
}
}
}
// * Process Color
if (root.containsKey("color")) {
uint8_t red = root["color"]["r"];
uint8_t green = root["color"]["g"];
uint8_t blue = root["color"]["b"];
ledstrip.setColor(red, green, blue);
}
// * Process FX Mode
if (root.containsKey("effect")) {
uint8_t new_led_mode, current_led_mode;
current_led_mode = ledstrip.getMode();
new_led_mode = (uint8_t) strtol(root["effect"], NULL, 10);
if (current_led_mode != new_led_mode) {
ledstrip.setMode(new_led_mode % ledstrip.getModeCount());
}
}
// * Process Brightness
if (root.containsKey("brightness")) {
uint8_t current_led_brightness, new_led_brightness;
current_led_brightness = ledstrip.getBrightness();
new_led_brightness = (uint8_t) strtol(root["brightness"], NULL, 10);
if (new_led_brightness >= uint8_t(0) || new_led_brightness <= uint8_t(255)) {
if (current_led_brightness != new_led_brightness) {
ledstrip.setBrightness(new_led_brightness);
}
}
}
// * Process Speed
if (root.containsKey("speed")) {
uint8_t current_led_speed, new_led_speed;
current_led_speed = ledstrip.getSpeed();
new_led_speed = (uint8_t) strtol(root["speed"], NULL, 10);
if (new_led_speed >= uint8_t(0) || new_led_speed <= uint8_t(255)) {
if (current_led_speed != new_led_speed) {
ledstrip.setSpeed(new_led_speed);
}
}
}
return true;
}
return false;
}
// * Initiate the ledstrip controller library
void setup_led_controller()
{
ledstrip.init();
ledstrip.setMode(DEFAULT_BASE_MODE);
ledstrip.setColor(DEFAULT_BASE_COLOR);
ledstrip.setSpeed(DEFAULT_BASE_SPEED);
ledstrip.setBrightness(DEFAULT_BASE_BRIGHTNESS);
ledstrip.start();
#ifdef DEBUG_PRINT
Serial.println(F("Led Controller Started"));
#endif
}
// **********************************
// * Webserver functions *
// **********************************
// * The main web page
void webserver_handle_index_html()
{
// * Send the main index file
webserver.send_P(200, "text/html", index_html);
}
// * Javascript endpoint
void webserver_handle_main_js()
{
// * Send "our" javascript code
webserver.send_P(200, "application/javascript", main_js);
}
// * Send a html table with all effect modes
void webserver_handle_modes()
{
// * Return a html table with all modes the library provides
webserver.send(200, "text/plain", modes);
}
// * Handle json input string
void webserver_handle_json()
{
// * Handle json input from http webserver
String json_input = webserver.arg("plain");
#ifdef DEBUG_PRINT
Serial.print(F("HTTP Incoming: "));
Serial.println(json_input);
#endif
bool result = process_json_input((char*) json_input.c_str());
if (result) {
send_mqtt_state();
webserver.send(200, "application/json", F("{\"success\":\"true\",\"message\":\"OK\"}"));
} else {
#ifdef DEBUG_PRINT
Serial.println(F("Error input from HTTP input: parseObject() failed."));
#endif
webserver.send(500, "application/json", F("{\"success\":\"false\",\"message\":\"Update Failed\"}"));
}
}
// * Return the status of the ledstrip as a json string
void webserver_handle_status()
{
// * Update the current state
update_json_output_buffer();
// * Send the current json status to the client
webserver.send(200, "application/json", LEDSTRIP_JSON_OUTPUT);
}
// * Return ON or OFF depending on the state of the ledstrip
void webserver_handle_on_or_off_state()
{
const char* state = get_on_or_off_state();
webserver.send(200, "text/plain", state);
}
// * Handle input commands from the web interface
void webserver_handle_set()
{
for (uint8_t argument=0; argument < webserver.args(); argument++){
// * Process Color
if (webserver.argName(argument) == "c") {
uint32_t color = (uint32_t) strtol(webserver.arg(argument).c_str(), NULL, 16);
if(color >= 0x000000 && color <= 0xFFFFFF) {
ledstrip.setColor(color);
}
}
// * Process FX Mode
if (webserver.argName(argument) == "m") {
uint8_t led_mode = (uint8_t) strtol(webserver.arg(argument).c_str(), NULL, 10);
ledstrip.setMode(led_mode % ledstrip.getModeCount());
}
// * Process Brightness
if (webserver.argName(argument) == "b") {
if(webserver.arg(argument)[0] == '-') {
ledstrip.decreaseBrightness(BRIGHTNESS_STEP);
} else {
ledstrip.increaseBrightness(BRIGHTNESS_STEP);
}
}
// * Process Speed
if (webserver.argName(argument) == "s") {
if(webserver.arg(argument)[0] == '-') {
ledstrip.decreaseSpeed(SPEED_STEP);
} else {
ledstrip.increaseSpeed(SPEED_STEP);
}
}
}
send_mqtt_state();
webserver.send(200, "text/plain", F("OK"));
}
// * Return a 404 when the page does not exist
void webserver_handle_not_found()
{
// * Handle 404 not found error
webserver.send(404, "text/plain", F("File Not Found"));
}
// * Get all effect modes from ws2801 library and create a html object that's included in the frontpage
void setup_led_html_modes()
{
for(uint8_t i=0; i < ledstrip.getModeCount(); i++) {
modes += "<li><a href='#' class='m' id='";
modes += i;
modes += "'>";
modes += ledstrip.getModeName(i);
modes += "</a></li>";
}
}
// * Setup the webserver
void setup_webserver()
{
#ifdef DEBUG_PRINT
Serial.println(F("HTTP Webserver setup"));
#endif
webserver.on("/", webserver_handle_index_html);
webserver.on("/main.js", webserver_handle_main_js);
webserver.on("/modes", webserver_handle_modes);
webserver.on("/set", webserver_handle_set);
webserver.on("/on-or-off", webserver_handle_on_or_off_state);
webserver.on("/status", webserver_handle_status);
webserver.on("/json", webserver_handle_json);
webserver.onNotFound(webserver_handle_not_found);
webserver.begin();
#ifdef DEBUG_PRINT
Serial.println(F("HTTP Webserver Started"));
#endif
}
// **********************************
// * MQTT *
// **********************************
// * Send the current state to the MQTT broker
void send_mqtt_state()
{
update_json_output_buffer();
#ifdef DEBUG_PRINT
Serial.print(F("MQTT Outgoing: "));
Serial.println(LEDSTRIP_JSON_OUTPUT);
#endif
bool result = mqtt_client.publish(MQTT_OUT_TOPIC, LEDSTRIP_JSON_OUTPUT, true);
if (!result) {
#ifdef DEBUG_PRINT
Serial.println(F("MQTT publish failed "));
#endif
}
}
// * Callback for incoming MQTT messages
void mqtt_callback(char* topic, byte* payload_in, unsigned int length)
{
char* payload = (char *) malloc(length + 1);
memcpy(payload, payload_in, length);
payload[length] = 0;
#ifdef DEBUG_PRINT
Serial.print(F("MQTT Incoming: "));
Serial.println(payload);
#endif
bool result = process_json_input(payload);
free(payload);
// * If processing ran successfully, send state to mqtt out topic
if (result) {
send_mqtt_state();
} else {
#ifdef DEBUG_PRINT
Serial.println(F("Error input from MQTT broker: parseObject() failed."));
#endif
}
}
// * Reconnect to MQTT server and subscribe to in and out topics
bool mqtt_reconnect()
{
// * Loop until we're reconnected
int MQTT_RECONNECT_RETRIES = 0;
while (!mqtt_client.connected() && MQTT_RECONNECT_RETRIES < MQTT_MAX_RECONNECT_TRIES) {
MQTT_RECONNECT_RETRIES++;
#ifdef DEBUG_PRINT
Serial.printf("MQTT connection attempt %d / %d ...\n", MQTT_RECONNECT_RETRIES, MQTT_MAX_RECONNECT_TRIES);
#endif
// * Attempt to connect
if (mqtt_client.connect(HOSTNAME, MQTT_USER, MQTT_PASS)) {
#ifdef DEBUG_PRINT
Serial.println(F("MQTT connected!"));
#endif
// * Once connected, publish an announcement...
char * message = new char[23 + strlen(HOSTNAME) + 1];
strcpy(message, "ws2801 ledstrip alive: ");
strcat(message, HOSTNAME);
mqtt_client.publish("hass/status", message);
// * Resubscribe to the incoming mqtt topic
mqtt_client.subscribe(MQTT_IN_TOPIC);
#ifdef DEBUG_PRINT
Serial.printf("MQTT topic in: %s\n", MQTT_IN_TOPIC);
Serial.printf("MQTT topic out: %s\n", MQTT_OUT_TOPIC);
#endif
} else {
#ifdef DEBUG_PRINT
Serial.print(F("MQTT Connection failed: rc="));
Serial.println(mqtt_client.state());
Serial.println(F(" Retrying in 5 seconds"));
Serial.println("");
#endif
// * Wait 5 seconds before retrying
delay(5000);
}
}
if (MQTT_RECONNECT_RETRIES >= MQTT_MAX_RECONNECT_TRIES) {
#ifdef DEBUG_PRINT
Serial.printf("*** MQTT connection failed, giving up after %d tries ...\n", MQTT_RECONNECT_RETRIES);
#endif
return false;
}
return true;
}
// **********************************
// * EEPROM helpers *
// **********************************
String read_eeprom(int offset, int len)
{
String res = "";
for (int i = 0; i < len; ++i) {
res += char(EEPROM.read(i + offset));
}
#ifdef DEBUG_PRINT
Serial.print(F("read_eeprom(): "));
Serial.println(res.c_str());
#endif
return res;
}
void write_eeprom(int offset, int len, String value)
{
#ifdef DEBUG_PRINT
Serial.print(F("write_eeprom(): "));
Serial.println(value.c_str());
#endif
for (int i = 0; i < len; ++i) {
if ((unsigned)i < value.length()) {
EEPROM.write(i + offset, value[i]);
} else {
EEPROM.write(i + offset, 0);
}
}
}
// ******************************************
// * Callback for saving WIFI config *
// ******************************************
bool shouldSaveConfig = false;
// * Callback notifying us of the need to save config
void save_wifi_config_callback ()
{
#ifdef DEBUG_PRINT
Serial.println(F("Should save config"));
#endif
shouldSaveConfig = true;
}
// **********************************
// * Setup OTA *
// **********************************
void setup_ota()
{
#ifdef DEBUG_PRINT
Serial.println(F("Arduino OTA activated."));
#endif
// * Port defaults to 8266
ArduinoOTA.setPort(8266);
// * Set hostname for OTA
ArduinoOTA.setHostname(HOSTNAME);
ArduinoOTA.setPassword(OTA_PASSWORD);
#ifdef DEBUG_PRINT
ArduinoOTA.onStart([]() {
Serial.println(F("Arduino OTA: Start"));
});
ArduinoOTA.onEnd([]() {
Serial.println(F("Arduino OTA: End (Running reboot)"));
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Arduino OTA Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Arduino OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println(F("Arduino OTA: Auth Failed"));
else if (error == OTA_BEGIN_ERROR)
Serial.println(F("Arduino OTA: Begin Failed"));
else if (error == OTA_CONNECT_ERROR)
Serial.println(F("Arduino OTA: Connect Failed"));
else if (error == OTA_RECEIVE_ERROR)
Serial.println(F("Arduino OTA: Receive Failed"));
else if (error == OTA_END_ERROR)
Serial.println(F("Arduino OTA: End Failed"));
});
#endif
ArduinoOTA.begin();
#ifdef DEBUG_PRINT
Serial.println(F("Arduino OTA finished"));
#endif
}
// **********************************
// * Setup MDNS discovery service *
// **********************************
void setup_mdns()
{
bool mdns_result = MDNS.begin(HOSTNAME);
#ifdef DEBUG_PRINT
Serial.println(F("Starting MDNS responder service"));
#endif
if (mdns_result) {
MDNS.addService("http", "tcp", 80);
}
}
// **********************************
// * Setup Main *
// **********************************
void setup()
{
// * Update watchdog value
last_loop = millis();
// Initiate watchdog ticker
tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), osWatch);
// * Configure Serial and EEPROM
Serial.begin(BAUD_RATE);
EEPROM.begin(512);
// * Set led pin as output
pinMode(LED_BUILTIN, OUTPUT);
// * Start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.6, tick);
// * Get MQTT Server settings
String settings_available = read_eeprom(134, 1);
if (settings_available == "1") {
read_eeprom(0, 64).toCharArray(MQTT_HOST, 64); // * 0-63
read_eeprom(64, 6).toCharArray(MQTT_PORT, 6); // * 64-69
read_eeprom(70, 32).toCharArray(MQTT_USER, 32); // * 70-101
read_eeprom(102, 32).toCharArray(MQTT_PASS, 32); // * 102-133
}
WiFiManagerParameter CUSTOM_MQTT_HOST("host", "MQTT hostname", MQTT_HOST, 64);
WiFiManagerParameter CUSTOM_MQTT_PORT("port", "MQTT port", MQTT_PORT, 6);
WiFiManagerParameter CUSTOM_MQTT_USER("user", "MQTT user", MQTT_USER, 32);
WiFiManagerParameter CUSTOM_MQTT_PASS("pass", "MQTT pass", MQTT_PASS, 32);
// * WiFiManager local initialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
// * Reset settings - uncomment for testing
// wifiManager.resetSettings();
// * Set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
// * Set timeout
wifiManager.setConfigPortalTimeout(WIFI_TIMEOUT);
// * Set save config callback
wifiManager.setSaveConfigCallback(save_wifi_config_callback);
// * Add all your parameters here
wifiManager.addParameter(&CUSTOM_MQTT_HOST);
wifiManager.addParameter(&CUSTOM_MQTT_PORT);
wifiManager.addParameter(&CUSTOM_MQTT_USER);
wifiManager.addParameter(&CUSTOM_MQTT_PASS);
// * Fetches SSID and pass and tries to connect
// * Reset when no connection after 10 seconds
if (!wifiManager.autoConnect()) {
#ifdef DEBUG_PRINT
Serial.println(F("Failed to connect to WIFI and hit timeout"));
#endif
// * Reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(WIFI_TIMEOUT);
}
// * Read updated parameters
strcpy(MQTT_HOST, CUSTOM_MQTT_HOST.getValue());
strcpy(MQTT_PORT, CUSTOM_MQTT_PORT.getValue());
strcpy(MQTT_USER, CUSTOM_MQTT_USER.getValue());
strcpy(MQTT_PASS, CUSTOM_MQTT_PASS.getValue());
// * Save the custom parameters to FS
if (shouldSaveConfig) {
#ifdef DEBUG_PRINT
Serial.println(F("Saving WiFiManager config"));
#endif
write_eeprom(0, 64, MQTT_HOST); // * 0-63
write_eeprom(64, 6, MQTT_PORT); // * 64-69
write_eeprom(70, 32, MQTT_USER); // * 70-101
write_eeprom(102, 32, MQTT_PASS); // * 102-133
write_eeprom(134, 1, "1"); // * 134 --> always "1"
EEPROM.commit();
}
// * If you get here you have connected to the WiFi
#ifdef DEBUG_PRINT
Serial.println(F("Connected to WIFI..."));
#endif
// * Keep LED on
ticker.detach();
digitalWrite(LED_BUILTIN, LOW);
// * Configure OTA
setup_ota();
// * Configure LED Controller
setup_led_controller();
// * Fill Controller Modes HTML
setup_led_html_modes();
// * Setup Webserver
setup_webserver();
// * Startup MDNS Service
setup_mdns();
// * Configure MQTT
snprintf(MQTT_IN_TOPIC, sizeof MQTT_IN_TOPIC, "lights/ws2801/%s/in", HOSTNAME);
snprintf(MQTT_OUT_TOPIC, sizeof MQTT_OUT_TOPIC, "lights/ws2801/%s/out", HOSTNAME);
mqtt_client.setServer(MQTT_HOST, atoi(MQTT_PORT));
mqtt_client.setCallback(mqtt_callback);
#ifdef DEBUG_PRINT
Serial.printf("MQTT active: %s:%s\n", MQTT_HOST, MQTT_PORT);
#endif
}
// **********************************
// * Loop *
// **********************************
void loop()
{
last_loop = millis();
ledstrip.service();
webserver.handleClient();
ArduinoOTA.handle();
if (!mqtt_client.connected()) {
long now = millis();
if (now - LAST_RECONNECT_ATTEMPT > 5000) {
LAST_RECONNECT_ATTEMPT = now;
if (mqtt_reconnect()) {
LAST_RECONNECT_ATTEMPT = 0;
}
}
}
else {
mqtt_client.loop();
}
}