Connecting my ESP8266 device to an IOT cloud service. When I push a button in the dashboard, it sends either 1 or 0. Then my ESP should turn a pin HIGH if signal received was 1 or Low if it was 0. Here is the code of the method in question:
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received[");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ( (byte)payload[0] == 0) {
digitalWrite(13, LOW);
Serial.println("Setting PIN on to LOW");
} else {
digitalWrite(13, HIGH);
Serial.println("Setting PIN off to HIGH");
}
}
When sending a 1 signal, serial monitor displays:
Message arrived [/v1.6/devices/esp8266/relay/lv] 1 Setting PIN off to HIGH
When sending a 0 signal, serial monitor displays:
Message arrived [/v1.6/devices/esp8266/relay/lv] 0 Setting PIN off to HIGH
So it seems like it always goes into the else
clause. Similarly, if casting the value as char:
if ( (char *)payload[0] == 0)
Then it will always go into the first condition payload[0] == 0
regardless of the value received.