ESP32 Stops sending data to AWS IoT after while

Hello,
I'm new in Arduino world I don't know what's going wrong my project Is ESP32 sends data to AWS IoT Core for every 10 mins but after while it stops sending
Currently I'm Using
Board: DOIT ESP32 DEVKIT V1
Sensor: DHT11
AWS IoT
Library: Hornbill

**I Tried **

#include <WiFi.h>
#include <DHT.h>
#include <AWS_IOT.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#define DHT_PIN 4      // pin connected to data pin of DHT11
#define DHT_TYPE DHT11 

#define WIFI_SSID "xxxxxx"
#define WIFI_PASSWD "xxxxxx"


#define CLIENT_ID "xxxxxxxxxx"                         // thing unique ID, this id should be unique among all things associated with your AWS account.
#define MQTT_TOPIC "xxxxxxxxxxx"                                //topic for the MQTT data
#define AWS_HOST "xxxxxxxxxxx" // your host for uploading data to AWS,

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

#define BUFFER_LEN 256
char msg[BUFFER_LEN];
DHT dht(DHT_PIN, DHT_TYPE);
AWS_IOT aws;

void setup()
{
    Serial.begin(9600);
    Serial.print("\nInitializing thing Temp_Humidity_DHT11_0 \n");

    Serial.print("\n  Initializing WIFI: Connecting to ");
    Serial.println(WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASSWD);
    Serial.print("  ");
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(500);
    }
    Serial.println("\n  Connected.\n  Done");

    Serial.print("\n  Initializing DHT11...");
    dht.begin();
    Serial.println("  Done.");

    Serial.println("\n  Initializing connetction to AWS....");
    if (aws.connect(AWS_HOST, CLIENT_ID) == 0)
    { // connects to host and returns 0 upon success
        Serial.println("  Connected to AWS\n  Done.");
    }
    else
    {
        Serial.println("  Connection failed!\n make sure your subscription to MQTT in the test page");
    }
    Serial.println("  Done.\n\nDone.\n");

    timeClient.begin();
    timeClient.setTimeOffset(19800);
}

void loop()
{
    timeClient.update();
    String time = timeClient.getFormattedTime();
    //Get a time structure
    unsigned long epochTime = timeClient.getEpochTime();
    struct tm *ptm = gmtime((time_t *)&epochTime);
    int monthDay = ptm->tm_mday;
    int currentMonth = ptm->tm_mon + 1;
    int currentYear = ptm->tm_year + 1900;
    String date = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
    // read temperature and humidity
    delay(500);
    float temp = dht.readTemperature();  // return temperature in °C
    float humidity = dht.readHumidity(); // return humidity in %

    int faildedCount = 0;

    // check whether reading was successful or not
    if (temp == NAN || humidity == NAN ) 
    { // NAN means no available data
      
        Serial.println("Reading failed.");
    }
    else
    {
        snprintf(msg, BUFFER_LEN, "{\"mac_id\" : \"%s\", \"temperture\" : \"%s\", \"humidity\" : \"%s\", \"time\" : \"%s\", \"date\" : \"%s\"}", WiFi.macAddress().c_str(), String(temp), String(humidity), time, date);
        Serial.println("Publishing:- ");
        Serial.println(msg);
        
        if(String(temp) != "nan" && String(humidity) != "nan"){
          if (aws.publish(MQTT_TOPIC, msg) == 0){ // publishes payload and returns 0 upon success
            Serial.println("Success\n");
            }
            else{
                faildedCount = faildedCount + 1;
                Serial.println("Failed!\n");
           }
        }
    }
    if(faildedCount >= 2){
      Serial.println("System failed 2 times restarting!\n");
      ESP.restart();
    }
    if (WiFi.status() != WL_CONNECTED)
    {
        Serial.println("connection lost restarting !\n");
        ESP.restart();
    }

         delay(600000);

}

I have found the DNS lookup on the ESP32 can fail. I tried various code changes to 'fix' it but eventually gave up and looked up the IP itself and used that.
If you are using a secure connection that may not work for you.

Sorry! I'm not able to get your point you that wifi might not work properly in ESP32

WiFi works but to connect to a host name (AWS_HOST) your ESP32 needs to look up the IP via DNS, and on my project that failed.

Thank you so much!
One more question which device works better to send data to AWS IoT Core

Not sure.
What I am doing for my commercial project is hardcoding the IP and modifying the ESP32 library DNS lookup to always just return that IP and rebooting the ESP32 regularly
see https://www.forward.com.au/pfod/ArduinoProgramming/ArduinoStrings/index.html#reboot
for the code.
You could probably reboot between each send if you needed to.
You should try and track down just what part of the connection is failing.

Could be poor power supply or intermittent router on your network, or interference from some other device.

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