adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
573 stars 291 forks source link

connect failed when added lastWill for esp8266 #108

Open myie9 opened 6 years ago

myie9 commented 6 years ago

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

  resetFunc();      
  delay(10000);
}
else
{
  //mqtt.will(gTopicStatus, "offline socEsp01", 1);
  ret = mqtt.connect();
}

}

Serial.println("MQTT Connected!");