MQTT with ESP32 - Keeping Session Alive

I am using the EMQX MQTT Broker and publishing some five values every 30 seconds to the broker . Using a ESP32 module + BM280 sensor for getting the climate related variables. The broker desktop client has a keep alive setting of 60 seconds.

What I find is that, all is fine till I have the PC running the desktop client . But when i close it in evening and start the next day the ESP32 module is no longer publishing. I am not sure how long it tries before being kicked out by the broker. I tried to restart the connection based on the result of publishing as in the code below. Not sure it helps ...

[code]
void loop() {
  client.loop();
  long valueToPost ;
  if ( millis() - sub_intervalMs > sub_interval) {
    sub_intervalMs = millis();
    digitalWrite( blueLED, HIGH );
    readBME280();
    bool pubStatus  = client.publish(topic, postValue);
    if (!pubStatus) {
      String client_id = "esp32-client-mogaraghu";
      digitalWrite( blueLED, LOW );
      Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
      if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
        Serial.println("EMQX MQTT broker Reconnected");
      }
    }
  }
}

[/code]

Is it that the EMQX server refuses ( after some time ) to accept published values if there are no clients accessing the data ? If this is not the case then the issue could be with my ESP32 setup... will a periodic reconnect at regular intervals help ?

thanks

OK for now problem solved with the ESP.restart() function. If there is any better solution would like to hear !

void loop() {
  client.loop();

  long valueToPost = millis();

  if ( millis() - sub_intervalMs > sub_interval) {
    sub_intervalMs = millis();
    digitalWrite( blueLED, HIGH );
    readBME280();
    bool pubStatus  = client.publish(topic, postValue);
    if (!pubStatus) {
      Serial.println ( "Publish failed ! Restarting ");
      digitalWrite( blueLED, LOW );
      ESP.restart();
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.