user72337: I'm making a NodeMCU based home automation. I adapted code I found on internet for controlling a device (say LED) via MQTT broker hosted on a Raspberry Pi. It works fine. But when I try to scale it up for more than one device, I'm hitting a wall.
void callback(char* topic, byte* payload, unsigned int length)
{
  
  const char* t_bedroom_light1 = "/home/bedroom/light1";
  
  Serial.print("Topic from MQTT broker is : ");
  Serial.print(topic);
  char p = payload[0];

  // if MQTT comes a 0 turn OFF pin D4
  if (topic == "/home/bedroom/light1" && p == '0')
  {
    digitalWrite(LIGHT_PIN, LOW);
    Serial.println(" Turn Light Off! ");
  }

  // if MQTT comes a 1, turn ON pin D4
  if (topic == "/home/bedroom/light1" && p == '1')
  {
    digitalWrite(LIGHT_PIN, HIGH);
    Serial.println(" Turn Light On! " );
  }
}

Serial monitor receives and prints the topic correctly from MQTT server. But topic == "/home/bedroom/light1" never returns true. If I delete that part then p =='0' or p=='1' works fine by itself. I've tried comparing the topic with the locally declared char array, tried converting it into String and doing strcmp. Nothing. What am I doing wrong? Here is the Serial monitor output
12:43:20.346 -> .......
12:43:24.187 -> WiFi connected
12:43:24.187 -> IP address: 
12:43:24.187 -> 192.168.1.5
12:43:24.187 -> Attempting MQTT connection...connected
12:43:30.752 -> Topic from MQTT broker is : /home/bedroom/light1


user46852: That's not how you compare C strings.
When you write
if (topic == "/home/bedroom/light1")

you're comparing whether the character pointer topic is the same value as the address of the string "/home/bedroom/light1", which of course it is not in this case.
To compare C strings, use the strcmp() function, which returns 0 if they're identical.
if (strcmp(topic, "/home/bedroom/light1") == 0 && p == '0')


72337: <p>I'm making a NodeMCU based home automation. I adapted code I found on internet for controlling a device (say LED) via MQTT broker hosted on a Raspberry Pi. It works fine. But when I try to scale it up for more than one device, I'm hitting a wall.</p>
<pre><code>void callback(char* topic, byte* payload, unsigned int length)
{
  
  const char* t_bedroom_light1 = &quot;/home/bedroom/light1&quot;;
  
  Serial.print(&quot;Topic from MQTT broker is : &quot;);
  Serial.print(topic);
  char p = payload[0];

  // if MQTT comes a 0 turn OFF pin D4
  if (topic == &quot;/home/bedroom/light1&quot; &amp;&amp; p == '0')
  {
    digitalWrite(LIGHT_PIN, LOW);
    Serial.println(&quot; Turn Light Off! &quot;);
  }

  // if MQTT comes a 1, turn ON pin D4
  if (topic == &quot;/home/bedroom/light1&quot; &amp;&amp; p == '1')
  {
    digitalWrite(LIGHT_PIN, HIGH);
    Serial.println(&quot; Turn Light On! &quot; );
  }
}
</code></pre>
<p>Serial monitor receives and prints the topic correctly from MQTT server. But <code>topic == &quot;/home/bedroom/light1&quot;</code> never returns true. If I delete that part then <code>p =='0'</code> or <code>p=='1'</code> works fine by itself. I've tried comparing the topic with the locally declared char array, tried converting it into String and doing strcmp. Nothing. What am I doing wrong? Here is the Serial monitor output</p>
<pre><code>12:43:20.346 -&gt; .......
12:43:24.187 -&gt; WiFi connected
12:43:24.187 -&gt; IP address: 
12:43:24.187 -&gt; 192.168.1.5
12:43:24.187 -&gt; Attempting MQTT connection...connected
12:43:30.752 -&gt; Topic from MQTT broker is : /home/bedroom/light1
</code></pre>
 46852: <p>That's not how you compare C strings.</p>
<p>When you write</p>
<pre><code>if (topic == &quot;/home/bedroom/light1&quot;)
</code></pre>
<p>you're comparing whether the character pointer <code>topic</code> is the same value as the address of the string <code>&quot;/home/bedroom/light1&quot;</code>, which of course it is not in this case.</p>
<p>To compare C strings, use the <code>strcmp()</code> function, which returns 0 if they're identical.</p>
<pre><code>if (strcmp(topic, &quot;/home/bedroom/light1&quot;) == 0 &amp;&amp; p == '0')
</code></pre>
