BUG? Sending data to server

Hello guys, im having a really big trouble right now. Im trying to solve this problem for really long time, but im not able to find answer anywere.

So, heres my problem: Im trying to send my meassured temperature every 10 seconds to my server through HTTPS. Im using deep sleep every 10 seconds so it takes like 12 seconds before wifi starts and data are sent. Everything goes well - i connect to the wifi, i even send the data.. but then.. somehow? some of the data does not come to the server.. i really dont know what is going on, because its running TCP (is not it?). Just look at here and u will see - http://www.homesp.cz
U can see that sometimes theres much bigger delay than 12 (13) seconds maximaly, so one or more measurements are missing. Im adding a picture for u to understand it.

It happens in totally random intervals. Sometime it goes well for 3 minutes and even more, but sometimes it drops 3 times in a row.
Im using ESP32 devKitC, which i bought from official reseller. Powering the chip through the USB cable. If someone can help me or tell me about some work-around i would be very thankful.

Heres the code:

#include <DallasTemperature.h>
#include <WiFiClientSecure.h>

#define uS_to_S 1000000LL
#define temperaturePin 32

const char* root_ca =
"-----BEGIN CERTIFICATE-----\n" \
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
"Z8h/pZq4UmE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GS5jN2p8M+X+Q7UNKEkROb3N6\n" \
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
"-----END CERTIFICATE-----\n";

WiFiClientSecure client;

String data;
int connection_retries = 0;

RTC_DATA_ATTR boolean did_once = false;
RTC_DATA_ATTR char ssid[33] = "xxxx", password[33] = "xxxx", seconds_to_sleep[5] = "10";

OneWire ds(temperaturePin);
DallasTemperature DS18B20(&ds);

void turnOnWiFi()
{
  int wifi_connect_initial_time = millis();

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    if (millis() - wifi_connect_initial_time > 1500)
    {
      connection_retries++;
      WiFi.disconnect();
      if(connection_retries == 10)
      {
        Serial.println("Failed connecting to wifi. Going to sleep.");
        esp_deep_sleep_start();
      }
      turnOnWiFi();
    }
  }
}

void sendValues()
{
  client.setCACert(root_ca);

  if (client.connect("homesp.cz", 443))
  {
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: homesp.cz");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Content-Length: " + String(data.length()));
    client.println();
    client.println(data);
  }
}

float getTemp()
{
  float temp;
  do
  {
    DS18B20.requestTemperaturesByIndex(0);
    temp = DS18B20.getTempCByIndex(0);
  } while (temp == 85.0 || temp == (-127.0));
  return temp;
}

void getValues()
{
  data = "temp=" + String(getTemp(), 3);
}

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

  if(did_once == false)
  {
    DS18B20.begin();
    DS18B20.setResolution(12); 

    Serial.setTimeout(5000);
  }
  getValues();
  turnOnWiFi();
  sendValues();
  Serial.println(millis());
  esp_deep_sleep(uS_to_S * atoi(seconds_to_sleep));
}

void loop()
{
  
}

Don't know much about your hardware, but...

void turnOnWiFi()
{
...
      turnOnWiFi();
...
}

Surely that would be better written without the recursion.

void turnOnWiFi()
{
...
      if(connection_retries == 10)
      {
        Serial.println("Failed connecting to wifi. Going to sleep.");
...
}

So you try to connect to the WiFi 10 times...

void sendValues()
{
  client.setCACert(root_ca);

  if (client.connect("homesp.cz", 443))
  {
    client.println("POST /add.php HTTP/1.1");
    client.println("Host: homesp.cz");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Content-Length: " + String(data.length()));
    client.println();
    client.println(data);
  }
}

But you only try to establish the connection to the server once. Try printing some debug to see if the connect is failing.

Also, consider explicitly flushing or closing the connection after the send to exclude any possibility that you end up in sleep mode before the data has been completely sent.

  if(did_once == false)

Where do you set did_once to true?

I can probably see where is the problem. The module does not want to wake up from deep sleep mode..

look at these pictures:


The module completly missed waking up in hh:58:ss, only woke in 55 56 57 and 59.. As u can see im printing ALIVE to the serial line after the wake, but the one in :58: is missing :frowning:

Any ideas?

So we've gone from every 12 seconds to once a minute. I assume you are printing the ALIVE right after the Serial.begin().
The API docs recommend explicitly disconnecting from the peripherals like Wi-Fi before calling esp_deep_sleep(), so I would suggest you do that too, even though it is probably unrelated.

Maybe comment out all the Wi-Fi stuff and add some more serial debug. Then just test the reliability of the wakeup by itself. If ok, gradually re-add the Wi-Fi and test.

Found the problem!!!!

As u can see here: homesp.cz, almost 3 hours with no problem :slight_smile: The module was closing the connection so fast after HTTP request was sent. I added this line of code

while(!client.available()) delay(1);

right after sending HTTP request and everything goes well! Just need to wait for server response, then i can send my module to sleep.
I dont even need to disconnect from wifi or stopping the client. Average time while the module is wake is 1800ms which is really great.

Thanks so much for your time tho!

Good stuff.