Hi, I am new to rpogramming, I need to restart/reboot my ESP8266 when I publish a specific topic on my MQTT server.
Is it possible, can you help?
Thanks
Hi, I am new to rpogramming, I need to restart/reboot my ESP8266 when I publish a specific topic on my MQTT server.
Is it possible, can you help?
Thanks
Did you try entering 'esp8266 restart' into your favorite search engine and looking through the result set for ESP.restart()?
A snippet from my own code. I just send a 'U' (for update) on a specific MQTT subscribed topic to force the ESP to reboot.
void callback(char* topic, byte * data, unsigned int length)// Callback for subscribed MQTT topics
{
Serial.print(topic);
Serial.print(": ");
for (int i = 0; i < length; i++)
{
Serial.print((char)data[i]);
}
Serial.println();
if (strncmp(topic, mqttTopic, strlen(mqttTopic)) == 0) // Check it's the right topic
{
//Update
if(data[0] == 'U') // Update
{
#ifdef DEBUG
Serial.println(F("MQTT Update Request."));
#endif
WiFi.disconnect(); // Drop current connection
delay(1000);
ESP.restart();
}
}
}
if (strncmp(topic, mqttTopic, strlen(mqttTopic)) == 0)
What do you think, what happens when the mqttTopic length is longer than the topic length?
Try to compare the length of the topic and the mqttTopic first and compare with strncmp when the length of the two string are not equals.
authgabor:
if (strncmp(topic, mqttTopic, strlen(mqttTopic)) == 0)
What do you think, what happens when the mqttTopic length is longer than the topic length?
When mqttTopic is longer than topic then they won't compare as the compare will fail on the zero on the end of the char array and also whatever is stored in memory behind it. As I'm using compare that does not alter the memory being compared and the MCU is not going to generate an error because I accessed protected memory it works for me.
Okay, but it is anti-pattern.