user37392: 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.

user4143: You receive the character 0 or 1 but compare it to the value 0 or 1. The two things are very different.
You are sending ASCII but comparing it to decimal. 
Instead you need to compare like for like.  Either:
if (payload[0] == '0') {

Or:
if (payload[0] == 48) {

You can find an easy reference for the full ASCII table here.

37392: <p>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:</p>

<pre><code>void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message received[");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i&lt;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");
  }

}
</code></pre>

<p>When sending a 1 signal, serial monitor displays:</p>

<blockquote>
  <p>Message arrived [/v1.6/devices/esp8266/relay/lv] 1 
  Setting PIN off to HIGH</p>
</blockquote>

<p>When sending a 0 signal, serial monitor displays:</p>

<blockquote>
  <p>Message arrived [/v1.6/devices/esp8266/relay/lv] 0 
  Setting PIN off to HIGH</p>
</blockquote>

<p>So it seems like it always goes into the <code>else</code> clause. Similarly, if casting the value as char:</p>

<pre><code>if ( (char *)payload[0] == 0) 
</code></pre>

<p>Then it will always go into the first condition <code>payload[0] == 0</code> regardless of the value received.</p>
 4143: <p>You receive the character <code>0</code> or <code>1</code> but compare it to the value <code>0</code> or <code>1</code>. The two things are very different.</p>

<p>You are sending ASCII but comparing it to decimal. </p>

<p>Instead you need to compare like for like.  Either:</p>

<pre><code>if (payload[0] == '0') {
</code></pre>

<p>Or:</p>

<pre><code>if (payload[0] == 48) {
</code></pre>

<p>You can find an easy reference for the full ASCII table <a href="http://asciitable.com" rel="nofollow noreferrer">here</a>.</p>
