Chat freely about anything...

User avatar
By m.maazi
#90473 Hi.
I have this esp8266-based project:
On the module I have a RTC module to track time, an LED, and a buttons to control LED.
And a Website running php and mySQL with to buttons to control LED.
Every 3 seconds, module sends the timestamp of every configuration to the server. Server compares received timestamp with the one in database. If received timestamp is older than database, sends new timestamp and configuration to module otherwise updates the database.
Every time, module connects to server and at the end disconnects. This sync process works fine with an unencrypted connection. But when I use SSL, It takes much longer time specially on stablishing connection.
I heard about Websockets. Is it possible to use Websockets to keep my connection alive and just send/ receive data? Is there an example?
Or is there a better way to sync with an encrypted connection?
Actually my project is far complicated than this and I optimized it by splitting sync process into multiple functions managed by TaskScheduler library.
I trimmed it down to this:

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
WiFiClientSecure CLIENT;

Task TASK_ESP_WIFI(3000, TASK_FOREVER, &ESP_WIFI_ANALYSE);
Task TASK_ESP_SYNC(1500, TASK_FOREVER, &ESP_SYNC_CONNECT);

bool LED_UPLOAD = false;
DynamicJsonDocument ESP_SYNC_REQUEST_JSONDOC(3000);
DynamicJsonDocument ESP_SYNC_RESPONSE_JSONDOC(3000);

////////////////////////////////////////////////////////////////
// This executes on setup()
void ESP_INIT(void) {

  WiFi.persistent(true);
  WiFi.mode(WIFI_STA);
  WiFi.setAutoReconnect(true);
  WiFi.begin(CONFIG.SSID, CONFIG.PASSWORD);

  CLIENT.setTimeout(200);
  CLIENT.setInsecure();
}

////////////////////////////////////////////////////////////////
// This executes every 3 seconds
void ESP_WIFI_ANALYSE(void) {

  if (WiFi.status() == WL_CONNECTED) {
    TASK_ESP_SYNC.enableIfNot();
  } else {
    TASK_ESP_SYNC.disable();
  }
}

////////////////////////////////////////////////////////////////

void ESP_SYNC_CONNECT(void) {

  if (CLIENT.connect(ESP_SYNC_HOST, 443)) {
    TASK_ESP_SYNC.setCallback(&ESP_SYNC_BUILD);
    TASK_ESP_SYNC.setInterval(500);
  } else {
    CLIENT.flush();
    CLIENT.stop();
    TASK_ESP_SYNC.setCallback(&ESP_SYNC_CONNECT);
    TASK_ESP_SYNC.setInterval(1000);
  }
}

////////////////////////////////////////////////////////////////

void ESP_SYNC_BUILD(void) {

  JsonArray tmp_LED_ARRAY = ESP_SYNC_REQUEST_JSONDOC.createNestedArray("LED");
  tmp_LED_ARRAY.add((String)SETTING.LED_TIMESTAMP);
  if (LED_UPLOAD) {
    tmp_LED_ARRAY.add((String)SETTING.LED_MODE);
  }
  #endif
  TASK_ESP_SYNC.setCallback(&ESP_SYNC_SEND);
  TASK_ESP_SYNC.setInterval(500);
}

////////////////////////////////////////////////////////////////

void ESP_SYNC_SEND(void) {

  String tmp_ESP_SYNC_REQUEST_BODY;
  serializeJson(ESP_SYNC_REQUEST_JSONDOC, tmp_ESP_SYNC_REQUEST_BODY);
  CLIENT.println("POST " + (String)ESP_SYNC_URL + " HTTP/1.1");
  CLIENT.println("Host: " + (String)ESP_SYNC_HOST);
  CLIENT.println("Content-Type: application/json");
  CLIENT.println("Content-Length: " + (String)tmp_ESP_SYNC_REQUEST_BODY.length());
  CLIENT.println();
  CLIENT.print(tmp_ESP_SYNC_REQUEST_BODY);
  if (CLIENT.println() != 0) {
    TASK_ESP_SYNC.setCallback(&ESP_SYNC_RECEIVE);
    TASK_ESP_SYNC.setInterval(500);
  } else {
    CLIENT.flush();
    CLIENT.stop();
    TASK_ESP_SYNC.setCallback(&ESP_SYNC_CONNECT);
    TASK_ESP_SYNC.setInterval(1000);
  }
}

////////////////////////////////////////////////////////////////

void ESP_SYNC_RECEIVE(void) {

  while (CLIENT.available()) tmp_PHP_RESULT = CLIENT.readStringUntil('\n');
  if (tmp_PHP_RESULT.length() > 0) {
    DeserializationError tmp_JSON_ERROR;
    tmp_JSON_ERROR = deserializeJson(ESP_SYNC_RESPONSE_JSONDOC, tmp_PHP_RESULT);
    if (!tmp_JSON_ERROR) {
      TASK_ESP_SYNC.setCallback(&ESP_SYNC_DECODE);
      TASK_ESP_SYNC.setInterval(500);
    } else {
      TASK_ESP_SYNC.setCallback(&ESP_SYNC_CONNECT);
      TASK_ESP_SYNC.setInterval(1000);
    }
  } else {
    TASK_ESP_SYNC.setCallback(&ESP_SYNC_CONNECT);
    TASK_ESP_SYNC.setInterval(1000);
  }
  CLIENT.flush();
  CLIENT.stop();
}

////////////////////////////////////////////////////////////////

void ESP_SYNC_DECODE(void) {

  if (ESP_SYNC_RESPONSE_JSONDOC.containsKey("LED")) {
    if (SETTING.LED_TIMESTAMP < (int)ESP_SYNC_RESPONSE_JSONDOC["LED"][0]) {
      SETTING.LED_TIMESTAMP = (int)ESP_SYNC_RESPONSE_JSONDOC["LED"][0];
      SETTING.LED_STATUS_MODE = (int)ESP_SYNC_RESPONSE_JSONDOC["LED"][1];
      LED_UPLOAD = false;
    } else if (SETTING.LED_TIMESTAMP > (int)ESP_SYNC_RESPONSE_JSONDOC["LED"][0]) {
      LED_UPLOAD = true;
    } else {
      LED_UPLOAD = false;
    }
  }
  TASK_ESP_SYNC.setCallback(&ESP_SYNC_CONNECT);
  TASK_ESP_SYNC.setInterval(1000);
}


My English sucks. Sorry.
Thanks for your help.