It's ok when no last will.
but failed connect when added last will.
WiFi.status:3
IP Address: 192.168.0.107
subscribe MQTT ok
set MQTT will
Connecting to MQTT...
after connect to MQTT
failed to connect to MQTT
Connection failed
void MQTT_connect() {
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.println("Connecting to MQTT... ");
int8_t ret = mqtt.connect();
delay(3000);
Serial.println("after connect to MQTT");
uint8_t retries = 3;
while (ret != 0) { // connect will return 0 for connected
Serial.println("failed to connect to MQTT");
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
It's ok when no last will. but failed connect when added last will.
WiFi.status:3 IP Address: 192.168.0.107 subscribe MQTT ok set MQTT will Connecting to MQTT... after connect to MQTT failed to connect to MQTT Connection failed
void MQTT_connect() { // Stop if already connected. if (mqtt.connected()) { return; }
Serial.println("set MQTT will"); mqtt.will(gTopicSend, "offline socEsp01");
Serial.println("Connecting to MQTT... "); int8_t ret = mqtt.connect(); delay(3000); Serial.println("after connect to MQTT"); uint8_t retries = 3; while (ret != 0) { // connect will return 0 for connected Serial.println("failed to connect to MQTT"); Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me
}
Serial.println("MQTT Connected!");