-3

I'm playing with Arduino and I want to compare the string coming from a MQTT message.

This is my code to catch the incoming messages:

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  int i=0;
  for (i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

Serial.println((char)payload[0]);

  if(topic==topic_conmutador){
    if(strcmp((char*)(payload[0]), "0")==0)
    digitalWrite(built_in_PIN, LOW);
  }

}

The line if(strcmp((char*)(payload[0]), "0")==0){ is giving me this error message:

error: invalid conversion from 'byte {aka unsigned char}' to 'const char*' [-fpermissive]

What am I doing wrong? I just want to check if the first character of the incoming message is a 0.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
user204415
  • 307
  • 1
  • 4
  • 20

1 Answers1

0

You probably just want:

if (payload[0] == '0')

This is comparing the first character of the string payload to a character '0'.

Note: "0" is string type with one character and '0' is char type.

gre_gor
  • 6,669
  • 9
  • 47
  • 52