If statements that wont work, why?

Hey guys,
I’m in the process of setting up some IoT controlled lights, using a raspberry pi as a mqtt broker and some esp8266 esp-01 to0 control my leds.

I’ve got them publishing and subscribing to each other no problems there.
My problem is something that is probably quite easy, but I cant figure out why it doesn’t work.
My code compiles and uploads no problems, but it doesn’t work!!

The problem area of my code is:

void callback(char* topic, byte* payload, unsigned int length) {
  char message_buff[100];
  int i = 0;
  message_buff[i] = '\0';
  Serial.println("");
  Serial.println("Buffer Cleared");
  
    Serial.println("--");
  Serial.print("Incoming Topic: "); Serial.println(topic);
  Serial.print("Incoming Message: ");
  for (i = 0; i < length; i++) {
   message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';
  Serial.println(message_buff);
  String msgString = String(message_buff);
  Serial.println("--");
  Serial.print("Decoded Message "); Serial.println(msgString);
  Serial.println("--");
  Serial.println("");
  
  if (topic == "Living_Room_Lights_StatusIn") 
    {
      onoff = String(message_buff).toInt();
    } 
  if (topic == "Living_Room_Lights_PatternIn") 
    {
      pattern = String(message_buff);
    }
  if (topic == "Living_Room_Lights_ColorIn")
    {
      color = String(message_buff);
    }
  if (topic == "Living_Room_Lights_BrightnessIn")
    {
      brightness = String(message_buff).toInt();
    }
 Serial.print("Status "); Serial.println(onoff);
 Serial.print("Pattern "); Serial.println(pattern); 
 Serial.print("Color "); Serial.println(color);
 Serial.print("Brightness "); Serial.println(brightness);
 Serial.println("--");

}

This function is called whenever one of the subscriptions is updates.
The subscription comes through ok, its the actual setting of the variables in the ‘if’ statements.
They dont get set at all.

See the screen cap of the serial monitor, you can see the incoming data and below that the variables, which are unset.

Your ifs work perfectly. Have you considered using strcmp?

if (topic == "Living_Room_Lights_StatusIn")

topic is a pointer to char. The constant string on the other side is a pointer to char. Even if they are both pointing to strings that contain the same letters, they're not pointing to the same place in memory so they're not ever going to be equal.

Use strcmp to compare two strings.

He's comparing a char array to a String....

DrAzzy: He's comparing a char array to a String....

No, he's comparing a char array to a string. Then he uses the String class in the body of those if statements in a most wasteful way.

OP, Google "C++ atoi" and drop the String class.

Thanks for the quick replies guys, you've helped me out. I've only been coding for about 6 months now, and can usually find answers to problems that I come across pretty easy, but this one had me stumped.

In my first iteration of my code I had put the constant string into a char variable but it didnt work, so then I tried to compare the variable 'topic' to the string, but still didn't work.

Before your posts I hadn't heard of strcmp, as its not referenced in any arduino resources that i have found.

I will google that, but as I said I'm still learning, so any new sources will be good for me.

FrankBrownster: Before your posts I hadn't heard of strcmp, as its not referenced in any arduino resources that i have found.

Use the Arduino resources for Arduino specific library stuff, like digitalWrite or digitalRead or stuff like that. For all the C++ stuff use a good C++ reference. The Arduino reference has just the very basics of C++ in it. There is a rich rich world of stuff that isn't covered there.