Turning RGB LED Strip on & off with MQTT

Hi, I’m a newbie working on a Voice assistant to MQTT project. I’ve connected an RGB LED strip to an active low relay module. In the code below, The communication between MQTT and Arduino’s Serial is working perfectly except my function OffRelay(). Am I doing something work here?

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>

// Connect to the WiFi
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "broker.mqttdashboard.com";
const char* mqtt_user = "";
const char* mqtt_pswd = "";
WiFiClient espClient;
PubSubClient client(espClient);

//Pins are connected to relays
const int RED_PIN = 12;    //D6: Red Pin
const int GREEN_PIN = 14;  //D5: Green Pin
const int BLUE_PIN = 13; //D7: Blue Pin

unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1;
int countR;
int countG;
int countB;

bool power=false;

// The function below connects your NodeMCU to the wireless network
void setup_wifi(){

    Serial.begin(38400);
    Serial.println();
    Serial.print("connecting to ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    delay(2000);
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Payload selected [");
  Serial.print(topic);
  Serial.print("]=> ");
  for (int i = 0; i < length; i++) {
    char receivedChar = (char)payload[i];
    if(receivedChar=='0'){
      Serial.print("Rainbow On");
      power=true;
      OnRelays();
   }
   if(receivedChar=='1'){
    Serial.print("Rainbow Off");
    power=false;
    OffRelays();
    }
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("Reception"),mqtt_user,mqtt_pswd) {    // "explicit name for each client"
      Serial.print("Reception is connected to ");
      Serial.println(mqtt_user);
      // ... and subscribe to topic
      client.subscribe("RGB/Output");//Topic subscribed to
    } else {
      Serial.print("Failed to connect to MQTT...");
      Serial.println();
      Serial.println("Attempting to connect again in 3 seconds.");
      // Wait  seconds before retrying
      delay(3000);
    }
  }
}

void OnRelays(){
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
}
void OffRelays(){
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
}
void setup()
{
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  if(power==true)
   RainbowEffect();
   else{
      countR=0;
      countG=0;
      countB=0;
       SetColours();
   }
}

void RainbowEffect()
{
 currentTime = millis();
  if (currentTime - startTime >= period)
  {
    countR++;
    if (countR > 255)
    {
      countR = 0;
      countG++;
      if (countG > 255)
      {
        countG = 0;
        countB++;
        if (countB > 255)
        {
          countB = 0;
        }
      }
    }
    SetColours();
    startTime = currentTime;
  }
}

void SetColours(){
  analogWrite(RED_PIN,countR);
    analogWrite(GREEN_PIN,countG);
    analogWrite(BLUE_PIN,countB);
}

why are you using pubsub library? you can cayenne library to connect Cayenne-MQTT-Arduino/ESP8266.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub

1 Like

whats the difference between cayene and Pub/sub ?

cayenne library is simpler to use. you dont have to manually create each topic. you can use inbuilt function like CAYENNE_IN() to subscribe and CAYENNE_OUT() to publish data.

I know nothing about cayene lib. How would CAYENNE_IN() & CAYENNE_OUT() fit in my code?

have you even looked at the cayenne code? it is quite straigth forward and everything things is explained in the comments.