BUG? Sending data to server

Azaelyus
Posts: 13
Joined: Tue Oct 24, 2017 7:38 pm

BUG? Sending data to server

Postby Azaelyus » Tue Oct 24, 2017 7:58 pm

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.
Image
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:

Code: Select all

#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/u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p4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+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);
    
    did_once = true;
  }
  getValues();
  turnOnWiFi();
  sendValues();
  Serial.println(millis());
  esp_deep_sleep(uS_to_S * atoi(seconds_to_sleep));
}

void loop()
{
  
}

Who is online

Users browsing this forum: dalmat and 122 guests