espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.42k stars 7.37k forks source link

start_ssl_client(): Free internal heap before TLS 138228 #8966

Closed StaelensJarne closed 9 months ago

StaelensJarne commented 10 months ago

Board

SparkFun ESP32 Thing

Device Description

I use a usb cable to flash the Platformio project to my IoT sensor. There is nothing else attached.

Hardware Configuration

This is an IoT sensor. The product page is here.

And the original code from the project is here. But the original code has not been modified since April 2022. Additional hardware info: ESP32 240MHz, 320KB RAM, 4MB Flash.

Version

v2.0.14

IDE Name

PlatformIO

Operating System

Windows 11

Flash frequency

240MHz

PSRAM enabled

no

Upload speed

11520

Description

These IoT sensor can send values with MQTT and REST. The MQTT works fine, but the REST not, due to the ssl_client not working.

Sketch

The setch is available here: https://github.com/controlco2/operame/blob/main/operame.ino

But I think this is an possible bug in the WiFiClientSecure library.
The only code in the sketch that could generate this error is:
if (wificlientsecure.connected() || wificlientsecure.connect(&rest_domain[0], rest_port)) {
    post_rest_message(message, wificlientsecure);
}

Debug Message

These messages are repeated every 5 seconds:

[240216][V][ssl_client.cpp:62] start_ssl_client(): Free internal heap before TLS 138228
[240216][E][WiFiClientSecure.cpp:144] connect(): start_ssl_client: -1
[240219][V][ssl_client.cpp:321] stop_ssl_socket(): Cleaning SSL connection.

And what does the value next to the error message mean?
In my case 138228. Where can I find what 138228 means?

Other Steps to Reproduce

Download the code from the original repo. And frash the project to the sensor. Then configure REST via the WiFi portal which is at 192.168.4,1

I have checked existing issues, online documentation and the Troubleshooting Guide

lucasssvaz commented 10 months ago

The number 138228 is the amount of free heap in bytes. You can check it here https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFiClientSecure/src/ssl_client.cpp#L61.

Is it possible for you to provide a simplified sketch that causes this behavior ? Also please double check you are using the informed version (v2.0.14) as we had breaking changes in version 3.0.0 (master branch)

lbernstone commented 10 months ago

Since 1.0.6, insecure connections are denied by default. You need to call WFCS::setInsecure() or provide appropriate certificates.

StaelensJarne commented 10 months ago

This simplified sketch is:

#include <WiFiClientSecure.h>

WiFiClientSecure  wificlientsecure;

/* This is a toggle on the configuration oprtal 
The default value is false, so this if should not be executed */
if (rest_cert_enabled) wificlientsecure.setCACert(rest_cert.c_str());

while(wificlientsecure.available()){
    String line = wificlientsecure.readStringUntil('\r');
    Serial.print(line);
}

if (wificlientsecure.connected() || wificlientsecure.connect(&rest_domain[0], rest_port)) {
    post_rest_message(message, wificlientsecure);
}

And the full code is on this github page.

But I am not sure if this is a bug, or the sensor that does not have enough memory available.

And here is my platformio.ini

[platformio]
src_dir = operame
data_dir = operame

[env:testREST]
platform = espressif32
board = esp32thing
framework = arduino
lib_deps = 
    juerd/ESP-WiFiSettings@3.8.0
    wifwaf/MH-Z19@1.5.4
    bodmer/TFT_eSPI@2.5.31
    256dpi/MQTT@2.5.1
    bblanchon/ArduinoJson@6.21.3
    adafruit/DHT sensor library@1.4.4
targets=upload
upload_port = COM5
build_flags =
  -DCORE_DEBUG_LEVEL=5
  -DUSER_SETUP_LOADED=1
  -DST7789_DRIVER=1
  -DCGRAM_OFFSET=1
  -DTFT_WIDTH=135
  -DTFT_HEIGHT=240
  -DTFT_MOSI=19
  -DTFT_SCLK=18
  -DTFT_CS=5
  -DTFT_DC=16
  -DTFT_RST=-23
  -DTFT_BL=4
  -DTFT_BACKLIGHT_ON=HIGH
  -DLOAD_GLCD=1
  -DLOAD_FONT2=1
  -DLOAD_FONT4=1
  -DLOAD_FONT6=1
  -DLOAD_FONT7=1
  -DLOAD_FONT8=1
  -DLOAD_GFXFF=1
  -DSPI_FREQUENCY=40000000
StaelensJarne commented 10 months ago

How can I know wificlientsecure is connected sucessfully started?

Is this: wificlientsecure.connected() that evaluates to true?

And can I force a sucessfull start with wificlient.setInsecure().

lbernstone commented 10 months ago

https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFiClientSecure/examples/WiFiClientInsecure/WiFiClientInsecure.ino You can't force anything. You are having a conversation with the server on the other end of the connection. If the conversation goes poorly, it fails.

StaelensJarne commented 9 months ago

Ok, with WiFiClientSecure.setInsecure() worked.

But I substituted the WiFiClientSecure with a normal WiFiClient since my REST API support also http URL's, instead of https.

StaelensJarne commented 9 months ago

This issue can now be closed.