So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By laci22
#67490 Hello! :)
at first I am noob .. but i am trying ...
i need help with code .. i need to control 8ch relay with MQTT from homebridge and problem is:
1. I cant make more than one relay
2. I had find some code but not working
3. I cannot find any good tutorial for MQTT for more then one output with status
4. if i post to topic device have no response allso it dont post the status

ps: pinout doesent matter.. its only test version
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
const char* ssid = "ZTE-CJYFDJ";
const char* password = "*********";
const char* mqtt_server = "192.168.1.15";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  //pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  //Serial.begin(9600);
  pinMode(D0, OUTPUT);
  digitalWrite(D0, HIGH);
  pinMode(D2, OUTPUT);
  digitalWrite(D2, HIGH);
  pinMode(D4, OUTPUT);
  digitalWrite(D4, HIGH);
  pinMode(D5, OUTPUT);
  digitalWrite(D5, HIGH);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  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());
}
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  //1 ( pin D0 )
  if (strcmp(topic,"house/balkon/ledky/1/set") == 0) {
    if (payload[0] == '0'){
      digitalWrite(D0, HIGH);
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("house/balkon/ledky/1/state","0");
    }
    else if (payload[0] == '1')
    {
      digitalWrite(D0, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("house/balkon/ledky/1/state","1");
    }
  }


  //2 ( pin 2 )
  if (strcmp(topic,"house/balkon/ledky/2/set")==0) {
    if (payload[0] == '0'){
      digitalWrite(D2, HIGH);
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("house/balkon/ledky/2/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(D2, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("house/balkon/ledky/2/state","1");
    }
  }

  //3 ( pin 4 )
  if (strcmp(topic,"house/balkon/ledky/3/set")==0) {
    if (payload[0] == '0'){
      digitalWrite(D4, HIGH);
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("house/balkon/ledky/3/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(D4, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("house/balkon/ledky/3/state","1");
    }
  }

  //4 ( pin 5 )
  if (strcmp(topic,"house/balkon/ledky/4/set")==0) {
    if (payload[0] == '0'){
      digitalWrite(D5, HIGH);
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("house/balkon/ledky/4/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(D5, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("house/balkon/ledky/4/state","1");
    }
  }
 
  //5 ( pin 6 )
  if (strcmp(topic,"house/balkon/ledky/5/set")==0) {
    if (payload[0] == '0'){
      digitalWrite(D6, HIGH);
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("house/balkon/ledky/5/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(D6, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("house/balkon/ledky/5/state","1");
    }
  }

 
}
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
     
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  }
User avatar
By laci22
#68787 Finaly :) after weeks of crying and saying bad words :D I have made it ... it works maybe there is something wrong but it works perfectly :) maybe it will help someone :)
Code: Select all#include <PubSubClient.h>
#include <ESP8266WiFi.h>

//////////////////////////////////////////
//EDIT THESE LINES TO MATCH YOUR SETUP  //
#define MQTT_SERVER "192.168.1.15"      //
const char* ssid = "ZTE-CJYFDJ_RPT";    //
const char* password = "ar9te77bj6vf";  //
//////////////////////////////////////////

//////// get Topics //////////////////////////////////////
char* led1Topic = "house/balkon/ledky/led1";            //
char* led2Topic = "house/balkon/ledky/led2";            //
char* led3Topic = "house/balkon/ledky/led3";            //
char* led4Topic = "house/balkon/ledky/led4";            //
char* led5Topic = "house/balkon/ledky/led5";            //
/////// status Topics ////////////////////////////////////
char* led1sTopic = "house/balkon/ledky/led1/status";    //
char* led2sTopic = "house/balkon/ledky/led2/status";    //
char* led3sTopic = "house/balkon/ledky/led3/status";    //
char* led4sTopic = "house/balkon/ledky/led4/status";    //
char* led5sTopic = "house/balkon/ledky/led5/status";    //
//////////////////////////////////////////////////////////

WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

///////////// connect to wifi///////////////////////////
void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}


///////////////////// Hearth of MQTT //////////////////////////////////////////

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.print(payload[0]);

  ////// convert topic to string to make it easier to work with //////
  String topicStr = topic;
  String payloadStr = (char*)payload;
   
 /////////////// led1Topic ////////////////////
if (topicStr.equals(led1Topic))
  {
    if (payload[0] == '1')
    {
      digitalWrite(D6, LOW);
      client.publish(led1sTopic, "1");
      Serial.println("led1ON");
    }

    else if (payload[0] == '0')
    {
      digitalWrite(D6, HIGH);
      client.publish(led1sTopic, "0");
      Serial.println("led1OFF");
    }
  }
  /////////////// led2Topic //////////////////////
  if (topicStr.equals(led2Topic))
  {
    if (payload[0] == '1')
    {
      digitalWrite(D2, LOW);
      client.publish(led2sTopic, "1");
    }

    else if (payload[0] == '0')
    {
      digitalWrite(D2, HIGH);
      client.publish(led2sTopic, "0");
    }
  }
  ////////led3Topic/////////////////////////
  if (topicStr.equals(led3Topic))
  {
    if (payload[0] == '1')
    {
      digitalWrite(D3, LOW);
      client.publish(led3sTopic, "1");
    }

    else if (payload[0] == '0')
    {
      digitalWrite(D3, HIGH);
      client.publish(led3sTopic, "0");
    }
  }
  ///////led4Topic//////////
  if (topicStr.equals(led4Topic))
  {
    if (payload[0] == '1')
    {
      digitalWrite(D4, LOW);
      client.publish(led4sTopic, "1");
    }

    else if (payload[0] == '0')
    {
      digitalWrite(D4, HIGH);
      client.publish(led4sTopic, "0");
    }
  }
  /////////led5Topic/////////////////////
  if (topicStr.equals(led5Topic))
  {
    if (payload[0] == '1')
    {
      digitalWrite(D5, LOW);
      client.publish(led5sTopic, "1");
    }

    else if (payload[0] == '0')
    {
      digitalWrite(D5, HIGH);
      client.publish(led5sTopic, "0");
    }
  }
}

////////////////// prepare pins for output///////////
void setup_pins()
{
  pinMode(D6, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  digitalWrite(D6, HIGH);
  digitalWrite(D2, HIGH);
  digitalWrite(D3, HIGH);
  digitalWrite(D4, HIGH);
  digitalWrite(D5, HIGH);
  Serial.print("pinout READY");
}


////////////////////////////////////////////////
void setup()
{
  Serial.begin(115200);
  delay(100);
  setup_pins();
  setup_wifi();
  client.setCallback(callback);
  delay(2000);
}

//////////////////////////////////////////////
void loop()
{
  //reconnect if connection is lost
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();


  //MUST delay to allow ESP8266 WIFI functions to run
  delay(10);
}

////////////////////////////////////////////////////
void reconnect(){
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("wemosD1mini")) {
      client.subscribe(led1Topic);
      Serial.println("led1 ... ok");
      client.subscribe(led2Topic);
      Serial.println("led2 ... ok");
        client.loop();
       Serial.println("client loop done");
      client.subscribe(led3Topic);
      Serial.println("led3 ... ok");
      client.subscribe(led4Topic);
      Serial.println("led4 ... ok");
       client.loop();
      client.subscribe(led5Topic);
      Serial.println("led5 ... ok");
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}