Bruh LED strip modification

Hi All, im trying to adapt the BRUH LED Strip project ever so slightly, but Im getting really stuck with the Arduino side of things.

All I want it to do it on receipt of the MQTT topic to turn on the lights - I want it to also bring a pin to HIGH and then LOW when it is turned off. Im finding this pretty tough - can anyone help me out please?
I have written out some code, but I may be miles away from where it wants to be.

  client.publish(light_state_topic, buffer, true);
  if (client.publish(light_state_topic) true) {
    pinMode(6, OUTPUT);
    digitalWrite(pin6, true);
    Serial.println("Relay is ON");
  }

  else (client.publish light_state_topic = false) {
    digitalWrite(pin6, false);
    Serial.println("Relay is OFF");
  }
}

Can you see what I am trying to do ?

Thanks,

I would use the sonoff code on this forum (see below) and change that to suit as that does pretty much what you want, you can easily adapt it to what you want. I would modify the callback as that’s where the on/off happens…

Also the pinmode is done once in the setup function. It already turns a relay on/off so just change the pin to whatever you want and add another for your pin 6. Hope that helps.

#include <MQTT.h>
#include <PubSubClient.h>
#include <Ticker.h>

#include <ESP8266WiFi.h>



#define BUTTON          0                                     
#define RELAY           12                                   
#define LED             13                                    

#define MQTT_CLIENT     "lights"           // mqtt client_id (Must be unique )
#define MQTT_SERVER     "xxx.xxx.x.xx"                      // mqtt server
#define MQTT_PORT       1883                                 // mqtt port
#define MQTT_TOPIC      "lights_onoff"          // mqtt topic (Must be unique )
#define MQTT_USER       "HASS"                               // mqtt user
#define MQTT_PASS       "xxxxxxx"                               // mqtt password

#define WIFI_SSID       "xxxxxxx"                           // wifi ssid
#define WIFI_PASS       "xxxxxxx"                           // wifi password

#define VERSION    "\n\n------------------  v1.0  -------------------"

extern "C" { 
  #include "user_interface.h" 
}

bool sendStatus = false;
bool requestRestart = false;

int kUpdFreq = 1;
int kRetries = 10;

unsigned long TTasks;
unsigned long count = 0;

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient, MQTT_SERVER, MQTT_PORT);
Ticker btn_timer;

void callback(const MQTT::Publish& pub) {
  if (pub.payload_string() == "stat") {
  }
  else if (pub.payload_string() == "on") {
    digitalWrite(LED, LOW);
    digitalWrite(RELAY, HIGH);
  }
  else if (pub.payload_string() == "off") {
    digitalWrite(LED, HIGH);
    digitalWrite(RELAY, LOW);
  }
  else if (pub.payload_string() == "reset") {
    requestRestart = true;
  }
  sendStatus = true;
}

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(BUTTON, INPUT);

  digitalWrite(LED, HIGH);
  digitalWrite(RELAY, LOW);
  
  btn_timer.attach(0.05, button);
  
  mqttClient.set_callback(callback);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.begin(115200);
  Serial.println(VERSION);
  Serial.print("\nESP ChipID: ");
  Serial.print(ESP.getChipId(), HEX);
  Serial.print("\nConnecting to "); Serial.print(WIFI_SSID); Serial.print(" Wifi"); 
  while ((WiFi.status() != WL_CONNECTED) && kRetries --) {
    delay(500);
    Serial.print(" .");
  }
  if (WiFi.status() == WL_CONNECTED) {  
    Serial.println(" DONE");
    Serial.print("IP Address is: "); Serial.println(WiFi.localIP());
    Serial.print("Connecting to ");Serial.print(MQTT_SERVER);Serial.print(" Broker . .");
    delay(500);
    while (!mqttClient.connect(MQTT::Connect(MQTT_CLIENT).set_keepalive(90).set_auth(MQTT_USER, MQTT_PASS)) && kRetries --) {
      Serial.print(" .");
      delay(1000);
    }
    if(mqttClient.connected()) {
      Serial.println(" DONE");
      Serial.println("\n----------------------------  Logs  ----------------------------");
      Serial.println();
      mqttClient.subscribe(MQTT_TOPIC);
      blinkLED(LED, 40, 8);
      digitalWrite(LED, HIGH);
    }
    else {
      Serial.println(" FAILED!");
      Serial.println("\n----------------------------------------------------------------");
      Serial.println();
    }
  }
  else {
    Serial.println(" WiFi FAILED!");
    Serial.println("\n----------------------------------------------------------------");
    Serial.println();
  }
}

void loop() { 
  mqttClient.loop();
  timedTasks();
  checkStatus();
}

void blinkLED(int pin, int duration, int n) {             
  for(int i=0; i<n; i++)  {  
    digitalWrite(pin, HIGH);        
    delay(duration);
    digitalWrite(pin, LOW);
    delay(duration);
  }
}

void button() {
  if (!digitalRead(BUTTON)) {
    count++;
  } 
  else {
    if (count > 1 && count <= 40) {   
      digitalWrite(LED, !digitalRead(LED));
      digitalWrite(RELAY, !digitalRead(RELAY));
      sendStatus = true;
    } 
    else if (count >40){
      Serial.println("\n\nRebooting . . . . . . . . Please Wait"); 
      requestRestart = true;
    } 
    count=0;
  }
}

void checkConnection() {
  if (WiFi.status() == WL_CONNECTED)  {
    if (mqttClient.connected()) {
      Serial.println("mqtt broker connection . . . . . . . . . . OK");
    } 
    else {
      Serial.println("mqtt broker connection . . . . . . . . . . LOST");
      requestRestart = true;
    }
  }
  else { 
    Serial.println("WiFi connection . . . . . . . . . . LOST");
    requestRestart = true;
  }
}

void checkStatus() {
  if (sendStatus) {
    if(digitalRead(LED) == LOW)  {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1));
      Serial.println("Relay . . . . . . . . . . . . . . . . . . ON");
    } else {
      mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1));
      Serial.println("Relay . . . . . . . . . . . . . . . . . . OFF");
    }
    sendStatus = false;
  }
  if (requestRestart) {
    blinkLED(LED, 400, 4);
    ESP.restart();
  }
}

void timedTasks() {
  if ((millis() > TTasks + (kUpdFreq*60000)) || (millis() < TTasks)) { 
    TTasks = millis();
    checkConnection();
  }
}

Thanks for that.

I will give it a go - I will need to figure out how to merge the two codes (this one and the BRUH led).

Lets see how it goes !

I wouldn’t merge them as that will just complicate matters, just use the above to test switching on/off the relay/lights. Once that’s working figure out why Bruh’s code doesn’t work. (Probably mqtt messaging or pins wrong).