- Why do we need this AsyncUDP_ESP32_SC_Ethernet library
- Changelog
- Prerequisites
- Installation
- Libraries' Patches
- HOWTO Fix
Multiple Definitions
Linker Error - HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)
- HOWTO Setting up the Async UDP Client
- How to connect W5500, W6100 or ENC28J60 to ESP32_S2/S3/C3
- Examples
- Example AsyncUdpNTPClient
- Debug Terminal Output Samples
- 1. AsyncUdpNTPClient on ESP32S3_DEV with ESP32_S3_W5500
- 2. AsyncUDPSendReceive on ESP32S3_DEV with ESP32_S3_W5500
- 3. AsyncUdpNTPClient on ESP32S3_DEV with ESP32_S3_ENC28J60
- 4. AsyncUDPSendReceive on ESP32S3_DEV with ESP32_S3_ENC28J60
- 5. AsyncUdpNTPClient on ESP32C3_DEV with ESP32_C3_ENC28J60
- 6. AsyncUdpNTPClient on ESP32S2_DEV with ESP32_S2_W5500
- 7. AsyncUdpNTPClient on ESP32S2_DEV with ESP32_S2_W6100
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this AsyncUDP_ESP32_SC_Ethernet library
This AsyncUDP_ESP32_SC_Ethernet library is a fully asynchronous UDP library, designed for a trouble-free, multi-connection network environment, for ESP32_S2/S3/C3
boards using LwIP
W5500, W6100 or ENC28J60 Ethernet. The library is easy to use and includes support for Unicast
, Broadcast
and Multicast
environments.
This library is based on, modified from:
to apply the better and faster asynchronous feature of the powerful AsyncUDP into ESP32_S2/S3/C3
boards using LwIP
W5500, W6100 or ENC28J60 Ethernet.
- Using asynchronous network means that you can handle more than one connection at the same time
- You are called once the request is ready and parsed
- When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background
- Speed is OMG
- After connecting to a UDP server as an Async Client, you are immediately ready to handle other connections while the Client is taking care of receiving the UDP responding packets in the background.
- You are not required to check in a tight loop() the arrival of the UDP responding packets to process them.
- ESP32_S3-based boards (ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)
- ESP32-S2 (ESP32-S2 Saola, AI-Thinker ESP-12K, etc.)
- ESP32-C3 (ARDUINO_ESP32C3_DEV, etc.)
using LwIP
W5500, W6100 or ENC28J60 Ethernet
FULL_DUPLEX, 100Mbps
FULL_DUPLEX, 100Mbps
FULL_DUPLEX, 10Mbps
Arduino IDE 1.8.19+
for ArduinoESP32 Core 2.0.5+
for ESP32-based boards.WebServer_ESP32_SC_W5500 library 1.2.1+
. To install, checkWebServer_ESP32_SC_W6100 library 1.2.1+
. To install, checkWebServer_ESP32_SC_ENC library 1.2.0+
. To install, check
The suggested way to install is to:
The best way is to use Arduino Library Manager
. Search for AsyncUDP_ESP32_SC_Ethernet
, then select / install the latest version. You can also use this link for more detailed instructions.
- Navigate to AsyncUDP_ESP32_SC_Ethernet page.
- Download the latest release
AsyncUDP_ESP32_SC_Ethernet-main.zip
. - Extract the zip file to
AsyncUDP_ESP32_SC_Ethernet-main
directory - Copy the whole
AsyncUDP_ESP32_SC_Ethernet-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install AsyncUDP_ESP32_SC_Ethernet library by using Library Manager. Search for AsyncUDP_ESP32_SC_Ethernet in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
To fix ESP32 compile error
, just copy the following file into the ESP32
cores/esp32 directory (e.g. ./arduino-1.8.19/hardware/espressif/cores/esp32) to overwrite the old file:
The current library implementation, using xyz-Impl.h
instead of standard xyz.cpp
, possibly creates certain Multiple Definitions
Linker error in certain use cases.
You can include this .hpp
file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "AsyncUDP_ESP32_SC_Ethernet.hpp" //https://github.com/khoih-prog/AsyncUDP_ESP32_SC_Ethernet
in many files. But be sure to use the following .h
file in just 1 .h
, .cpp
or .ino
file, which must not be included in any other file, to avoid Multiple Definitions
Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "AsyncUDP_ESP32_SC_Ethernet.h" //https://github.com/khoih-prog/AsyncUDP_ESP32_SC_Ethernet
Check the new multiFileProject example for a HOWTO
demo.
Please have a look at ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to have more detailed description and solution of the issue.
ADC1
controlsADC
function for pins GPIO32-GPIO39ADC2
controlsADC
function for pins GPIO0, 2, 4, 12-15, 25-27
Look in file adc_common.c
In ADC2, there're two locks used for different cases:
lock shared with app and Wi-Fi: ESP32: When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed. ESP32S2: The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
lock shared between tasks: when several tasks sharing the ADC2, we want to guarantee all the requests will be handled. Since conversions are short (about 31us), app returns the lock very soon, we use a spinlock to stand there waiting to do conversions one by one.
adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
- In order to use
ADC2
for other functions, we have to acquire complicated firmware locks and very difficult to do - So, it's not advisable to use
ADC2
with WiFi/BlueTooth (BT/BLE). - Use
ADC1
, and pins GPIO32-GPIO39 - If somehow it's a must to use those pins serviced by
ADC2
(GPIO0, 2, 4, 12, 13, 14, 15, 25, 26 and 27), use the fix mentioned at the end of ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example to work with ESP32 WiFi/BlueTooth (BT/BLE).
FULL_DUPLEX, 100Mbps
FULL_DUPLEX, 100Mbps
FULL_DUPLEX, 10Mbps
You can change the INT
pin to another one. Default is GPIO4
// Must connect INT to GPIOxx or not working
#define INT_GPIO 4
W5500, W6100 or ENC28J60 | <---> | ESP32_S3 |
---|---|---|
MOSI | <---> | GPIO11 |
MISO | <---> | GPIO13 |
SCK | <---> | GPIO12 |
CS/SS | <---> | GPIO10 |
INT | <---> | GPIO4 |
RST | <---> | RST |
GND | <---> | GND |
3.3V | <---> | 3.3V |
You can change the INT
pin to another one. Default is GPIO4
// Must connect INT to GPIOxx or not working
#define INT_GPIO 4
W5500, W6100 or ENC28J60 | <---> | ESP32_S2 |
---|---|---|
MOSI | <---> | GPIO35 |
MISO | <---> | GPIO37 |
SCK | <---> | GPIO36 |
CS/SS | <---> | GPIO34 |
INT | <---> | GPIO4 |
RST | <---> | RST |
GND | <---> | GND |
3.3V | <---> | 3.3V |
You can change the INT
pin to another one. Default is GPIO4
// Must connect INT to GPIOxx or not working
#define INT_GPIO 10
W5500, W6100 or ENC28J60 | <---> | ESP32_C3 |
---|---|---|
MOSI | <---> | GPIO6 |
MISO | <---> | GPIO5 |
SCK | <---> | GPIO4 |
CS/SS | <---> | GPIO7 |
INT | <---> | GPIO10 |
RST | <---> | RST |
GND | <---> | GND |
3.3V | <---> | 3.3V |
- AsyncUDPClient
- AsyncUdpNTPClient
- AsyncUdpSendReceive
- AsyncUDPServer
- AsyncUDPMulticastServer
- multiFileProject
Example AsyncUdpNTPClient
This is terminal debug output when running AsyncUdpNTPClient on ESP32_S3_W5500 (ESP32S3_DEV + W5500). It connects to NTP Server using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUdpNTPClient on ESP32S3_DEV with ESP32_S3_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 2
[UDP] MOSI: 11
[UDP] MISO: 13
[UDP] SCK: 12
[UDP] CS: 10
[UDP] INT: 4
[UDP] SPI Clock (MHz): 25
[UDP] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:0C, IPv4: 192.168.2.34
FULL_DUPLEX, 100Mbps
AsyncUdpNTPClient started @ IP address: 192.168.2.34
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.34:55314, Length: 48
Seconds since Jan 1 1900 = 3882392659
Epoch/Unix time = 1673403859
The UTC/GMT time is Wed 2023-01-11 02:24:19 GMT
This is terminal debug output when running AsyncUDPSendReceive on ESP32_S3_W5500 (ESP32S3_DEV + W5500). It connects to NTP Server 0.ca.pool.ntp.org
(IP=208.81.1.244) using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is received and processed asynchronously to print current UTC/GMT
time. The ACK packet is then sent to give acknowledge to the NTP server
Start AsyncUDPSendReceive on ESP32S3_DEV with ESP32_S3_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 2
[UDP] MOSI: 11
[UDP] MISO: 13
[UDP] SCK: 12
[UDP] CS: 10
[UDP] INT: 4
[UDP] SPI Clock (MHz): 25
[UDP] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:FE:11, IPv4: 192.168.2.34
FULL_DUPLEX, 100Mbps
AsyncUDPSendReceive started @ IP address: 192.168.2.34
Starting connection to server...
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.34:62164, Length: 48
Seconds since Jan 1 1900 = 3882392738
Epoch/Unix time = 1673403938
The UTC/GMT time is Wed 2023-01-11 02:25:38 GMT
============= sendACKPacket =============
This is terminal debug output when running AsyncUdpNTPClient on ESP32_S3_ENC28J60 (ESP32S3_DEV + ENC28J60). It connects to NTP Server using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUdpNTPClient on ESP32S3_DEV with ESP32_S3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 1
[UDP] MOSI: 11
[UDP] MISO: 13
[UDP] SCK: 12
[UDP] CS: 10
[UDP] INT: 4
[UDP] SPI Clock (MHz): 8
[UDP] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:FE:0F, IPv4: 192.168.2.34
FULL_DUPLEX, 10Mbps
AsyncUdpNTPClient started @ IP address: 192.168.2.34
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.34:62164, Length: 48
Seconds since Jan 1 1900 = 3882392857
Epoch/Unix time = 1673404057
The UTC/GMT time is Wed 2023-01-11 02:27:37 GMT
This is terminal debug output when running AsyncUDPSendReceive on ESP32_S3_ENC28J60 (ESP32S3_DEV + ENC28J60). It connects to NTP Server 0.ca.pool.ntp.org
(IP=208.81.1.244) using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is received and processed asynchronously to print current UTC/GMT
time. The ACK packet is then sent to give acknowledge to the NTP server
Start AsyncUDPSendReceive on ESP32S3_DEV with ESP32_S3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 1
[UDP] MOSI: 11
[UDP] MISO: 13
[UDP] SCK: 12
[UDP] CS: 10
[UDP] INT: 4
[UDP] SPI Clock (MHz): 8
[UDP] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:FE:0F, IPv4: 192.168.2.34
FULL_DUPLEX, 10Mbps
AsyncUDPSendReceive started @ IP address: 192.168.2.34
Starting connection to server...
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.34:62164, Length: 48
Seconds since Jan 1 1900 = 3882392738
Epoch/Unix time = 1673403938
The UTC/GMT time is Wed 2023-01-11 02:25:38 GMT
============= sendACKPacket =============
This is terminal debug output when running AsyncUdpNTPClient on ESP32_C3_ENC28J60 (ESP32C3_DEV + ENC28J60). It connects to NTP Server using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUdpNTPClient on ESP32C3_DEV with ESP32_C3_ENC28J60
WebServer_ESP32_SC_ENC v1.2.0 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 1
[UDP] MOSI: 6
[UDP] MISO: 5
[UDP] SCK: 4
[UDP] CS: 7
[UDP] INT: 10
[UDP] SPI Clock (MHz): 8
[UDP] =========================
Using built-in mac_eth = 7C:DF:A1:DA:66:87
ETH Started
ETH Connected
ETH MAC: 7C:DF:A1:DA:66:87, IPv4: 192.168.2.134
FULL_DUPLEX, 10Mbps
AsyncUdpNTPClient started @ IP address: 192.168.2.134
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.134:62164, Length: 48
Seconds since Jan 1 1900 = 3882392857
Epoch/Unix time = 1673404057
The UTC/GMT time is Wed 2023-01-11 02:27:37 GMT
This is terminal debug output when running AsyncUdpNTPClient on ESP32_S2_W5500 (ESP32S2_DEV + W5500). It connects to NTP Server using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUdpNTPClient on ESP32S2_DEV with ESP32_S2_W5500
WebServer_ESP32_SC_W5500 v1.2.1 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 2
[UDP] MOSI: 35
[UDP] MISO: 37
[UDP] SCK: 36
[UDP] CS: 34
[UDP] INT: 4
[UDP] SPI Clock (MHz): 25
[UDP] =========================
Using built-in mac_eth = 7E:DF:A1:08:32:C9
ETH Started
ETH Connected
ETH MAC: 7E:DF:A1:08:32:C9, IPv4: 192.168.2.36
FULL_DUPLEX, 100Mbps
AsyncUdpNTPClient started @ IP address: 192.168.2.36
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.36:55314, Length: 48
Seconds since Jan 1 1900 = 3882392659
Epoch/Unix time = 1673403859
The UTC/GMT time is Wed 2023-01-11 02:24:19 GMT
This is terminal debug output when running AsyncUdpNTPClient on ESP32_S2_W6100 (ESP32S2_DEV + W6100). It connects to NTP Server using AsyncUDP_ESP32_SC_Ethernet
library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUdpNTPClient on ESP32S2_DEV with ESP32_S2_W5500
WebServer_ESP32_SC_W6100 v1.2.1 for core v2.0.0+
AsyncUDP_ESP32_SC_Ethernet v2.2.0 for core v2.0.0+
[UDP] Default SPI pinout:
[UDP] SPI_HOST: 2
[UDP] MOSI: 35
[UDP] MISO: 37
[UDP] SCK: 36
[UDP] CS: 34
[UDP] INT: 4
[UDP] SPI Clock (MHz): 25
[UDP] =========================
Using built-in mac_eth = 7E:DF:A1:08:32:C9
ETH Started
ETH Connected
ETH MAC: 7E:DF:A1:08:32:C9, IPv4: 192.168.2.36
FULL_DUPLEX, 100Mbps
AsyncUdpNTPClient started @ IP address: 192.168.2.36
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.36:55314, Length: 48
Seconds since Jan 1 1900 = 3882392659
Epoch/Unix time = 1673403859
The UTC/GMT time is Wed 2023-01-11 02:24:19 GMT
Debug is enabled by default on Serial. To disable, use level 0
#define ASYNC_UDP_ESP32_SC_ETHERNET_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_UDP_ESP32_SC_ETHERNET_LOGLEVEL_ 1
You can also change the debugging level from 0 to 4
#define ASYNC_UDP_ESP32_SC_ETHERNET_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_UDP_ESP32_SC_ETHERNET_LOGLEVEL_ 4
If you get compilation errors, more often than not, you may need to install a newer version of Arduino IDE, the Arduino ESP32
core or depending libraries.
Sometimes, the library will only work if you update the ESP32
core to the latest and stable version because I am always using the latest stable cores/libraries.
Submit issues to: AsyncUDP_ESP32_SC_Ethernet issues
- Fix bug. Add enhancement
- Add support to more
LwIP Ethernet
shields
- Initial port to
ESP32_S3
boards usingLwIP W5500, W6100 or ENC28J60 Ethernet
- Use
allman astyle
and addutils
. Restyle the library - Add support to
ESP32_S2
andESP32_C3
usingLwIP W5500, W6100 or ENC28J60 Ethernet
- Add support to
ESP32_S2/S3/C3
usingLwIP W6100 Ethernet
- Based on and modified from Hristo Gochkov's AsyncUDP. Many thanks to Hristo Gochkov for great AsyncUDP Library
⭐️⭐️ Hristo Gochkov |
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under GPLv3
Copyright (c) 2018- Hristo Gochkov
Copyright (c) 2022- Khoi Hoang