Skip to content

W6100 khoih-prog / AsyncDNSServer_ESP32_Ethernet

This AsyncDNSServer_ESP32_Ethernet library is a fully asynchronous DNSServer library, designed for a trouble-free, multi-connection network.

eric_g

Published February 01, 2023

Original author: khoih prog · CanadaOriginal source (new tab)

W6100 khoih-prog / AsyncDNSServer_ESP32_Ethernet

Project description

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

 

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(); --> ESP32_W6100_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(); --> ESP32_W6100_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() 
{
}

Documents

Comments

Similar projects you might like

Comments