diff --git a/DrZzs_LEDs_Pub.ino b/DrZzs_LEDs_Pub.ino index a5cf739..d1df1bb 100644 --- a/DrZzs_LEDs_Pub.ino +++ b/DrZzs_LEDs_Pub.ino @@ -1,8 +1,10 @@ #include +#include //Additional Lib Required #include #define FASTLED_INTERRUPT_RETRY_COUNT 0 #include +#define FL_ALIGN_PROGMEM __attribute__ ((aligned (4))) //From Pull Request #25 #include #include @@ -26,6 +28,7 @@ int OTAport = 8266; #define COLOR_ORDER RGB //change to match your LED configuration // RGB for 2811's | GRB for 2812's // #define NUM_LEDS 175 //change to match your setup +int ReconnectCounter = 0; //Required to declare mqtt reconnect counter ///////////////DrZzs Palettes for custom BPM effects////////////////////////// ///////////////Add any custom palettes here////////////////////////////////// @@ -224,17 +227,19 @@ DEFINE_GRADIENT_PALETTE( Orange_to_Purple_gp ) { /****************************** MQTT TOPICS (change these topics as you wish) ***************************************/ -#define colorstatuspub "bruh/mqttstrip/colorstatus" -#define setcolorsub "bruh/mqttstrip/setcolor" -#define setpowersub "bruh/mqttstrip/setpower" -#define seteffectsub "bruh/mqttstrip/seteffect" -#define setbrightness "bruh/mqttstrip/setbrightness" +#define colorstatuspub SENSORNAME "/colorstatus" +#define setcolorsub SENSORNAME "/setcolor" +#define setpowersub SENSORNAME "/setpower" +#define seteffectsub SENSORNAME "/seteffect" +#define setbrightness SENSORNAME "/setbrightness" +#define setanimationspeed SENSORNAME "/setanimationspeed" -#define setcolorpub "bruh/mqttstrip/setcolorpub" -#define setpowerpub "bruh/mqttstrip/setpowerpub" -#define seteffectpub "bruh/mqttstrip/seteffectpub" -#define setbrightnesspub "bruh/mqttstrip/setbrightnesspub" -#define setanimationspeed "bruh/mqttstrip/setanimationspeed" +#define setOnlinePub SENSORNAME "/status" +#define setcolorpub SENSORNAME "/currentColor" +#define setpowerpub SENSORNAME "/currentPower" +#define seteffectpub SENSORNAME "/currentEffect" +#define setbrightnesspub SENSORNAME "/currentBrightness" +#define setanimationspeedpub SENSORNAME "/currentSpeed" /*************************** EFFECT CONTROL VARIABLES AND INITIALIZATIONS ************************************/ @@ -329,7 +334,7 @@ bool gReverseDirection = false; uint8_t gHue = 0; char message_buff[100]; - +SimpleTimer timer; WiFiClient espClient; //this needs to be unique for each controller PubSubClient client(espClient); //this needs to be unique for each controller @@ -399,9 +404,24 @@ void setup() { Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); - + + timer.setTimeout(120000, checkIn); } +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +void checkIn(){ + client.publish(setpowerpub, String(setPower).c_str(), true); //send power status + client.publish(seteffectpub, String(setEffect).c_str(), true); //send current effect + client.publish(setbrightnesspub, String(setBrightness).c_str(), true); //send current brightness + client.publish(setcolorpub, String(setColor).c_str(), true); //send current color + client.publish(setanimationspeedpub, String(setAnimationSpeed).c_str(), true); //set current animation speed + client.publish(SENSORNAME "/rssi", String(WiFi.RSSI()).c_str(), true); //send current signal strength + + client.publish(SENSORNAME "/minutesUptime", String((millis()/60000)).c_str(), true); //send uptime in minutes + timer.setTimeout(120000, checkIn); //set next status update +} void setup_wifi() { @@ -435,11 +455,11 @@ void callback(char* topic, byte* payload, unsigned int length) { setPower = String(message_buff); Serial.println("Set Power: " + setPower); if (setPower == "OFF") { - client.publish(setpowerpub, "OFF"); + client.publish(setpowerpub, "OFF", true); } if (setPower == "ON") { - client.publish(setpowerpub, "ON"); + client.publish(setpowerpub, "ON", true); } } @@ -452,6 +472,7 @@ void callback(char* topic, byte* payload, unsigned int length) { setEffect = String(message_buff); Serial.println("Set Effect: " + setEffect); setPower = "ON"; + client.publish(seteffectpub, String(setEffect).c_str(), true); client.publish(setpowerpub, "ON"); if (setEffect == "Twinkle") { twinklecounter = 0; @@ -471,6 +492,7 @@ void callback(char* topic, byte* payload, unsigned int length) { Serial.println("Set Brightness: " + setBrightness); brightness = setBrightness.toInt(); setPower = "ON"; + client.publish(setbrightnesspub, String(brightness).c_str(), true); client.publish(setpowerpub, "ON"); } @@ -483,6 +505,7 @@ void callback(char* topic, byte* payload, unsigned int length) { setColor = String(message_buff); Serial.println("Set Color: " + setColor); setPower = "ON"; + client.publish(setcolorpub, String(setColor).c_str(), true); client.publish(setpowerpub, "ON"); } @@ -493,11 +516,10 @@ void callback(char* topic, byte* payload, unsigned int length) { message_buff[i] = '\0'; setAnimationSpeed = String(message_buff); animationspeed = setAnimationSpeed.toInt(); + client.publish(setanimationspeedpub, String(setAnimationSpeed).c_str(), true); } } - - void loop() { if (!client.connected()) { @@ -506,6 +528,7 @@ void loop() { client.loop(); ArduinoOTA.handle(); + timer.run(); int Rcolor = setColor.substring(0, setColor.indexOf(',')).toInt(); int Gcolor = setColor.substring(setColor.indexOf(',') + 1, setColor.lastIndexOf(',')).toInt(); @@ -683,6 +706,7 @@ void loop() { dothue += 32; } } + if (setEffect == "Confetti" ) { // random colored speckles that blink in and fade smoothly fadeToBlackBy( leds, NUM_LEDS, 10); @@ -725,8 +749,7 @@ void loop() { fill_solid(leds, NUM_LEDS, CRGB(Rcolor, Gcolor, Bcolor)); } - - + if (setEffect == "Twinkle") { twinklecounter = twinklecounter + 1; if (twinklecounter < 2) { //Resets strip if previous animation was running @@ -782,7 +805,6 @@ void loop() { } - if (setEffect == "Police One") { //POLICE LIGHTS (TWO COLOR SINGLE LED) idex++; if (idex >= NUM_LEDS) { @@ -802,7 +824,6 @@ void loop() { leds[i] = CHSV(0, 0, 0); } } - } if (setEffect == "Police All") { //POLICE LIGHTS (TWO COLOR SOLID) @@ -859,10 +880,7 @@ void loop() { if (setEffect == "Fire") { Fire2012WithPalette(); } - random16_add_entropy( random8()); - - - + random16_add_entropy( random8()); EVERY_N_MILLISECONDS(10) { nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // FOR NOISE ANIMATION @@ -879,7 +897,6 @@ void loop() { // In some sketches, I've used millis() instead of an incremented counter. Works a treat. } - if (setEffect == "Ripple") { for (int i = 0; i < NUM_LEDS; i++) leds[i] = CHSV(bgcol++, 255, 15); // Rotate background colour. switch (step) { @@ -901,9 +918,7 @@ void loop() { step ++; // Next step. break; } - } - - + } } EVERY_N_SECONDS(5) { @@ -1028,24 +1043,29 @@ void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect - if (client.connect(SENSORNAME, mqtt_user, mqtt_password)) { + if (client.connect(SENSORNAME, mqtt_user, mqtt_password, SENSORNAME "/status", 1, 1, "OFFLINE")) { //Added last will Serial.println("connected"); + ReconnectCounter = 0; //reset reconnect counter FastLED.clear (); //Turns off startup LEDs after connection is made FastLED.show(); client.subscribe(setcolorsub); client.subscribe(setbrightness); - //client.subscribe(setcolortemp); client.subscribe(setpowersub); client.subscribe(seteffectsub); client.subscribe(setanimationspeed); - client.publish(setpowerpub, "OFF"); + //client.publish(setpowerpub, "OFF"); //Removed as publish Topic behavior improved + client.publish(setOnlinePub, "ONLINE", true); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying + ReconnectCounter++; + if(ReconnectCounter==120){ //after 10 min of no connection + ESP.restart(); + } delay(5000); } }