knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.78k stars 1.46k forks source link

Client.state() changes to MQTT_CONNECTION_TIMEOUT after 30 seconds #980

Open timf34 opened 1 year ago

timf34 commented 1 year ago

Hi there,

I am subscribing to an AWS MQTT topic, however after 30 seconds of me sending data to the MQTT topic and listening to it through my ESP32, the client state changes to MQTT_CONNECTION_TIMEOUT.

Here is the code where I am setting up the connection.

void connectAWS()
{
  wifiManagerSetup();

  // Configure WiFiClientSecure to use the AWS IoT device credentials
  net.setCACert(AWS_CERT_CA);
  net.setCertificate(AWS_CERT_CRT);
  net.setPrivateKey(AWS_CERT_PRIVATE);

  // Connect to the MQTT broker on the AWS endpoint we defined earlier
  client.setServer(AWS_IOT_ENDPOINT, 8883);

  // Create a message handler
  client.setCallback(messageHandler);

  // Set buffer size (standard is 256)
  // client.setBufferSize(512);

  Serial.println("Connecting to AWS IOT");

  while (!client.connect(THINGNAME)) {
    Serial.print(".");
    delay(100);
  }

  if(!client.connected()){
    Serial.println("AWS IoT Timeout!");
    return;
  }

  // Subscribe to a topic
  client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);

  Serial.println("AWS IoT Connected!");
}

And here is messageHandler():

void messageHandler(char* topic, byte* payload, unsigned int length) {
  Serial.print("incoming: ");
  Serial.println(topic);

  StaticJsonDocument<200> doc;
  deserializeJson(doc, payload);
  float timestamp = doc["Timestamp"];
  int xReceived = doc["X"];
  float yReceived = doc["Y"];
  int possession = doc["Possession"];
  int pass = doc["Pass"];

  int received = doc["Receive"];
  int homeGoal = doc["home goal"];
  int awayGoal = doc["away goal"] ;
  int out = doc["Out"] ;

  //Serial.println(message);

  Serial.print("Timestamp: "); Serial.println(timestamp);
  Serial.print("X Coord: "); Serial.println(xReceived);
  Serial.print("Y Coord: "); Serial.println(yReceived);
  Serial.print("Possession: "); Serial.println(possession);
  Serial.print("Pass: "); Serial.println(pass);
  Serial.print("Received: "); Serial.println(received);
  Serial.print("Home Goal: "); Serial.println(homeGoal);
  Serial.print("Away Goal: "); Serial.println(awayGoal);
  Serial.print("Out: "); Serial.println(out);

  if (pass == 1 && possession == 1) vibeMode = 0;
  else if (pass == 1 && possession == 0) vibeMode = 1;
  else if (received == 1 && possession == 1) vibeMode = 2;
  else if (received == 1 && possession == 0) vibeMode = 3;
  else if (homeGoal == 1) vibeMode = 4;
  else if (awayGoal == 1) vibeMode = 5;
  else vibeMode = 6;

  lastPoss = possession;
  Serial.print("Vibe Mode: "); Serial.println(vibeMode);
  if(vibeMode != 6)coreSetup();

//  Function to calculate required speed of movement
  // based on distance between last coord and next coord  
  speedCalc(prevX, prevY , xReceived, yReceived);

  //Function to move stepper motors to next position
  Serial.print("xReceived: "); Serial.println(xReceived);
  Serial.print("yReceived: "); Serial.println(yReceived);
  // moveStepsToPos(xReceived, yReceived);

  prevX = xReceived;
  prevY = yReceived;

}

Help would really be appreciated, thanks!