Skip to content

ESP32S2 - WiFiClientSecure HTTPS fails with SSL - Memory allocation failed #4523

Closed
@rvdende

Description

@rvdende

Make your question, not a Statement, inclusive. Include all pertinent information:

What you are trying to do?

I'm trying to connect an ESP32S2 dev board to a HTTPS JSON api but it fails where a ESP32 works.

System: macOS Catalina 10.15.7 with Arduino 1.8.13

What I did was go to https://github.com/espressif/arduino-esp32/tree/esp32s2 and download the branch zip.

cd tools
python get.py

Open arduino IDE > Preferences
Click on the folder path at the bottom of the panel
In my case this takes you to <user>/Library/Arduino15/
go to packages and duplicate esp32 folder as a backup

Now copy and overwrite (merge) the downloaded version on top of this:

/packages/esp32/hardware/esp32/1.0.4/cores
/packages/esp32/hardware/esp32/1.0.4/libraries
/packages/esp32/hardware/esp32/1.0.4/tools
/packages/esp32/hardware/esp32/1.0.4/variants
/packages/esp32/hardware/esp32/1.0.4/boards.txt
/packages/esp32/hardware/esp32/1.0.4/platform.txt
/packages/esp32/hardware/esp32/1.0.4/programmers.txt

Now restart Arduino IDE
Go to Tools > Board > ESP32S2 Dev Module

Show the shortest possible code that will duplicate the error.

The code I use that works on the ESP32 but not the ESP32S2:

#include <WiFiClientSecure.h>

const char* ssid     = "myssid";     // your network SSID 
const char* password = "mypass"; // your network password

const char*  server = "www.howsmyssl.com";  // Server URL

/* use 
openssl s_client -showcerts -connect www.howsmyssl.com:443 </dev/null 
to get this certificate */
const char* ca_cert = \ 
"-----BEGIN CERTIFICATE-----\n" \ 
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \ 
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \ 
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \ 
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \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sF\n" \ 
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" \ 
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \ 
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \ 
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \ 
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \ 
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \ 
"-----END CERTIFICATE-----\n";

/* create an instance of WiFiClientSecure */
WiFiClientSecure client;

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

    WiFi.begin(ssid, password);

    /* waiting for WiFi connect */
    while (WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(100);
    }

    Serial.print("Connected to ");
    Serial.println(ssid);

    /* set SSL/TLS certificate */
    client.setCACert(ca_cert);

    Serial.println("Connect to server via port 443");
    if (!client.connect(server, 443)){
        Serial.println("Connection failed!");

        char err_buf[100];
      if (client.lastError(err_buf, 100) < 0) {
          Serial.println(err_buf);
      }

    } else {
        Serial.println("Connected to server!");
        /* create HTTP request */
        client.println("GET https://www.howsmyssl.com/a/check HTTP/1.0");
        client.println("Host: www.howsmyssl.com");
        client.println("Connection: close");
        client.println();

        Serial.print("Waiting for response ");
        while (!client.available()){
            delay(50); //
            Serial.print(".");
        }  
        /* if data is available then receive and print to Terminal */
        while (client.available()) {
            char c = client.read();
            Serial.write(c);
        }

        /* if the server disconnected, stop the client */
        if (!client.connected()) {
            Serial.println();
            Serial.println("Server disconnected");
            client.stop();
        }
  }
}

void loop() {
}

The Serial monitor shows:

......................Connected to myssid
Connect to server via port 443
Connection failed!
SSL - Memory allocation failed

Everything else I've done so far with the ESP32S2 is working fine.. I2C, SPI, Neopixel RGB LED...

Has anyone had success with HTTPS on the ESP32S2 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions