4 relay control

Hi,

As it was expected, I’ve encountered a new problem. This time is with the Arduino IDE sketch code in my NodeMCU. I want to control 4 relays. However, it doesn’t matter the switch I activate in Openhab2, it always activates the first relay.

It seems that the problem is with the payload. Here’s a part of my code.

Definition of gpios:

//GPIOS

const int piscinaFILTRACION = 5;
const int piscinaLUCES = 4;
const int piscinaCHORRO = 0;
const int piscinaJACUZZI = 2;

Topics:

//topic to subscribe 

char* filtracionTopic = "/dva/piscina/filtracion";
char* lucesTopic = "/dva/piscina/luces";
char* chorroTopic = "/dva/piscina/chorro";
char* jacuzziTopic = "/dva/piscina/jacuzzi";

//topic to publish to confirm that the light has been turned on for the python script to log

char* filtracionConfirmTopic = "/dva/piscina/filtracionconfirm";
char* lucesConfirmTopic = "/dva/piscina/lucesconfirm";
char* chorroConfirmTopic = "/dva/piscina/choroconfirm";
char* jacuzziConfirmTopic = "/dva/piscina/jacuzziconfirm";
//MQTT callback

void callback(char* topic, byte* payload, unsigned int length) {

 char p0 = (char)payload[0];
  char p1 = (char)payload[1];
  char p2 = (char)payload[2];
  char p3 = (char)payload[3];
  Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }
  //convert topic to string to make it easier to work with

  String topicStr = topic; 

  //Print out some debugging info

  Serial.println("Callback update.");

  Serial.print("Topic: ");

  Serial.println(topicStr);


  //turn the light on if the payload is '1' and publish to the confirmation topic so the python script can log it

    if(p0 == '1'){

    digitalWrite(piscinaFILTRACION, HIGH);

    client.publish(filtracionConfirmTopic, "On");

  }
    else if (p0 == '0'){

    digitalWrite(piscinaFILTRACION, LOW);

    client.publish(filtracionConfirmTopic, "Off");

  }

    if(p1 == '1'){

    digitalWrite(piscinaLUCES, HIGH);

    client.publish(lucesConfirmTopic, "On");

  }
    else if (p1 == '0'){

    digitalWrite(piscinaLUCES, LOW);

    client.publish(lucesConfirmTopic, "Off");

  }
    if(p2 == '1'){

    digitalWrite(piscinaCHORRO, HIGH);

    client.publish(chorroConfirmTopic, "On");

  }
    else if (p2 == '0'){

    digitalWrite(piscinaCHORRO, LOW);

    client.publish(chorroConfirmTopic, "Off");

  }

  
    if(p3 == '1'){

    digitalWrite(piscinaJACUZZI, HIGH);

    client.publish(jacuzziConfirmTopic, "On");

  }
    else if (p3 == '0'){

    digitalWrite(piscinaJACUZZI, LOW);

    client.publish(jacuzziConfirmTopic, "Off");

  }
  

  }

Could you please check the code?
Thanks

1 Like

This really isn’t an Arduino forum. While you might find some help here, you are going to reach far more people who can help posting to an Arduino forum.

1 Like