[Solved] ESP8266 MQTT Keepalive timeout

Hello
Newbie to Losant, need help
Using the function below I am sending data by Losant functions from ESP8266. If sending one variable - seems to work OK. When sending 3 - mostly OK, 5+ fails all the time.
Connection succeeds
Auth succeeds
Error: Disconnected from Losant - Keepalive timeout

Second question: How can I estimate the size of the needed buffer for the json string?

Please help
Thanks
Uri

void LosantUpdateData(double PID_Setpoint, float* MeasuredTemp, double* Fan_setpoint) 
{
    INFO("LosantUpdateData:: Started.\n");
    bool toReconnect = false;

  if(WiFi.status() != WL_CONNECTED) {
    ERR("LosantUpdateData:: Disconnected from WiFi");
    toReconnect = true;
  }

  if(!device.connected()) {
    ERR("LosantUpdateData:: Disconnected from MQTT. Err = %d", device.mqttClient.state());
    toReconnect = true;
  }

  if(toReconnect) {
    //connect();
    //Losant_setup();
    device.connectSecure(wifiClient, Losant_access_key, Losant_access_secret);
    while(!device.connected()) 
    {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to Losant!");
  }
  device.loop();
  
  StaticJsonBuffer<400> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  INFO("LosantUpdateData:: Starting data build.\n");
  INFO("LosantUpdateData:: PID_Setpoint = %s.\n", printDouble(PID_Setpoint,2));
  root["PID_Setpoint"] = PID_Setpoint;
  root["MeasuredTemp_0"] = MeasuredTemp[0];
  root["MeasuredTemp_1"] = MeasuredTemp[1];
  root["MeasuredTemp_2"] = MeasuredTemp[2];
  root["MeasuredTemp_3"] = MeasuredTemp[3];
  //root["MeasuredTemp_4"] = MeasuredTemp[4];
  //root["MeasuredTemp_5"] = MeasuredTemp[5];
  //root["Fan_setpoint_0"] = Fan_setpoint[0];
  //root["Fan_setpoint_1"] = Fan_setpoint[1];
 // root["Fan_setpoint_2"] = Fan_setpoint[2];
  INFO("LosantUpdateData:: Finished data build.\n");
  device.sendState(root);
  INFO("LosantUpdateData:: END.\n");
}

Hi @Uri_Bear,

Check out this forum post:

You can increase the size of the mqtt packet. But, you have to do it within the library. The library doesn’t yet expose a configurable size yet.

1 Like

That did it! many thanks!

I changed #define MQTT_MAX_PACKET_SIZE 128 from 128 to 256 on 3/26/2018 1:15 MDT. But I still get keepalive timeouts when sending battery volts and battery current after about 36 hours or so. I also get a lot of “establishing new connection…” messages. Is this normal?

I recently added RSSI, WiFi signal strength in the ESP8266’s report and it still seems to timeout after a couple of days just like before,

05/08/2018 06:44:13 489 0 Device establishing new connection, closing previous connection
05/08/2018 06:26:33
05/08/2018 06:26:33 416 0 Device establishing new connection, closing previous connection
05/08/2018 06:11:33
05/08/2018 06:11:33 1860 0 Device establishing new connection, closing previous connection
05/08/2018 05:04:58
05/08/2018 05:04:58 773 0 Device establishing new connection, closing previous connection
05/08/2018 04:37:11.