Description
Board
ESP32 Devkit V1 DOIT
Device Description
There is connected HC-SR04 ultrasonic sensor that have no impact on problem.
Hardware Configuration
D22 --> Trigger
D23 --> Echo
3V3 --> Vcc
GND --> GND
Version
v2.0.2
IDE Name
Arduino IDE 1.8.19
Operating System
Windows 10 Education x64
Flash frequency
80 MHz
PSRAM enabled
no
Upload speed
921600
Description
I have developed code in January for ESP32 that is using HTTPS connection via WiFiClientSecure library.
It is doing measurements using HC-SR04 or JSN-SR04T ultrasonic sensor of water level height. Datas are sent to webserver, that is freehosting hosted at 000webhost --> https://hladinomer.000webhostapp.com/
I am not sure what version of Arduino Core I was using at development, maybe it was 2.0.2, but not sure...
Sketch is using FreeRTOS, there are other versions of sketch without FreeRTOS, but same problem. In FreeRTOS sketch there is used Queue. When datas are measured (once per 5 minutes), they are pushed to Queue, and HTTPS connection task will read them and will execute once to store them in webserver MySQL database.
In similar time as I developed Arduino Core, I have created identical sketch (from point of how it works) in ESP-IDF framework 4.4 on mbed_tls example and it is working without problem for months, so it doesn't have that issue... Right now I am using Arduino Core 2.0.2 - latest stable release. And code that was originally working in January is not working now. During SSL handshake there is ssl_client return -1, so certificate is not verified. Root CA certificate is same (DigiCert Global Root CA), also website certificate is valid for more than +120 days to go. So, nothing changed on that side for Secure client... Source code is correct.
Core 2.0.2 output with Core Debug level Verbose - Connection not working:
I have tried WiFiClientSecure example and it is working good. When i compared both websites, there is difference at TLS version that is used on webservers. 000webhost is on subdomains using TLS 1.3 and 1.2 and howsmyssl.com (from WiFiClientSecure example) is using TLS 1.2.
So i think there might be problem with TLS 1.3 connection. Also in WiFiClientSecure of actual 2.0.2 release branch there is comment: "Running on TLS 1.2 using mbedTLS". Also I am not sure if 000webhost had TLS 1.3 before or it was added in recent days or months, I don't know how to find that out... Is it possible to force 1.2 TLS connection from side of ESP32?
How should I know if connection is made by TLS 1.3 or TLS 1.2? Debug giving me no output related to it
I tried to downgrade Arduino Core to 1.0.6. Then I tried same sketch and connection to 000webhost is working normally and datas are successfully sent to server where are stored in database. So I am a bit confused... I have tried to study documentation and also some Gitter threads, but nothing relevant I have found... So what should be wrong?
There is Wokwi simulator with lightweight sketch with one task that is trying to sent example datas to webservers without ultrasonic measurement - no luck on connection: https://wokwi.com/projects/327948646817464914
Core 1.0.6 output with Core Debug level Verbose - Connection WORKING! :
Sketch
/*|-----------------------------------------------------------------------------------|*/
/*|Projekt: Hladinomer - HTTPS - FreeRTOS - HC-SR04 / JSN-SR04T / HY-SRF05 |*/
/*|ESP32 (DevKit, Generic) |*/
/*|Autor: Martin Chlebovec (martinius96) |*/
/*|E-mail: [email protected] |*/
/*|Info k projektu (schéma): https://martinius96.github.io/hladinomer-studna-scripty/ |*/
/*|Testovacie webove rozhranie: https://hladinomer.000webhostapp.com/ |*/
/*|Licencia pouzitia: MIT |*/
/*|Revízia: 3. Január 2022 |*/
/*|-----------------------------------------------------------------------------------|*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <NewPingESP8266.h>
const char * ssid = "MY_WIFI"; //MENO WiFi SIETE
const char * password = "MY_WIFI_PASSWORD"; //HESLO WiFi SIETE
const char* host = "hladinomer.000webhostapp.com"; //adresa webservera (doména) na ktorú sa odosielajú dáta
String url = "/data.php"; //URL adresa - cesta pod domenou k cieľovemu .php súboru, ktorý realizuje zápis
#define pinTrigger 22
#define pinEcho 23
#define maxVzdialenost 450
NewPingESP8266 sonar(pinTrigger, pinEcho, maxVzdialenost);
TaskHandle_t Task1; //ULTRASONIC MEASUREMENT
TaskHandle_t Task2; //WIFI HTTP SOCKET
QueueHandle_t q = NULL;
WiFiClientSecure client;
static void Task1code( void * parameter);
static void Task2code( void * parameter);
//Root CA cert --> CERTIFIKÁT CERTIFIKAČNEJ AUTORITY, KTORÁ VYDALA CERTIFIKÁT VÁŠ WEBSERVER v .pem formáte
//DST ROOT CA X3 EXAMPLE (https://i.imgur.com/fvw4huT.png)
const static char* test_root_ca PROGMEM = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDrzCCApegAwI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XjcKChzUyImZOMkXDiqw8cvpOp/2PV5Adg\n" \
"06O/nVsJ8dWO41P0jmP6P6fbtGbfYmbW0W5BjfIttep3Sp+dWOIrWcBAI+0tKIJF\n" \
"PnlUkiaY4IBIqDfv8NZ5YBberOgOzW6sRBc4L0na4UU+Krk2U886UAb3LujEV0ls\n" \
"YSEY1QSteDwsOoBrp+uvFRTp2InBuThs4pFsiv9kuXclVzDAGySj4dzp30d8tbQk\n" \
"CAUw7C29C79Fv1C5qfPrmAESrciIxpg0X40KPMbp1ZWVbd4=\n" \
"-----END CERTIFICATE-----\n";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(F(""));
Serial.println(F("Wifi connected with IP:"));
Serial.println(WiFi.localIP());
client.setCACert(test_root_ca);
q = xQueueCreate(20, sizeof(int));
if (q != NULL) {
Serial.println(F("Queue FIFO buffer is created"));
vTaskDelay(1000 / portTICK_PERIOD_MS); //wait for a second
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
Serial.println(F("Ultrasonic measurement task started"));
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
Serial.println(F("HTTP Socket task started"));
} else {
Serial.println(F("Queue creation failed"));
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password); //pripoj sa na wifi siet s heslom
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
yield();
}
static void Task1code( void * parameter) {
if (q == NULL) {
Serial.println(F("Queue in Measurement task is not ready"));
return;
}
while (1) {
int distance = sonar.ping_cm();
delay(50);
Serial.print(F("Test measurement: "));
Serial.print(distance);
Serial.println(F(" cm"));
if (distance > 0) {
distance = 0;
for (int i = 0; i < 10; i++) {
distance += sonar.ping_cm();
delay(50);
}
distance = distance / 10;
Serial.print(F("Distance to water level is: "));
Serial.print(distance);
Serial.println(F(" cm."));
xQueueSend(q, (void *)&distance, (TickType_t )0); //add the measurement value to Queue
for (int countdown = 300; countdown >= 0; countdown--) {
Serial.print(F("Next measurement in: "));
Serial.print(countdown);
Serial.println(F(" seconds"));
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
}
}
static void Task2code( void * parameter) {
int distance;
if (q == NULL) {
Serial.println(F("Queue in HTTP socket task is not ready"));
return;
}
while (1) {
xQueueReceive(q, &distance, portMAX_DELAY); //read measurement value from Queue and run code below, if no value, WAIT....
String data = "hodnota=" + String(distance) + "&token=123456789";
client.stop();
if (client.connect(host, 443)) {
Serial.println(F("Connected to server successfully"));
client.println("POST " + url + " HTTP/1.0");
client.println("Host: " + (String)host);
client.println(F("User-Agent: ESP"));
client.println(F("Connection: close"));
client.println(F("Content-Type: application/x-www-form-urlencoded;"));
client.print(F("Content-Length: "));
client.println(data.length());
client.println();
client.println(data);
Serial.println(F("Datas were sent to server successfully"));
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
} else {
Serial.println(F("Connection to webserver was NOT successful"));
}
client.stop();
}
}
Debug Message
Copy from UART monitor is not working, I don't know why, I can just copy [ character...
I don't have ExceptionDecoder.
Other Steps to Reproduce
I tried Wokwi simulator, output is same.
Link provided in Description. After downgrading Arduino Core to 1.0.6, connection is working without problem.
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.