mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 90 forks source link

[Bug] Present omitted if initial mqtt connection fails #425

Closed cimba007 closed 6 years ago

cimba007 commented 6 years ago

If I upload a new sketch the mqtt-client does not logoff from the mqtt-server (buildin mycontroller). On next sketch run presentation is skipped:

MC[I]: Booting ESP8266 device...<\n>
MC[I]: WIFI not connected, trying connection...<\n>
MC[D]: Searching BSSID for ssid:[FRITZ!Box Fon WLAN 7390]<\n>
MC[D]: Found BSSID[] for ssid:[FRITZ!Box Fon WLAN 7390], RSSI:[-62 -dBm, 76 %]<\n>
MC[D]: Selected bssid:[]<\n>
MC[I]: Connecting to WiFi with ssid[FRITZ!Box Fon WLAN 7390] and with password.....OK<\n>
MC[I]: WiFi BSSID:[], RSSI:[-68 dBm, 64 %], IP:[], StatusCode:[3]<\n>
MC[D]: WiFi connection status: Connected<\n>
MC[I]: Configuration(NodeEUI:[], Mqtt{mDNS-status:[0], Server:[], Port:[], FeedId:[esp], User:[]})<\n>
MC[D]: Attempting MQTT connection...<\n>
MC[I]: MQTT settings(Broker:[], Port:[])<\n>
MC[I]: MQTT authenticating as user:[]<\n>
MC[I]: MQTT connection failed, rc=-4<\n>
MC[D]: Try again in 3 seconds<\n>
MC[I]: Initialization done...<\n>
MC[I]: MQTT failed! Retryting to connect...<\n>
MC[D]: WiFi connection status: Connected<\n>
MC[D]: Attempting MQTT connection...<\n>
MC[I]: MQTT settings(Broker:[], Port:[])<\n>
MC[I]: MQTT authenticating as user:[]<\n>
MC[I]: MQTT connected :)<\n>
MC[I]: MQTT topic subscribed:[in_esp/ESP_C0DE98/#]<\n>
jkandasa commented 6 years ago

@cimba007 Could you please elaborate this issue?

cimba007 commented 6 years ago

I will try .. assume I upload my sketch to the esp8266 and everything is working as expected.

Now I want to upload a new sketch .. the upload process resets the esp8266 and leaves an open mqtt-connection on the broker (the client just vanished and did no proper logoff). Now the new sketch is uploaded .. and tries to connect again to the broker.

MC[D]: Attempting MQTT connection...<\n>
MC[I]: MQTT settings(Broker:[], Port:[])<\n>
MC[I]: MQTT authenticating as user:[]<\n>
MC[I]: MQTT connection failed, rc=-4<\n>
MC[D]: Try again in 3 seconds<\n>

The connection fails .. but succeeds after 3 seconds on the next retry.

https://github.com/mycontroller-org/MyControllerDevice/blob/95ea3ea77a9cdb33c846c3f435ec57cea829f426/MyController.cpp#L121

The code presented here will be skipped ...

  if(mqttClient.connected()){
    before();
    sendRSSI();
    sendStatistics();
    mcPresentation();
  }

When the yellow part is called

image

The only check and chance to run

  if(mqttClient.connected()){
    before();
    sendRSSI();
    sendStatistics();
    mcPresentation();
  }

is gone.

I hope this made it a little bit more clear.

cimba007 commented 6 years ago

I am not good with github and pull requests but I suggest to change this:

   mqttClient.connect(WiFi.hostname().c_str(), _mqttUser, _mqttPwd)

to

      int limit = 0;
      while(!mqttClient.connect(WiFi.hostname().c_str(), _mqttUser, _mqttPwd) && limit++ < 5)
      {
          MC_SERIAL.printf("MC[I]: MQTT retry .. %d\n",limit);
          mcDelay(500);
      }

in void MyController::checkMqtt() {

This will make sure that even if the innitical connection during initialize-method fails one times there are a few more retrys.

jkandasa commented 6 years ago

@cimba007 Yes, thank you for the detailed information. I understand that I need fix this issue on https://github.com/mycontroller-org/MyControllerDevice

moving to https://github.com/mycontroller-org/MyControllerDevice/issues/15