ExploreEmbedded / Hornbill-Examples

89 stars 171 forks source link

Cannot connect to AWS in the loop. #18

Open WizKid32 opened 5 years ago

WizKid32 commented 5 years ago

Hi,

I am currently involved with a project in which we use an ESP32 device to service a mesh of BLE devices, and I have discovered a bug I don't know how to fix. The bug in question involves the central node device. This device starts a BLE Client and services the mesh. Once collected in JSON format, the data is then to be sent over to AWS.

This is where my issue starts. I cannot connect the MQTT bridge to AWS in the loop. When I try I get the following message: E (56089) aws_iot: failed! mbedtls_ssl_handshake returned -0x4d80 E (56093) AWS_IOT: Error(-4) connecting to a3twuhzs2qbepq-ats.iot.us-east.2.amazonaws.com:8883, Trying to reconnect E (57385) aws_iot: failed! mbedtls_ssl_handshake returned -0x10 E (57388) AWS_IOT: Error(-4) connecting to a3twuhzs2qbepq-ats.iot.us-east-2.amazonaws.com:8883, Trying to reconnect

Please note that the BLE service has been switched off after the mesh has been serviced. This is required else the ESP32 core crashes. Also, note that I have had to repartition the ESP32 device due to the size of the program. I am currently using the Huge_App.csv partition scheme.

Also, note that I did copy and run the same code in the setup to send a test package and all goes well. I also tested the repartition settings by using all available partition schemes — no change in result.

Here is the code I used to test with:

void setup() {
  Serial.begin(115200);

  // if(!connectWiFi()) reconnectWiFi();
  // connectAWS(AWS_HOST_GATEWAY_ENDPOINT, POLICY_ID, aws_root_ca_pem, certificate_pem_crt, private_pem_key);
  // delay(1000);
  // subscribeToTopic();
  // delay(1000);
  // strcpy(msgAWS, "VSC says hello.");
  // publishToTopic();
  // delay(1000);
  // disconnectAWS(); 

  createBLEClient();
  scanBLEDevices();
}

void loop() {
  connectToBLEServer();
    if(flagSendBLEData) {
        flagSendBLEData = false;
        if(!connectWiFi()) reconnectWiFi();
        Serial.print("Gateway name: ");
        Serial.println(AWS_HOST_GATEWAY_ENDPOINT);
        Serial.print("Policy name: ");
        Serial.println(POLICY_ID);
        Serial.print("Root: ");
        Serial.println(aws_root_ca_pem);
        Serial.print("CA: ");
        Serial.println(certificate_pem_crt);
        Serial.print("Key");
        Serial.println(private_pem_key);
        connectAWS(AWS_HOST_GATEWAY_ENDPOINT, POLICY_ID, aws_root_ca_pem, certificate_pem_crt, private_pem_key);
        delay(1000);
        subscribeToTopic();
        delay(1000);
        strcpy(msgAWS, "VSC says hello.");
        publishToTopic();
        delay(1000);
        disconnectAWS(); 
    }
}

Please let me know if anyone manages to fix this bug. Thanks.