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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...