khoih-prog / AsyncDNSServer_ESP32_Ethernet

Fully Asynchronous DNS Server Library for ESP32 boards using LwIP W5500, W6100 or ENC28J60 Ethernet. This library is one of the current or future and more advanced Async libraries, such as AsyncWebServer_ESP32_Ethernet, AsyncHTTPRequest_ESP32_Ethernet, AsyncHTTPSRequest_ESP32_Ethernet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AsyncDNSServer_ESP32_Ethernet Library

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Features

This AsyncDNSServer_ESP32_Ethernet library is a fully asynchronous DNSServer library, designed for a trouble-free, multi-connection network environment, for ESP32/S2/S3/C3 boards using LwIP W5500, W6100 or ENC28J60 Ethernet.

This library is based on, modified from:

  1. Develo's ESPAsyncDNSServer

to apply the better and faster asynchronous feature of the powerful AsyncDNSServer_ESP32_Ethernet Library into ESP32/S2/S3/C3 boards using LwIP W5500, W6100 or ENC28J60 Ethernet.

Why Async is better

  • Using asynchronous network means that you can handle more than one connection at the same time
  • You are called once the packet is ready
  • After a DNS Client connected to this Async DNS server, you are immediately ready to handle other connections while the Server is taking care of receiving and responding to the UDP packets in the background.
  • You are not required to check in a tight loop() the arrival of the DNS requesting packets to process them.
  • Speed is OMG

Currently supported Boards

  1. ESP32 boards
  2. ESP32_S3-based boards (ESP32S3_DEV, ESP32_S3_BOX, UM TINYS3, UM PROS3, UM FEATHERS3, etc.)
  3. ESP32-S2 (ESP32-S2 Saola, AI-Thinker ESP-12K, etc.)
  4. ESP32-C3 (ARDUINO_ESP32C3_DEV, etc.)

using LwIP W5500, W6100 or ENC28J60 Ethernet


ESP32S3_DEV

ESP32S2_DEV

ESP32C3_DEV


W5500

FULL_DUPLEX, 100Mbps


W6100

FULL_DUPLEX, 100Mbps


ENC28J60

FULL_DUPLEX, 10Mbps


To-be Supported Boards

1. New ESP32 using LwIP W5500, W6100 or ENC28J60 Ethernet



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino
  2. ESP32 Core 2.0.6+ for ESP32-based boards. Latest release

For ESP32

  1. WebServer_ESP32_W5500 library 1.5.2+ for ESP32 using W5500. To install, check arduino-library-badge
  2. WebServer_ESP32_W6100 library 1.5.2+ for ESP32 using W6100. To install, check arduino-library-badge
  3. WebServer_ESP32_ENC library 1.5.1+ for ESP32 using ENC28J60. To install, check arduino-library-badge
  4. AsyncUDP_ESP32_W5500 library 2.0.0+ for ESP32 using W5500. To install, check arduino-library-badge
  5. AsyncUDP_ESP32_W6100 library 2.0.0+ for ESP32 using W6100. To install, check arduino-library-badge
  6. AsyncUdp_ESP32_ENC library 2.0.0+ for ESP32 using ENC28J60. To install, check arduino-library-badge

For ESP32_S2/S3/C3

  1. WebServer_ESP32_SC_W5500 library 1.2.1+ for ESP32_S2/S3/C3 using W5500. To install, check arduino-library-badge
  2. WebServer_ESP32_SC_W6100 library 1.2.1+ for ESP32_S2/S3/C3 using W6100. To install, check arduino-library-badge
  3. WebServer_ESP32_SC_ENC library 1.2.0+ for ESP32_S2/S3/C3 using ENC28J60. To install, check arduino-library-badge
  4. AsyncUDP_ESP32_SC_Ethernet library 2.2.0+ for ESP32_S2/S3/C3 using W5500, W6100 or ENC28J60. To install, check arduino-library-badge


Installation

The suggested way to install is to:

Use Arduino Library Manager

The best way is to use Arduino Library Manager. Search for AsyncDNSServer_ESP32_Ethernet, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

  1. Navigate to AsyncDNSServer_ESP32_Ethernet page.
  2. Download the latest release AsyncDNSServer_ESP32_Ethernet-main.zip.
  3. Extract the zip file to AsyncDNSServer_ESP32_Ethernet-main directory
  4. Copy the whole AsyncDNSServer_ESP32_Ethernet-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncDNSServer_ESP32_Ethernet library by using Library Manager. Search for AsyncDNSServer_ESP32_Ethernet in Platform.io Author's Libraries
  4. 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


Libraries' Patches

1. For fixing ESP32 compile error

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:



HOWTO Fix Multiple Definitions Linker Error

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 "AsyncDNSServer_ESP32_Ethernet.hpp"     //https://github.com/khoih-prog/AsyncDNSServer_ESP32_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 "AsyncDNSServer_ESP32_Ethernet.h"       //https://github.com/khoih-prog/AsyncDNSServer_ESP32_Ethernet


HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)

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.

1. ESP32 has 2 ADCs, named ADC1 and ADC2

2. ESP32 ADCs functions

  • ADC1 controls ADC function for pins GPIO32-GPIO39
  • ADC2 controls ADC function for pins GPIO0, 2, 4, 12-15, 25-27

3.. ESP32 WiFi uses ADC2 for WiFi functions

Look in file adc_common.c

In ADC2, there're two locks used for different cases:

  1. 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.

  2. 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)


HOWTO Setting up the Async DNS Server

For ESP32 using LwIP W5500 Ethernet

#include <AsyncDNSServer_ESP32_Ethernet.h>
#include <ESPAsyncWebServer.h>

const byte DNS_PORT = 53;

IPAddress apIP;

AsyncDNSServer dnsServer;

AsyncWebServer server(80);

void setup()
{
  ...
  
  ///////////////////////////////////

  // To be called before ETH.begin()
  ESP32_W5500_onEvent();

  // start the ethernet connection and the server:
  // Use DHCP dynamic IP and random mac
  //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
  //           int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
  ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
  //ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
  //ETH.config(myIP, myGW, mySN, myDNS);

  ESP32_W5500_waitForConnect();

  ///////////////////////////////////

  apIP = ETH.localIP();

  ///////////////////////////////////

	// modify TTL associated  with the domain name (in seconds)
	// default is 60 seconds
	dnsServer.setTTL(300);
	// set which return code will be used for all other domains (e.g. sending
	// ServerFailure instead of NonExistentDomain will reduce number of queries
	// sent by clients)
	// default is AsyncDNSReplyCode::NonExistentDomain
	dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure);

	// start DNS server for a specific domain name
	dnsServer.start(DNS_PORT, "*", apIP);

  ...
}

void loop() 
{
}


How to connect W5500, W6100 or ENC28J60 to ESP32

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
MOSI <---> GPIO23
MISO <---> GPIO19
SCK <---> GPIO18
CS/SS <---> GPIO5
INT <---> GPIO4
RST <---> RST
GND <---> GND
3.3V <---> 3.3V


How to connect W5500, W6100 or ENC28J60 to ESP32_S2/S3/C3

W5500

FULL_DUPLEX, 100Mbps


W6100

FULL_DUPLEX, 100Mbps


ENC28J60

FULL_DUPLEX, 10Mbps


ESP32S3_DEV

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

ESP32S2_DEV

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

ESP32C3_DEV

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


Examples

1. For ESP32_W5500

  1. AsyncCaptivePortalAdvanced_ESP32_W5500
  2. AsyncCaptivePortal_ESP32_W5500
  3. AsyncDNServerFull_ESP32_W5500
  4. AsyncDNSServer_ESP32_W5500

2. For ESP32_ENC

  1. AsyncCaptivePortalAdvanced_ESP32_ENC
  2. AsyncCaptivePortal_ESP32_ENC
  3. AsyncDNServerFull_ESP32_ENC
  4. AsyncDNSServer_ESP32_ENC

3. For ESP32_SC_W5500

  1. AsyncCaptivePortalAdvanced_ESP32_SC_W5500
  2. AsyncCaptivePortal_ESP32_SC_W5500
  3. AsyncDNServerFull_ESP32_SC_W5500
  4. AsyncDNSServer_ESP32_SC_W5500

4. For ESP32_SC_ENC

  1. AsyncCaptivePortalAdvanced_ESP32_SC_ENC
  2. AsyncCaptivePortal_ESP32_SC_ENC
  3. AsyncDNServerFull_ESP32_SC_ENC
  4. AsyncDNSServer_ESP32_SC_ENC

5. For ESP32_W6100

  1. AsyncCaptivePortalAdvanced_ESP32_W6100
  2. AsyncCaptivePortal_ESP32_W6100
  3. AsyncDNServerFull_ESP32_W6100
  4. AsyncDNSServer_ESP32_W6100

6. For ESP32_SC_W6100

  1. AsyncCaptivePortalAdvanced_ESP32_SC_W6100
  2. AsyncCaptivePortal_ESP32_SC_W6100
  3. AsyncDNServerFull_ESP32_SC_W6100
  4. AsyncDNSServer_ESP32_SC_W6100


#if !( defined(ESP32) )
#error This code is designed for (ESP32 + LwIP W5500) to run on ESP32 platform! Please check your Tools->Board setting.
#endif
#define USING_W5500 true
#define USING_W6100 false
#define USING_ENC28J60 false
#define ASYNC_DNS_ESP32_ETHERNET_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_DNS_ESP32_ETHERNET_LOGLEVEL_ 4
/////////////////////////////////////////////
// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20
byte mac[][NUMBER_OF_MAC] =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
};
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
//////////////////////////////////////////////////////////
// For ESP32-S3
// Optional values to override default settings
//#define ETH_SPI_HOST SPI2_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
//#define INT_GPIO 4
//#define MISO_GPIO 13
//#define MOSI_GPIO 11
//#define SCK_GPIO 12
//#define CS_GPIO 10
// For ESP32_C3
// Optional values to override default settings
// Don't change unless you know what you're doing
//#define ETH_SPI_HOST SPI2_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
//#define INT_GPIO 10
//#define MISO_GPIO 5
//#define MOSI_GPIO 6
//#define SCK_GPIO 4
//#define CS_GPIO 7
//////////////////////////////////////////////////////////
#include <AsyncDNSServer_ESP32_Ethernet.h>
#include <ESPAsyncWebServer.h>
//////////////////////////////////////////////////////////
const byte DNS_PORT = 53;
IPAddress apIP;
AsyncDNSServer dnsServer;
AsyncWebServer server(80);
void handleNotFound(AsyncWebServerRequest *request)
{
String message = "Hello World from " + String(ARDUINO_BOARD);
message += "\nURI: ";
message += request->url();
request->send(200, "text/plain", message);
}
void setup()
{
Serial.begin(115200);
while (!Serial && (millis() < 5000));
Serial.print(F("\nStart AsyncDNSServer_ESP32_W5500 on "));
Serial.print(ARDUINO_BOARD);
Serial.print(F(" with "));
Serial.println(SHIELD_TYPE);
Serial.println(WEBSERVER_ESP32_SC_W5500_VERSION);
Serial.println(ASYNC_UDP_ESP32_SC_ETHERNET_VERSION);
Serial.println(ASYNC_DNS_SERVER_ESP32_ETHERNET_VERSION);
Serial.setDebugOutput(true);
DNS_LOGWARN(F("Default SPI pinout:"));
DNS_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
DNS_LOGWARN1(F("MOSI:"), MOSI_GPIO);
DNS_LOGWARN1(F("MISO:"), MISO_GPIO);
DNS_LOGWARN1(F("SCK:"), SCK_GPIO);
DNS_LOGWARN1(F("CS:"), CS_GPIO);
DNS_LOGWARN1(F("INT:"), INT_GPIO);
DNS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
DNS_LOGWARN(F("========================="));
///////////////////////////////////
// To be called before ETH.begin()
ESP32_W5500_onEvent();
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(myIP, myGW, mySN, myDNS);
ESP32_W5500_waitForConnect();
///////////////////////////////////
apIP = ETH.localIP();
///////////////////////////////////
// modify TTL associated with the domain name (in seconds)
// default is 60 seconds
dnsServer.setTTL(300);
// set which return code will be used for all other domains
// (e.g. sending ServerFailure instead of NonExistentDomain will reduce number of queries
// sent by clients). Default is AsyncDNSReplyCode::NonExistentDomain
dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure);
// start DNS server for a specific domain name
dnsServer.start(DNS_PORT, "*", apIP);
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("AsyncDNSServer is @ IP : "));
Serial.println(apIP);
}
void loop()
{
}



Debug

Debug is enabled by default on Serial. To disable, use level 0

#define ASYNC_DNS_ESP32_ETHERNET_DEBUG_PORT      Serial

// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_DNS_ESP32_ETHERNET_LOGLEVEL_      0

You can also change the debugging level from 0 to 4

#define ASYNC_DNS_ESP32_ETHERNET_DEBUG_PORT      Serial


// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_DNS_ESP32_ETHERNET_LOGLEVEL_    4

Troubleshooting

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'm always using the latest stable cores/libraries.



Issues

Submit issues to: AsyncDNSServer_ESP32_Ethernet issues


TO DO

  1. Fix bug. Add enhancement
  2. Add support to more LwIP Ethernet shields

DONE

  1. Add support to ESP32/S3-based boards using LwIP W5500 or ENC28J60 Ethernet
  2. Add debugging features
  3. Add astyle using allman style. Restyle the library
  4. Add support to ESP32_S2/C3-based boards using LwIP W5500 or ENC28J60 Ethernet
  5. Add support to ESP32/S2/S3/C3-based boards using LwIP W6100 Ethernet


Contributions and Thanks

  1. Based on and modified from Develo's ESPAsyncDNSServer Library.
devyte
⭐️ Develo


Contributing

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


License

  • The library is licensed under GPLv3

Copyright

  • Copyright (c) 2016- Develo
  • Copyright (c) 2022- Khoi Hoang

About

Fully Asynchronous DNS Server Library for ESP32 boards using LwIP W5500, W6100 or ENC28J60 Ethernet. This library is one of the current or future and more advanced Async libraries, such as AsyncWebServer_ESP32_Ethernet, AsyncHTTPRequest_ESP32_Ethernet, AsyncHTTPSRequest_ESP32_Ethernet

License:GNU General Public License v3.0


Languages

Language:C 68.2%Language:C++ 31.0%Language:Shell 0.7%