So I am stuck on this one. I have some led lights that I am trying to get to work. I have two strips that are connected to a NodeMCU (one on pin 5 and one on pin 6) I want to be able to control them separately. I am trying to control them using MQTT. I used BRUH Automation’s code for the most part and it works if I don’t seperate them out to two pins. I have since added a second set of topics and modified my code to include a second pin. I have tried lots of things and now have gone back to removing most of the code to see if I could find the problem. What I came across is that if I include the following code in my arduino then I can no longer turn the leds on or off for either pin:
client.subscribe(setcolorsub2);
I comment this out it seems to work.
Here is the rest of my code if that helps. I am just stuck and probably missing something simple.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "FastLED.h"
/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid "Omit" //enter your WIFI SSID
#define wifi_password "Omit" //enter your WIFI Password
#define mqtt_server "Omit" // Enter your MQTT server adderss or IP. I use my DuckDNS adddress (yourname.duckdns.org) in this field
#define mqtt_user "Omit" //enter your MQTT username
#define mqtt_password "Omit" //enter your password
/************ FastLED Defintions ******************/
#define DATA_PIN 6 //on the NodeMCU 1.0, FastLED will default to the D5 pin after throwing an error during compiling. Leave as is.
#define LED_TYPE WS2811 //change to match your LED type
#define COLOR_ORDER BRG //change to match your LED configuration
#define NUM_LEDS 51 //change to match your setup
#define DATA_PIN2 5 //on the NodeMCU 1.0, FastLED will default to the D5 pin after throwing an error during compiling. Leave as is.
//#define LED_TYPE WS2811 //change to match your LED type
//#define COLOR_ORDER BRG //change to match your LED configuration
#define NUM_LEDS2 52 //change to match your setup
/****************************** MQTT TOPICS (change these topics as you wish) ***************************************/
#define colorstatuspub "home/livingroom/tvleds/colorstatus"
#define setcolorsub "home/livingroom/tvleds/setcolor"
#define setpowersub "home/livingroom/tvleds/setpower"
#define seteffectsub "home/livingroom/tvleds/seteffect"
#define setbrightness "home/livingroom/tvleds/setbrightness"
#define setcolorpub "home/livingroom/tvleds/setcolorpub"
#define setpowerpub "home/livingroom/tvleds/setpowerpub"
#define seteffectpub "home/livingroom/tvleds/seteffectpub"
#define setbrightnesspub "home/livingroom/tvleds/setbrightnesspub"
#define setanimationspeed "home/livingroom/tvleds/setanimationspeed"
#define colorstatuspub2 "home/livingroom/cabinetleds/colorstatus"
#define setcolorsub2 "home/livingroom/cabinetleds/setcolor"
#define setpowersub2 "home/livingroom/cabinetleds/setpower"
#define seteffectsub2 "home/livingroom/cabinetleds/seteffect"
#define setbrightness2 "home/livingroom/cabinetleds/setbrightness"
//
#define setcolorpub2 "home/livingroom/cabinetleds/setcolorpub"
#define setpowerpub2 "home/livingroom/cabinetleds/setpowerpub"
#define seteffectpub2 "home/livingroom/cabinetleds/seteffectpub"
#define setbrightnesspub2 "home/livingroom/cabinetleds/setbrightnesspub"
#define setanimationspeed2 "home/livingroom/cabinetleds/setanimationspeed"
/*************************** EFFECT CONTROL VARIABLES AND INITIALIZATIONS ************************************/
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
String setColor ="0,0,150";
String setPower;
String setEffect = "Solid";
String setBrightness = "150";
int brightness = 150;
String setAnimationSpeed;
int animationspeed = 240;
String setColorTemp;
int Rcolor = 0;
int Gcolor = 0;
int Bcolor = 0;
CRGB leds[NUM_LEDS];
String setColor2 ="0,0,150";
String setPower2;
String setEffect2 = "Solid";
String setBrightness2 = "150";
int brightness2 = 150;
String setAnimationSpeed2;
int animationspeed2 = 240;
String setColorTemp2;
int Rcolor2 = 0;
int Gcolor2 = 0;
int Bcolor2 = 0;
CRGB leds2[NUM_LEDS2];
char message_buff[100];
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// FastLED.addLeds<LED_TYPE, 5, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, DATA_PIN2, COLOR_ORDER>(leds2, NUM_LEDS2).setCorrection(TypicalLEDStrip);
FastLED.setMaxPowerInVoltsAndMilliamps(12, 10000); //experimental for power management. Feel free to try in your own setup.
FastLED.setBrightness(brightness);
FastLED.setBrightness(brightness2);
fill_solid(leds, NUM_LEDS, CRGB(255, 0, 0)); //Startup LED Lights
fill_solid(leds2, NUM_LEDS2, CRGB(255, 0, 0)); //Startup LED Lights
FastLED.show();
setup_wifi();
client.setServer(mqtt_server, 1883); //CHANGE PORT HERE IF NEEDED
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
if (String(topic) == setpowersub) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setPower = String(message_buff);
Serial.println("Set Power: " + setPower);
if (setPower == "OFF") {
client.publish(setpowerpub, "OFF");
}
if (setPower == "ON") {
client.publish(setpowerpub, "ON");
}
}
if (String(topic) == setpowersub2) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setPower2 = String(message_buff);
Serial.println("Set Power: " + setPower2);
if (setPower2 == "OFF") {
client.publish(setpowerpub2, "OFF");
}
if (setPower2 == "ON") {
client.publish(setpowerpub2, "ON");
}
}
if (String(topic) == seteffectsub) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setEffect = String(message_buff);
Serial.println("Set Effect: " + setEffect);
setPower = "ON";
client.publish(setpowerpub, "ON");
}
if (String(topic) == seteffectsub2) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setEffect2 = String(message_buff);
Serial.println("Set Effect: " + setEffect2);
setPower2 = "ON";
client.publish(setpowerpub2, "ON");
}
if (String(topic) == setbrightness) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setBrightness = String(message_buff);
Serial.println("Set Brightness: " + setBrightness);
brightness = setBrightness.toInt();
setPower = "ON";
client.publish(setpowerpub, "ON");
}
if (String(topic) == setbrightness2) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
setBrightness2 = String(message_buff);
Serial.println("Set Brightness: " + setBrightness2);
brightness2 = setBrightness2.toInt();
setPower2 = "ON";
client.publish(setpowerpub2, "ON");
}
if (String(topic) == setcolorsub) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
client.publish(setcolorpub, message_buff);
setColor = String(message_buff);
Serial.println("Set Color: " + setColor);
setPower = "ON";
client.publish(setpowerpub, "ON");
}
if (String(topic) == setcolorsub2) {
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
client.publish(setcolorpub2, message_buff);
setColor2 = String(message_buff);
Serial.println("Set Color: " + setColor2);
setPower2 = "ON";
client.publish(setpowerpub2, "ON");
}
// if (String(topic) == setanimationspeed) {
// for (i = 0; i < length; i++) {
// message_buff[i] = payload[i];
// }
// message_buff[i] = '\0';
// setAnimationSpeed = String(message_buff);
// animationspeed = setAnimationSpeed.toInt();
// }
//
// if (String(topic) == setanimationspeed2) {
// for (i = 0; i < length; i++) {
// message_buff[i] = payload[i];
// }
// message_buff[i] = '\0';
// setAnimationSpeed2 = String(message_buff);
// animationspeed2 = setAnimationSpeed2.toInt();
// }
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
int Rcolor = setColor.substring(0, setColor.indexOf(',')).toInt();
int Gcolor = setColor.substring(setColor.indexOf(',') + 1, setColor.lastIndexOf(',')).toInt();
int Bcolor = setColor.substring(setColor.lastIndexOf(',') + 1).toInt();
int Rcolor2 = setColor2.substring(0, setColor2.indexOf(',')).toInt();
int Gcolor2 = setColor2.substring(setColor2.indexOf(',') + 1, setColor2.lastIndexOf(',')).toInt();
int Bcolor2 = setColor2.substring(setColor2.lastIndexOf(',') + 1).toInt();
if (setPower == "OFF") {
setEffect = "Solid";
for ( int i = 0; i < NUM_LEDS; i++) {
leds[i].fadeToBlackBy( 8 ); //FADE OFF LEDS
}
}
if (setPower2 == "OFF") {
setEffect2 = "Solid";
for ( int i = 0; i < NUM_LEDS2; i++) {
leds2[i].fadeToBlackBy( 8 ); //FADE OFF LEDS
}
}
if (setEffect == "Solid" & setPower == "ON" ) { //Fill entire strand with solid color
fill_solid(leds, NUM_LEDS, CRGB(Rcolor, Gcolor, Bcolor));
}
// if (setEffect2 == "Solid" & setPower2 == "ON" ) { //Fill entire strand with solid color
// fill_solid(leds2, NUM_LEDS2, CRGB(Rcolor2, Gcolor2, Bcolor2));
// }
FastLED.setBrightness(brightness); //EXECUTE EFFECT COLOR
FastLED.setBrightness(brightness2); //EXECUTE EFFECT COLOR
FastLED.show();
if (animationspeed > 0 && animationspeed < 150) { //Sets animation speed based on receieved value
FastLED.delay(1000 / animationspeed);
}
// if (animationspeed2 > 0 && animationspeed2 < 150) { //Sets animation speed based on receieved value
// FastLED.delay(1000 / animationspeed2);
// }
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("LivingRoomTVLEDs1", mqtt_user, mqtt_password)) {
Serial.println("connected");
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.subscribe(setcolorsub2);
client.subscribe(setbrightness2);
// //client.subscribe(setcolortemp2);
client.subscribe(setpowersub2);
client.subscribe(seteffectsub2);
// client.subscribe(setanimationspeed2);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}