Firstly, excuse my poor English, this is not my native langage.
I'm trying to use an Arduino Uno to drive an 8 relay board from an MQTT Subscription. I ask for help because i'm some kind of beginner in arduino, i'm just a Linux addict.
The idea is to get a payload from PubSubclient and use it to manage relays. For example, the payload '71' ask the relay 7 to turn on and '42' make the relay 4 to turn off.
All works fine until I wanted to write a function with two parameters : 'relay_number' and 'Action'
All parameters are received but the relay never goes up or down.
My code :
void callback(char* topic, byte* payload, unsigned int length) {
Relay_Nb = (char)payload[0];
Action = char(payload[1]);
Serial.println(Relay_Nb);
Serial.println(Action);
RelayControl(Relay_Nb, Action);
}
void RelayControl(char Relay_Nb, char Action) {
Serial.println(Relay_Nb); // for debugging purpose
Serial.println(Action);
if ( Action == '0') {
Serial.print("Bringing down Relay ");
Serial.println(Relay_Nb);
digitalWrite(Relay_Nb, LOW);
} else if (Action == '1') {
Serial.print("Bringing up Relay ");
Serial.println(Relay_Nb);
digitalWrite(Relay_Nb, HIGH);
} else if (Action == '2') {
// Relay temporisé (ouverture portail par ex)
Serial.println("Bringing up, and down Relay ");
digitalWrite(Relay_Nb, HIGH);
delay(750);
digitalWrite(Relay_Nb, LOW);
}
}
I'm sure an old maker have the solution i'm searching for hours now.
Please help and don't hesitate to show me others ways. Thanks.