🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Unable to request HTTPS endpoint with LAN Module 13.2

    Arduino
    2
    3
    431
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      msauv
      last edited by

      General informations :

      • M5Stack Core 2
      • LAN Module 13.2
      • Using PlatformIO with platform=platformio/espressif32@^6.9.0 & board=m5stack-core2
      • M5Stack powered by USB-C

      Problem :
      I need to make requests to a secure HTTPS server. They work perfectly on WiFi with the “WiFiClientSecure” client and the “WiFiClient” + “SSLClientESP32” client, but not with “EthernetClient” of “M5Module_LAN” or “Ethernet” + “SSLClientESP32”.

      I've tried many modules and libraries without getting HTTPS to work, HTTP works in ethernet but is completely useless and outdated. Do you know a way to solve this problem? It shouldn't be so complicated to connect to a site using TLS in 2024.

      Here are some sample codes I've tried :

      #include <WiFi.h>
      #include <WiFiClientSecure.h>
      #include <HTTPClientGeneric.h>
      #include <SPI.h>
      
      char ssid[] = "ssid";
      char pass[] = "password";
      
      WiFiClientSecure wifi;
      
      void setup()
      {
       Serial.begin(115200);
       WiFi.begin(ssid, pass);
       uint16_t timeout = 0;
       while (WiFi.status() != WL_CONNECTED)
       {
         if (timeout > 600)
         {
           Serial.println(" Timeout reached, aborting");
         }
         delay(100);
         Serial.print(".");
         timeout++;
       }
       Serial.println(" Connection established!");
       Serial.println("Network::connectWifi(): IP address   : " + WiFi.localIP().toString());
       Serial.println("Network::connectWifi(): Subnet mask  : " + WiFi.subnetMask().toString());
       Serial.println("Network::connectWifi(): Gateway IP   : " + WiFi.gatewayIP().toString());
       Serial.println("Network::connectWifi(): DNS          : " + WiFi.dnsIP().toString());
       Serial.println("Network::connectWifi(): SSID         : " + WiFi.SSID());
       Serial.println("Network::connectWifi(): Signal       : " + String(WiFi.RSSI()) + " dBm");
       wifi.setInsecure(); // Just trying to connect, will use certificate later
      }
      
      void loop()
      {
       HTTPClientGeneric https;
       https.begin(wifi, " [URL of my HTTPS endpoint] ");
       int httpCode = https.GET();
       Serial.print("GET Status code: ");
       Serial.println(httpCode);
       String response = https.getString();
       Serial.println("Response:");
       Serial.println(response);
       delay(5000);
      }
      

      This one works perfectly, the local IP and the response are displayed in the serial port.

      #include <WiFi.h>
      #include <WiFiClient.h>
      #include <HTTPClientGeneric.h>
      #include <SSLClientESP32.h>
      #include <SPI.h>
      
      char ssid[] = "ssid";
      char pass[] = "password";
      
      const char *isrg_root_ca = "-----BEGIN CERTIFICATE-----\n"
                                 "...\n"
                                 "-----END CERTIFICATE-----\n";
      
      WiFiClient wifi;
      SSLClientESP32 ssl_client(&wifi);
      
      void setup()
      {
        Serial.begin(115200);
        WiFi.begin(ssid, pass);
        uint16_t timeout = 0;
        while (WiFi.status() != WL_CONNECTED)
        {
          if (timeout > 600)
          {
            Serial.println(" Timeout reached, aborting");
          }
          delay(100);
          Serial.print(".");
          timeout++;
        }
        Serial.println(" Connection established!");
        Serial.println("Network::connectWifi(): IP address   : " + WiFi.localIP().toString());
        Serial.println("Network::connectWifi(): Subnet mask  : " + WiFi.subnetMask().toString());
        Serial.println("Network::connectWifi(): Gateway IP   : " + WiFi.gatewayIP().toString());
        Serial.println("Network::connectWifi(): DNS          : " + WiFi.dnsIP().toString());
        Serial.println("Network::connectWifi(): SSID         : " + WiFi.SSID());
        Serial.println("Network::connectWifi(): Signal       : " + String(WiFi.RSSI()) + " dBm");
        ssl_client.setCACert(isrg_root_ca);
        ssl_client.setInsecure(); // Same
      }
      
      void loop()
      {
        HTTPClientGeneric https;
        https.begin(ssl_client, " [URL of my HTTPS endpoint] ");
        int httpCode = https.GET();
        Serial.print("GET Status code: ");
        Serial.println(httpCode);
        String response = https.getString();
        Serial.println("Response:");
        Serial.println(response);
        delay(5000);
      }
      

      This one works perfectly, the local IP and the response are displayed in the serial port.

      #include <M5Unified.h>
      #include <M5Module_LAN.h>
      #include <WifiClient.h>
      
      #include <HTTPClientGeneric.h>
      #include <SSLClientESP32.h>
      #include <SPI.h>
      
      byte mac[] = (mac);
      Client *client;
      EthernetClient ethClient;
      SSLClientESP32 ssl_client(&ethClient);
      
      void setup()
      {
        M5.begin();
        Serial.begin(115200);
        M5Module_LAN LAN;
      
        uint8_t cs_pin;
        uint8_t rst_pin;
        uint8_t int_pin;
      
        m5::board_t board = M5.getBoard();
        switch (board)
        {
        case m5::board_t::board_M5Stack:
        {
          cs_pin = 5;
          rst_pin = 0;
          int_pin = 35;
        }
        break;
        case m5::board_t::board_M5StackCore2:
        {
          cs_pin = 33;
          rst_pin = 0;
          int_pin = 35;
        }
        break;
        case m5::board_t::board_M5StackCoreS3:
        {
          cs_pin = 1;
          rst_pin = 0;
          int_pin = 10;
        }
        break;
        }
      
        SPI.begin(SCK, MISO, MOSI, -1);
      
        LAN.setResetPin(rst_pin);
        LAN.reset();
        LAN.init(cs_pin);
      
        while (LAN.begin(mac) != 1)
        {
          Serial.println("Error getting IP address via DHCP, trying again...");
          delay(2000);
        }
        Serial.println(" Connection established!");
        Serial.println("Network::connectWifi(): IP address   : " + LAN.localIP().toString());
        Serial.println("Network::connectWifi(): Subnet mask  : " + LAN.subnetMask().toString());
        Serial.println("Network::connectWifi(): Gateway IP   : " + LAN.gatewayIP().toString());
        Serial.println("Network::connectWifi(): DNS          : " + LAN.dnsServerIP().toString());
        ssl_client.setCACert(isrg_root_ca);
      }
      
      void loop()
      {
        HTTPClientGeneric https;
        https.begin(ssl_client, " [URL of my HTTPS endpoint] ");
        int httpCode = https.GET();
        Serial.print("GET Status code: ");
        Serial.println(httpCode);
        String response = https.getString();
        Serial.println("Response:");
        Serial.println(response);
        delay(5000);
      }
      

      It does not work with the following error:

      [ 20591][E][ssl_lib_client.cpp:35] _handle_error(): [start_ssl_client():300]: (-1) ERROR - Generic error
      [ 20601][E][SSLClientESP32.cpp:123] connect(): start_ssl_client: -1
      [ 21775][E][ssl_lib_client.cpp:35] _handle_error(): [start_ssl_client():300]: (-1) ERROR - Generic error
      [ 21784][E][SSLClientESP32.cpp:123] connect(): start_ssl_client: -1
      

      Note that the IP is displayed correctly. It works with the EthernetClient but the request is HTTP only, so not what I want.

      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF
        felmue @msauv
        last edited by

        Hello @msauv

        have you looked at this example from the ESP_SSLClient library? Works for me.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        M 1 Reply Last reply Reply Quote 0
        • M
          msauv @felmue
          last edited by

          @felmue Thanks it works perfectly with the "ESP_SSLClient" module instead of "SSLClientESP32" !

          1 Reply Last reply Reply Quote 0
          • First post
            Last post