ESP32: ethernet w5500 with plain (HTTP) and SSL (HTTPS)
ESP32: ethernet w5500 with plain (HTTP) and SSL (HTTPS)

ESP32: ethernet w5500 with plain (HTTP) and SSL (HTTPS)
First of all, it must be said that the ESP32 already has a MAC, a TCP / IP stack, and an Arduino network library, but we will still use it with an Ethernet library with W5500 devices since they are the most popular of all and work very well.
Many people ask me for some examples of wired connections; in my mind, the first thing I thought of is the w5500 device, one of the most famous and powerful. We’ll start to learn how to manage plain and SSL requests.
Devices
The W5500 chip is a Hardwired TCP/IP embedded Ethernet controller that provides an easier Internet connection to embedded systems. W5500 enables users to have Internet connectivity in their applications just by using the single chip in which TCP/IP stack, 10/100 Ethernet MAC, and PHY are embedded.
WIZnet‘s Hardwired TCP/IP is the market-proven technology supporting TCP, UDP, IPv4, ICMP, ARP, IGMP, and PPPoE protocols. W5500 embeds the 32Kbyte internal memory buffer for the Ethernet packet processing. If you use W5500, you can implement the Ethernet application by adding the simple socket program. It’s a faster and easier way rather than using any other Embedded Ethernet solution. Users can use eight independent hardware sockets simultaneously.
SPI (Serial Peripheral Interface) is provided for easy integration with the external MCU. The W5500’s SPI supports 80 MHz speed. To reduce the system’s power consumption, W5500 provides WOL (Wake on LAN) and power-down mode.
Features
- Supports Hardwired TCP/IP Protocols : TCP, UDP, ICMP, IPv4, ARP, IGMP, PPPoE
- Supports 8 independent sockets simultaneously
- Supports Power down mode
- Supports Wake on LAN over UDP
- Supports High Speed Serial Peripheral Interface(SPI MODE 0, 3)
- Internal 32Kbytes Memory for TX/RX Buffers
- 10BaseT/100BaseTX Ethernet PHY embedded
- Supports Auto Negotiation (Full and half duplex, 10 and 100-based )
- Not supports IP Fragmentation
- 3.3V operation with 5V I/O signal tolerance
- LED outputs (Full/Half duplex, Link, Speed, Active)
- 48 Pin LQFP Lead-Free Package (7x7mm, 0.5mm pitch)
There is a wide series of w5500 devices, but the most popular are 2 in particular.
The less expensive is the w5500 that you can see in the photo.
But now there is a compact version named w5500 lite, which is very interesting as a device for production.
Here my selection of tested ethernet devices w5500 lite - w5500 - enc26j60 mini - enc26j60
Wiring
Here my selection of esp32 devices AliExpress ESP32 Dev Kit v1 - AliExpress selectable - AliExpress TTGO T-Display 1.14 ESP32 - AliExpress NodeMCU V3 V2 ESP8266 Lolin32 - AliExpress WeMos Lolin32 CP2104 CH340 - AliExpress ESP32-CAM programmer - AliExpress ESP32-CAM bundle - AliExpress ESP32-WROOM-32 - AliExpress ESP32-S
This device uses an SPI interface; by default, I will use a base SPI interface.
ESP32 | w5500 |
---|---|
D5 | CS |
D18 | SCK |
D19 | MISO |
D23 | MOSI |
3.3v (better with external 200mha) | VCC |
GND | GND |
Same connection for w5500 lite, but the previous w5500 device can work with 3.3v of ESP32 (not ever true); this device It’s improbable that works without an external power supply.
Pay attention not all devices have so much ampere to power the w5500 device, so if you have trouble you must add an external power supply.
If you have trouble try to power the Ethernet device with an external power supply.
Library
A wide selection of libraries exists, but the standard one Is the best choice. You can find It in the standard Arduino library manager.
SSLClient
This library and device do not support SSL, so adding these features exists an alternate library named SSLClient that needs a little patch of Ethernet.
SSLClient adds TLS 1.2 functionality to any network library implementing the Arduino Client interface, including the Arduino EthernetClient and WiFiClient classes. SSLClient was created to integrate TLS seamlessly with the Arduino infrastructure using BearSSL as an underlying TLS engine. Unlike ArduinoBearSSL, SSLClient is entirely self-contained and does not require any additional hardware (other than a network connection). (cit.)
SSLClient with Ethernet
If you are using the Arduino Ethernet library, you will need to modify the library to support the large buffer sizes required by SSL (detailed in resources). You can either modify the library yourself or use this fork of the Ethernet library with the modification. To use the fork: download a zipped copy of the fork through GitHub, use the “add a .zip library” button in Arduino to install the library, and replace #include "Ethernet.h"
it with #include "EthernetLarge.h"
in your sketch. Alternatively, if, for some reason, this solution does not work, you can apply the modification manually using the instructions below.
Buffer extension
I also notice that to get good stability, you probably must change something else.
In SSLClient.h you must change this line.
to
Manual Modification
First, find the location of the library in the directory where Arduino is installed (C:\Program Files (x86)\Arduino
on Windows). Inside of this directory, navigate to libraries\Ethernet\src
(C:\Program Files (x86)\Arduino\libraries\Ethernet\src
on Windows). Modify Ethernet.h
to replace these lines:
With this:
You may need to use sudo
or administrator permissions to make this modification. We change MAX_SOCK_NUM
and ETHERNET_LARGE_BUFFERS
so the Ethernet hardware can allocate a larger space for SSLClient. However, a downside of this modification is that we can only have two sockets concurrently. As most microprocessors barely have enough memory for one SSL connection, this limitation will rarely be encountered in practice.
Code
Now we try to do a simple WebRequest with a native client.
But first, we write the code needed for the connection, we try to ask the IP to the DHCP server, and if It fails, we start a connection with a static IP.
Initialize device
Ethernet uses the default SPI interface by default, so first, we must set the correct SS pin (probably not needed).
This board’s different from LAN8720 (default device for esp32) and needs a MAC address.
Then we try to make a DHCP request.
But If it fails, we try to make a static IP connection with these parameters
and here is the connection code
Simple HTTP request
First of all, we’ll try to make a simple HTTP request. I chose an online service created to test this kind of request to do this test.
I’m going to use a simple service given from httpbin.org, and you can use the same REST API in HTTP and HTTPS.
Remember that HTTP work on port 80 HTTPS on 443, so to query the endpoint on 443 port, you must validate a certificate.
To make our connection, we use the basic EthernetClient.
And then, we try to connect and make a request to an endpoint in GET.
In the loop, wait for a response from the server.
And then read the response and put it on Serial output.
And finally, the complete sketch.
The result is this.
HTTPS request
Now, if we change the endpoint to port 443, we will request a secure server with SSL encryption.
And we obtain this response.
So the problem is that request and response messages aren’t transmitted using SSL (Secure Sockets Layer) or its successor TLS (Transport Layer Security). To add this feature, we are going to use SSLClient.
Retrieve certificate
To use an SSL, we need the server certificate, but in this case, SSLClient uses a trick given by BearSSL implementation. This minimal x509 verification engine allows using of Trust Anchors.
I add a simple online generator that you can find here.
You must only write the site’s address (httpbin.org) in the first input box, click Generate code
, copy the code, and put it inside a file called trust_anchors.h
and put it inside the sketch folder.
Here is the content of trust_anchors.h
.
Add SSLClient wrapper
Now we are going to add the SSLClient library and the trust_anchors.h
file.
I also changed the Ethernet library with EthernetLarge instead of the change described in the upper section.
Then apply the wrapper EthernetClient
with all the references of Trust Anchors. The file contains generated trust anchor array names TAs
with length. TAs_NUM
.
And we change the port to 443 (HTTPS).
Here is the complete sketch.
Now when we execute the sketch, we obtain this output.
Thanks
I use this SSLClient library in my EMailSender library to use Gmail SMTP server (SSL) with Ethernet. Unfortunately, SSL connections need quite a bit of resource, and only Arduino SAMD, STM32, and ESP32 have so many resources.
- ESP32: pinout, specs and Arduino IDE configuration
- ESP32: integrated SPIFFS Filesystem
- ESP32: manage multiple Serial and logging
- ESP32 practical power saving
- ESP32 practical power saving: manage WiFi and CPU
- ESP32 practical power saving: modem and light sleep
- ESP32 practical power saving: deep sleep and hibernation
- ESP32 practical power saving: preserve data, timer and touch wake up
- ESP32 practical power saving: external and ULP wake up
- ESP32 practical power saving: UART and GPIO wake up
- ESP32: integrated LittleFS FileSystem
- ESP32: integrated FFat (Fat/exFAT) FileSystem
- ESP32-wroom-32
- ESP32-CAM
- ESP32: use ethernet w5500 with plain (HTTP) and SSL (HTTPS)
- ESP32: use ethernet enc28j60 with plain (HTTP) and SSL (HTTPS)
- Firmware and OTA update management
- Firmware management
- ESP32: flash compiled firmware (.bin)
- ESP32: flash compiled firmware and filesystem (.bin) with GUI tools
- OTA update with Arduino IDE
- ESP32 OTA update with Arduino IDE: filesystem, firmware and password
- OTA update with Web Browser
- ESP32 OTA update with Web Browser: firmware, filesystem and authentication
- ESP32 OTA update with Web Browser: upload in HTTPS (SSL/TLS) with self signed certificate
- ESP32 OTA update with Web Browser: custom web interface
- Self OTA uptate from HTTP server
- ESP32 self OTA update firmware from server
- ESP32 self OTA update firmware from server with version check
- ESP32 self OTA update in HTTPS (SSL/TLS) with trusted self signed certificate
- Non standard Firmware update
- ESP32 firmware and filesystem update from SD card
- ESP32 firmware and filesystem update with FTP client
- Firmware management
- […]