Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Fully Asynchronous DNS Server Library for Teensy 4.1 using QNEthernet. This library is one of the current or future Async libraries to support Teensy 4.1 using QNEthernet, such as Teensy41_AsyncTCP, AsyncHTTPRequest_Teensy41, AsyncHTTPSRequest_Teensy41, AsyncMQTT_Generic, AsyncWebServer_Teensy41, AsyncDNSServer_Teensy41, AsyncDNSServer_Teensy41,…

License

Notifications You must be signed in to change notification settings

khoih-prog/AsyncDNSServer_Teensy41

Repository files navigation

AsyncDNSServer_Teensy41 Library

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Features

This AsyncDNSServer_Teensy41 library is a fully asynchronous DNSServer library, designed for a trouble-free, multi-connection network environment, for Teensy 4.1 using QNEthernet Library.

This library is based on, modified from:

  1. Develo's ESPAsyncDNSServer
  2. AsyncDNSServer_STM32

to apply the better and faster asynchronous feature into Teensy 4.1 using QNEthernet Library.

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. Teensy 4.1 using QNEthernet Library


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Teensy core v1.57+
  3. QNEthernet Library version v0.16.0+ for Teensy 4.1 built-in Ethernet.
  4. AsyncUDP_Teensy41 library v1.2.1+. 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_Teensy41, 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_Teensy41 page.
  2. Download the latest release AsyncDNSServer_Teensy41-main.zip.
  3. Extract the zip file to AsyncDNSServer_Teensy41-main directory
  4. Copy the whole AsyncDNSServer_Teensy41-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncDNSServer_Teensy41 library by using Library Manager. Search for AsyncDNSServer_Teensy41 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


Packages' Patches

1. For Teensy boards

To be able to compile and run on Teensy boards, you have to copy the files in Packages_Patches for Teensy directory into Teensy hardware directory (./arduino-1.8.19/hardware/teensy/avr/boards.txt).

Supposing the Arduino version is 1.8.19. These files must be copied into the directory:

  • ./arduino-1.8.19/hardware/teensy/avr/boards.txt
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy/Stream.h
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy3/Stream.h
  • ./arduino-1.8.19/hardware/teensy/avr/cores/teensy4/Stream.h

Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz These files must be copied into the directory:

  • ./arduino-x.yy.zz/hardware/teensy/avr/boards.txt
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy/Stream.h
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy3/Stream.h
  • ./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy4/Stream.h


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_Teensy41.hpp"         //https://github.com/khoih-prog/AsyncDNSServer_Teensy41

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_Teensy41.h"           //https://github.com/khoih-prog/AsyncDNSServer_Teensy41

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



HOWTO Setting up the Async DNS Server

#include "QNEthernet.h"       // https://github.com/ssilverman/QNEthernet
using namespace qindesign::network;

#include <AsyncDNSServer_Teensy41.h>

const byte DNS_PORT = 53;

IPAddress apIP;

AsyncDNSServer dnsServer;


void setup()
{
  ...
  
  // 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, "*", Ethernet.localIP());

  ...
}

void loop() 
{
}


Examples

  1. AsyncCaptivePortal
  2. AsyncCaptivePortalAdvanced
  3. AsyncDNServerFull
  4. AsyncDNSServer
  5. multiFileProject

#include "defines.h"
#include <AsyncDNSServer_Teensy41.h>
#include <AsyncWebServer_Teensy41.h>
const byte DNS_PORT = 53;
IPAddress apIP;
AsyncDNSServer dnsServer;
AsyncWebServer server(80);
void handleNotFound(AsyncWebServerRequest *request)
{
String message = "Hello World from " + String(BOARD_NAME) + " using QNEthernet\n\n";
message += "URI: ";
message += request->url();
request->send(200, "text/plain", message);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(1000);
Serial.print("\nStart AsyncDNSServer on ");
Serial.println(BOARD_NAME);
Serial.println(ASYNC_DNS_SERVER_TEENSY41_VERSION);
#if defined(ASYNC_UDP_TEENSY41_VERSION_MIN)
if (ASYNC_UDP_TEENSY41_VERSION_INT < ASYNC_UDP_TEENSY41_VERSION_MIN)
{
Serial.print("Warning. Must use this example on Version equal or later than : ");
Serial.println(ASYNC_UDP_TEENSY41_VERSION_MIN_TARGET);
}
#endif
delay(500);
#if USING_DHCP
// Start the Ethernet connection, using DHCP
Serial.print("Initialize Ethernet using DHCP => ");
Ethernet.begin();
#else
// Start the Ethernet connection, using static IP
Serial.print("Initialize Ethernet using static IP => ");
Ethernet.begin(myIP, myNetmask, myGW);
Ethernet.setDNSServerIP(mydnsServer);
#endif
if (!Ethernet.waitForLocalIP(5000))
{
Serial.println(F("Failed to configure Ethernet"));
if (!Ethernet.linkStatus())
{
Serial.println(F("Ethernet cable is not connected."));
}
// Stay here forever
while (true)
{
delay(1);
}
}
else
{
apIP = Ethernet.localIP();
Serial.print(F("Connected! IP address:"));
Serial.println(apIP);
}
#if USING_DHCP
delay(1000);
#else
delay(2000);
#endif
// 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("HTTP EthernetWebServer is @ IP : "));
Serial.println(apIP);
}
void loop()
{
}

2. File defines.h

#ifndef defines_h
#define defines_h
#if !( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) )
//#error Only Teensy 4.1 supported
#endif
#define ASYNC_UDP_TEENSY41_DEBUG_PORT Serial
// Debug Level from 0 to 4
#define _ASYNC_UDP_TEENSY41_LOGLEVEL_ 4
#define SHIELD_TYPE "Teensy4.1 QNEthernet"
#if (_ASYNC_UDP_TEENSY41_LOGLEVEL_ > 3)
#warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error
#endif
#define USING_DHCP true
//#define USING_DHCP false
#if !USING_DHCP
// Set the static IP address to use if the DHCP fails to assign
IPAddress myIP(192, 168, 2, 222);
IPAddress myNetmask(255, 255, 255, 0);
IPAddress myGW(192, 168, 2, 1);
//IPAddress mydnsServer(192, 168, 2, 1);
IPAddress mydnsServer(8, 8, 8, 8);
#endif
#include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet
using namespace qindesign::network;
#endif //defines_h



Debug

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

#define ASYNC_DNS_TEENSY41_DEBUG_PORT      Serial

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

You can also change the debugging level from 0 to 4

#define ASYNC_DNS_TEENSY41_DEBUG_PORT      Serial


// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_DNS_TEENSY41_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 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.



Issues

Submit issues to: AsyncDNSServer_Teensy41 issues


TO DO

  1. Fix bug. Add enhancement

DONE

  1. Initial porting and coding for Teensy 4.1 using built-in QNEthernet
  2. Add more examples.
  3. Add debugging features.
  4. Add astyle using allman style. Restyle the library


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 Teensy 4.1 using QNEthernet. This library is one of the current or future Async libraries to support Teensy 4.1 using QNEthernet, such as Teensy41_AsyncTCP, AsyncHTTPRequest_Teensy41, AsyncHTTPSRequest_Teensy41, AsyncMQTT_Generic, AsyncWebServer_Teensy41, AsyncDNSServer_Teensy41, AsyncDNSServer_Teensy41,…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published