Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MQTT Status Topic (RSSI, Uptime, LWAT) #27

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 51 additions & 31 deletions DrZzs_LEDs_Pub.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

#include <ESP8266WiFi.h>
#include <SimpleTimer.h> //Additional Lib Required
#include <PubSubClient.h>
#define FASTLED_INTERRUPT_RETRY_COUNT 0
#include <FastLED.h>
#define FL_ALIGN_PROGMEM __attribute__ ((aligned (4))) //From Pull Request #25

#include <ArduinoOTA.h>
#include <ESP8266mDNS.h>
Expand All @@ -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//////////////////////////////////

Expand Down Expand Up @@ -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 ************************************/

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -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");
}

Expand All @@ -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");
}

Expand All @@ -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()) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -782,7 +805,6 @@ void loop() {
}



if (setEffect == "Police One") { //POLICE LIGHTS (TWO COLOR SINGLE LED)
idex++;
if (idex >= NUM_LEDS) {
Expand All @@ -802,7 +824,6 @@ void loop() {
leds[i] = CHSV(0, 0, 0);
}
}

}

if (setEffect == "Police All") { //POLICE LIGHTS (TWO COLOR SOLID)
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -901,9 +918,7 @@ void loop() {
step ++; // Next step.
break;
}
}


}
}

EVERY_N_SECONDS(5) {
Expand Down Expand Up @@ -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);
}
}
Expand Down