Message parsing from MQTT server 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 = &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; );
  }
}

Serial monitor receives and prints the topic correctly from MQTT server. But topic == &quot;/home/bedroom/light1&quot; 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 -&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
That's not how you compare C strings.
When you write
if (topic == &quot;/home/bedroom/light1&quot;)

you're comparing whether the character pointer topic is the same value as the address of the string &quot;/home/bedroom/light1&quot;, 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, &quot;/home/bedroom/light1&quot;) == 0 &amp;&amp; p == '0')