- Why do we need this AsyncUDP_Ethernet library
- Changelog
- Prerequisites
- Installation
- HOWTO Fix
Multiple Definitions
Linker Error - HOWTO Setting up the Async UDP Client
- Examples
- Example AsyncUdpNTPClient
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this AsyncUDP_Ethernet library
This AsyncUDP_Ethernet library is a fully asynchronous UDP library, designed for a trouble-free, multi-connection network environment, for ESP8266 boards using W5x00 or ENC28J60 Ethernet using lwIP_w5100, lwIP_w5500 or lwIP_enc28j60 library.
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 ESPAsyncUDP Library into ESP8266 boards using W5x00 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.
- ESP8266 boards using W5x00 or ENC28J60 Ethernet
Arduino IDE 1.8.19+
for Arduino.ESP8266 Core 3.0.2+
for ESP8266-based boards. .
The suggested way to install is to:
The best way is to use Arduino Library Manager
. Search for AsyncUDP_Ethernet
, then select / install the latest version. You can also use this link for more detailed instructions.
- Navigate to AsyncUDP_Ethernet page.
- Download the latest release
AsyncUDP_Ethernet-main.zip
. - Extract the zip file to
AsyncUDP_Ethernet-main
directory - Copy the whole
AsyncUDP_Ethernet-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install AsyncUDP_Ethernet library by using Library Manager. Search for AsyncUDP_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
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_Ethernet.hpp" //https://github.com/khoih-prog/AsyncUDP_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_Ethernet.h" //https://github.com/khoih-prog/AsyncUDP_Ethernet
Check the new multiFileProject example for a HOWTO
demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
#include <AsyncUDP_Ethernet.h>
IPAddress timeWindowsCom = IPAddress(13, 86, 101, 172);
#define NTP_REQUEST_PORT 123
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
AsyncUDP Udp;
// send an NTP request to the time server at the given address
void createNTPpacket(void)
{
...
}
void sendNTPPacket(void)
{
createNTPpacket();
//Send unicast
Udp.write(packetBuffer, sizeof(packetBuffer));
}
void parsePacket(AsyncUDPPacket packet)
{
...
}
void setup()
{
...
//NTP requests are to port NTP_REQUEST_PORT = 123
if (Udp.connect(timeWindowsCom, NTP_REQUEST_PORT))
{
// Setting up Async packet Handler
Udp.onPacket([](AsyncUDPPacket packet)
{
parsePacket(packet);
});
}
}
void loop()
{
sendNTPPacket();
// wait 60 seconds before asking for the time again
delay(60000);
}
- AsyncUDPClient
- AsyncUDPMulticastServer
- AsyncUdpNTPClient
- AsyncUdpSendReceive
- AsyncUDPServer
- multiFileProject
Example AsyncUdpNTPClient
1. File AsyncUdpNTPClient.ino
AsyncUDP_Ethernet/examples/AsyncUdpNTPClient/AsyncUdpNTPClient.ino
Lines 12 to 202 in 8db69d3
2. File defines.h
AsyncUDP_Ethernet/examples/AsyncUdpNTPClient/defines.h
Lines 12 to 74 in 8db69d3
This is terminal debug output when running AsyncUdpNTPClient on ESP8266_NODEMCU_ESP12E with ESP8266_W5500 Ethernet. It connects to NTP Server "0.ca.pool.ntp.org" (IPAddress(208, 81, 1, 244)) using AsyncUDP_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 ESP8266_NODEMCU_ESP12E with ESP8266_W5500 Ethernet
AsyncUDP_Ethernet v1.2.1
Connecting to network : ..
Ethernet DHCP IP address: 192.168.2.188
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.188:56234, Length: 48
Seconds since Jan 1 1900 = 3858990229
Epoch/Unix time = 1650001429
The UTC/GMT time is Fri 2022-04-15 05:43:49 GMT
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.188:56234, Length: 48
Seconds since Jan 1 1900 = 3858990289
Epoch/Unix time = 1650001489
The UTC/GMT time is Fri 2022-04-15 05:44:49 GMT
This is terminal debug output when running AsyncUDPSendReceive on ESP8266_NODEMCU_ESP12E with ESP8266_W5500 Ethernet. It connects to NTP Server "time.nist.gov" (IPAddress(208, 81, 1, 244)) using AsyncUDP_Ethernet library, and requests NTP time every 60s. The packet is then received and processed asynchronously to print current UTC/GMT time.
Start AsyncUDPSendReceive on ESP8266_NODEMCU_ESP12E with ESP8266_W5500 Ethernet
AsyncUDP_Ethernet v1.2.1
Connecting to network : .
Ethernet DHCP IP address: 192.168.2.188
Starting connection to server...
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.188:63199, Length: 48
Seconds since Jan 1 1900 = 3859022269
Epoch/Unix time = 1650033469
The UTC/GMT time is Fri 2022-04-15 14:37:49 GMT
============= sendACKPacket =============
This is terminal debug output when running AsyncUdpNTPClient on ESP8266_NODEMCU_ESP12E with ESP8266_ENC28J60 Ethernet. It connects to NTP Server "0.ca.pool.ntp.org" (IPAddress(208, 81, 1, 244)) using AsyncUDP_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 ESP8266_NODEMCU_ESP12E with ESP8266_W5500 Ethernet
AsyncUDP_Ethernet v1.2.1
Connecting to network : ..
Ethernet DHCP IP address: 192.168.2.188
UDP connected
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.188:56234, Length: 48
Seconds since Jan 1 1900 = 3858990229
Epoch/Unix time = 1650001429
The UTC/GMT time is Fri 2022-04-15 05:43:49 GMT
============= createNTPpacket =============
Received UDP Packet Type: Unicast
From: 208.81.1.244:123, To: 192.168.2.188:56234, Length: 48
Seconds since Jan 1 1900 = 3858990289
Epoch/Unix time = 1650001489
The UTC/GMT time is Fri 2022-04-15 05:44:49 GMT
Debug is enabled by default on Serial. To disable, use level 0
#define ASYNC_UDP_ETHERNET_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_UDP_ETHERNET_LOGLEVEL_ 0
You can also change the debugging level from 0 to 4
#define ASYNC_UDP_ETHERNET_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_UDP_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 STM32
core or depending libraries.
Sometimes, the library will only work if you update the STM32
core to the latest version because I am always using the latest cores /libraries.
Submit issues to: AsyncUDP_Ethernet issues
- Fix bug. Add enhancement
- Initial porting and coding for ESP8266 using W5x00 or ENC28J60 Ethernet using lwIP_w5100, lwIP_w5500 or lwIP_enc28j60 library.
- Add more examples.
- Add debugging features.
- Based on and modified from Hristo Gochkov's ESPAsyncUDP. Many thanks to Hristo Gochkov for good ESPAsyncUDP 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) 2016- Hristo Gochkov
- Copyright (c) 2022- Khoi Hoang