-->
Page 1 of 1

ESPAsyncTCP + SSL

PostPosted: Fri Jan 22, 2021 2:44 am
by Jana Vitkova
Hello everyone,
I come again for advice. I'm trying to implement asynchronous HTTPS POST request into ESP8266, but I am unsuccessfull.
I use the ESPAsyncTCP library with encryption enabled.
After calling the connect function, the axtls layer returns error 40 which means SSL_ALERT_HANDSHAKE_FAILURE:
Code: Select alltcp_ssl_new: 0
_recv[1]: 7
tcp_ssl_read: 0
tcp_ssl_read: -40
tcp_ssl_read: read error: -40
_recv[1] err: -40
tcp_ssl_free: 0
Disconnected 0

relevant code is here:
Code: Select allstatic const char *REQUEST = "GET /a/check HTTP/1.1\r\nHost: www.howsmyssl.com\r\nUser-Agent: ESP8266\r\nConnection: close\r\n\r\n";

static void replyToServer(void* arg) {
   AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);

   // send reply
   if (client->space() > strlen(REQUEST) && client->canSend()) {
      client->add(REQUEST, strlen(REQUEST), 0);
      client->send();
   }
   else{
      Serial.print("OUT of space\r\n");
   }
}

/* event callbacks */
static void handleData(void* arg, AsyncClient* client, void *data, size_t len) {
   Serial.print("Data received from ");
   Serial.print(client->remoteIP().toString());
   Serial.print(": ");
   Serial.write((uint8_t*)data, len);
   Serial.println();
}

void onConnect(void* arg, AsyncClient* client) {
   IPAddress adr = client->getRemoteAddress();
   Serial.print("Client has been connected to ");
   Serial.println(adr.toString());
   replyToServer(client);
}

void onError(void * arg, AsyncClient * client, err_t error){
   Serial.print("Connect Error ");
   Serial.println(error);
   Serial.println(client->errorToString(error));
   client->close(true);
}

void onTimeout(void * arg, AsyncClient * client, uint32_t time){
   Serial.print("TimeOut ");
   Serial.println(time);
   client->close(true);
   delete client;
}

void onDisconnect(void * arg, AsyncClient * client){
   Serial.print("Disconnected ");
   Serial.println(client->space(), 10);
   client->close(true);
   delete client;
}

void SendStats(uint8_t ad){

   AsyncClient* cl = new AsyncClient;
   if(!cl)//could not allocate client
       return;
      cl->onTimeout(&onTimeout, cl);
      cl->onData(&handleData, cl);
      cl->onConnect(&onConnect, cl);
      cl->onError(&onError, cl);
      cl->onDisconnect(&onDisconnect, cl);
      cl->connect("howsmyssl.com", 443, true);
}

...
void loop(){
...
// WiFi is connected so every few minutes send request
SendStats(0);
...
}

Any ideas or what libraries (functions) do you use?
I can't use the synchronous HTTPClient, because in the event of an internet failure (WiFi is still available), the program will freeze for too many seconds.
Many thanks