ESP32: HTTPS + Bluetooth - lack of RAM

Hello everyone and thank you for your help.

I have a problem. I have a ready working project: esp32 sends HTTP requests to the server and communicates via Bluetooth with the phone. I decided to switch from HTTP to HTTPS protocol and faced with a lack of memory even in an empty project (new), where I use only Bluetooth and HTTPS.

For example, here is the code with commented bluetooth initialization (The response from the server was received as expected):

#include <BluetoothSerial.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <WiFi.h>

char* sendPostRequest(const char* url, const char* prefix, const char* payload);
void initHTTPS(const char* url);
void initWebSocket(const char* url);

BluetoothSerial bs;
WiFiClientSecure clientHTTPS;
HTTPClient http;

String ssid = "TP-ssid";
String password = "password";

String msg = "{\"chipId\":\"34545642323523564\"}";
String url = "https://blink-off.space:8443/init-token";

void setup() { 
    Serial.begin(115200);

    WiFi.begin(ssid.c_str(), password.c_str());
    for(int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
        Serial.print(".");
        delay(1000);
    }

    //bs.begin("blName");

    initHTTPS(url.c_str());

    //bs.begin("blName");
}

void loop() {
    Serial.printf("Free heap: %u bytes\n", ESP.getFreeHeap());

    String rq = sendPostRequest(url.c_str(), "init", msg.c_str());
    Serial.println(rq);

    delay(5000);
}

void initHTTPS(const char* url) {
    clientHTTPS.setInsecure(); // Disabling certificate verification
    //clientHTTPS.setCACert(echo_org_ssl_ca_cert); 

    if (!http.begin(clientHTTPS, url)) {
        // If connection initialization failed
        //return nullptr;
    }
    http.addHeader("Content-Type", "application/json");
}

char* sendPostRequest(const char* url, const char* prefix, const char* payload) {
    //String payloadString = String(payload);
    String encryptedPayload = String(payload);

    // Forming a JSON message
    char message[256];
    snprintf(message, sizeof(message), "{\"%s\" : \"%s\"}", prefix, encryptedPayload.c_str());

    int httpResponseCode = http.POST(message);

    if (httpResponseCode > 0) {
        String response = http.getString();
        http.end();

        // We allocate memory for the response and copy it there
        char* responseCStr = (char*)malloc(response.length() + 1);
        if (responseCStr) {
            strcpy(responseCStr, response.c_str());
        }
        return responseCStr;
    } else {
        http.end();
        return nullptr;
    }
}

Now let's uncomment the line bs.begin("blName"); And we see a cyclic error, every 5 seconds when trying to request:

[  1823][E][ssl_client.cpp:37] _handle_error(): [start_ssl_client():273]: (-17040) RSA - The public key operation failed : BIGNUM - Memory allocation failed

[  1837][E][WiFiClientSecure.cpp:144] connect(): start_ssl_client: -17040

I repeat, when commenting on the bluetooth initialization line, everything works fine, but in my main project I need both bluetooth and HTTPS at the same time. HTTP (no hhtps) + Bluetooth is working correctly.

it's unfortunately not new, see

The problem is not the firmware of the board: I have enough flash memory. The problem is that I don't have enough RAM during the execution of the program. The presented post recommends using lightweight BLE libraries, but I have classic Bluetooth. Again, the decision is about something else.

sorry I misunderstood your point.

No problems

Where are you freeing the string you allocated with malloc()?

-jim lee

You might like to try with NimBLE library.
Resolved similar situation few months ago where stock BLE library didn't invite a way out...

Thank you, yes, you're right, but it's not critical in the context of the problem.

I've only tried classic BLE, I'll need to try this library. If anyone else has encountered this problem, you can also try GitHub - jjoe64/esp32_ble_light: Stripped version of the ESP32 BLE library, to only support server logic and reduce size . Thank you

Your problem is running out of RAM. So a memory leak is not critical?

Ok

-jimlee

The lack of RAM only occurs when initializing Bluetooth.
Anyway, thank you very much for your attention.

Can you call these before and after of your .begin() ?
I mean , call and printf the result.

             heap_caps_get_total_size(MALLOC_CAP_DEFAULT), 
             heap_caps_get_free_size(MALLOC_CAP_DEFAULT), 
             heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT),

with these arguments:

MALLOC_CAP_INTERNAL,  MALLOC_CAP_DEFAULT, MALLOC_CAP_SPIRAM

So we should get 9 numbers before and 9 numbers after

Aw. Can't find the library you use. BluetoothSerial library, which is available through Arduino IDE seems to do not have BluetoothSerial::begin(const char *) method. What library do you use?

most likely, you haven't connected esp32 to the manager's board

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.