|
| 1 | +# AsyncDNSServer_ESP32_ENC |
| 2 | + |
| 3 | + |
| 4 | +[](https://www.ardu-badge.com/AsyncDNSServer_ESP32_ENC) |
| 5 | +[](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC/releases) |
| 6 | +[](#Contributing) |
| 7 | +[](http://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC/issues) |
| 8 | + |
| 9 | + |
| 10 | +<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a> |
| 11 | +<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a> |
| 12 | +<a href="https://profile-counter.glitch.me/khoih-prog/count.svg" title="Total khoih-prog Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog/count.svg" style="height: 30px;width: 200px;"></a> |
| 13 | +<a href="https://profile-counter.glitch.me/khoih-prog-AsyncDNSServer_ESP32_ENC/count.svg" title="Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog-AsyncDNSServer_ESP32_ENC/count.svg" style="height: 30px;width: 200px;"></a> |
| 14 | + |
| 15 | + |
| 16 | +--- |
| 17 | +--- |
| 18 | + |
| 19 | +## Table of Contents |
| 20 | + |
| 21 | + |
| 22 | +* [Why do we need this AsyncDNSServer_ESP32_ENC library](#why-do-we-need-this-AsyncDNSServer_ESP32_ENC-library) |
| 23 | + * [Features](#features) |
| 24 | + * [Why Async is better](#why-async-is-better) |
| 25 | + * [Currently supported Boards](#currently-supported-boards) |
| 26 | +* [Changelog](changelog.md) |
| 27 | +* [Prerequisites](#prerequisites) |
| 28 | +* [Installation](#installation) |
| 29 | + * [Use Arduino Library Manager](#use-arduino-library-manager) |
| 30 | + * [Manual Install](#manual-install) |
| 31 | + * [VS Code & PlatformIO](#vs-code--platformio) |
| 32 | +* [Libraries' Patches](#libraries-patches) |
| 33 | + * [1. For fixing ESP32 compile error](#1-for-fixing-esp32-compile-error) |
| 34 | +* [HOWTO Fix `Multiple Definitions` Linker Error](#howto-fix-multiple-definitions-linker-error) |
| 35 | +* [HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE)](#howto-use-analogread-with-esp32-running-wifi-andor-bluetooth-btble) |
| 36 | + * [1. ESP32 has 2 ADCs, named ADC1 and ADC2](#1--esp32-has-2-adcs-named-adc1-and-adc2) |
| 37 | + * [2. ESP32 ADCs functions](#2-esp32-adcs-functions) |
| 38 | + * [3. ESP32 WiFi uses ADC2 for WiFi functions](#3-esp32-wifi-uses-adc2-for-wifi-functions) |
| 39 | +* [HOWTO Setting up the Async DNS Server](#howto-setting-up-the-async-dns-server) |
| 40 | +* [Examples](#examples) |
| 41 | + * [ 1. AsyncCaptivePortalAdvanced_ESP32_ENC](examples/AsyncCaptivePortalAdvanced_ESP32_ENC) |
| 42 | + * [ 2. AsyncCaptivePortal_ESP32_ENC](examples/AsyncCaptivePortal_ESP32_ENC) |
| 43 | + * [ 3. AsyncDNServerFull_ESP32_ENC](examples/AsyncDNServerFull_ESP32_ENC) |
| 44 | + * [ 4. AsyncDNSServer_ESP32_ENC](examples/AsyncDNSServer_ESP32_ENC) |
| 45 | +* [Example AsyncDNSServer_ESP32_ENC](#example-AsyncDNSServer_ESP32_ENC) |
| 46 | + * [1. File AsyncDNSServer_ESP32_ENC.ino](#1-file-AsyncDNSServer_ESP32_ENCino) |
| 47 | +* [Debug](#debug) |
| 48 | +* [Troubleshooting](#troubleshooting) |
| 49 | +* [Issues](#issues) |
| 50 | +* [TO DO](#to-do) |
| 51 | +* [DONE](#done) |
| 52 | +* [Contributions and Thanks](#contributions-and-thanks) |
| 53 | +* [Contributing](#contributing) |
| 54 | +* [License](#license) |
| 55 | +* [Copyright](#copyright) |
| 56 | + |
| 57 | +--- |
| 58 | +--- |
| 59 | + |
| 60 | +### Why do we need this [AsyncDNSServer_ESP32_ENC library](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC) |
| 61 | + |
| 62 | +#### Features |
| 63 | + |
| 64 | +This [AsyncDNSServer_ESP32_ENC library](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC) is a fully asynchronous DNSServer library, designed for a trouble-free, multi-connection network environment, for **ESP32 boards using LwIP ENC28J60 Ethernet**. |
| 65 | + |
| 66 | +This library is based on, modified from: |
| 67 | + |
| 68 | +1. [Develo's ESPAsyncDNSServer](https://github.com/devyte/ESPAsyncDNSServer) |
| 69 | + |
| 70 | +to apply the better and faster **asynchronous** feature of the **powerful** [AsyncDNSServer_ESP32_ENC Library](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC) into **ESP32 boards using LwIP ENC28J60 Ethernet**. |
| 71 | + |
| 72 | + |
| 73 | +#### Why Async is better |
| 74 | + |
| 75 | +- Using asynchronous network means that you can handle **more than one connection at the same time** |
| 76 | +- You are called once the packet is ready |
| 77 | +- 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. |
| 78 | +- You are not required to check in a tight loop() the arrival of the DNS requesting packets to process them. |
| 79 | +- **Speed is OMG** |
| 80 | + |
| 81 | + |
| 82 | +#### Currently supported Boards |
| 83 | + |
| 84 | +1. **ESP32_ENC boards** using ESP32 boards with `LwIP` ENC28J60 Ethernet |
| 85 | + |
| 86 | +--- |
| 87 | +--- |
| 88 | + |
| 89 | +## Prerequisites |
| 90 | + |
| 91 | + 1. [`Arduino IDE 1.8.19+` for Arduino](https://www.arduino.cc/en/Main/Software) |
| 92 | + 2. [`ESP32 Core 2.0.5+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [](https://github.com/espressif/arduino-esp32/releases/latest/) |
| 93 | + 3. [`WebServer_ESP32_ENC library 1.5.1+`](https://github.com/khoih-prog/WebServer_ESP32_ENC). To install, check [](https://www.ardu-badge.com/WebServer_ESP32_ENC) |
| 94 | + 4. [`AsyncUdp_ESP32_ENC library 2.0.0+`](https://github.com/khoih-prog/AsyncUdp_ESP32_ENC). To install, check [](https://www.ardu-badge.com/AsyncUdp_ESP32_ENC) |
| 95 | + |
| 96 | +--- |
| 97 | +--- |
| 98 | + |
| 99 | +### Installation |
| 100 | + |
| 101 | +The suggested way to install is to: |
| 102 | + |
| 103 | +#### Use Arduino Library Manager |
| 104 | + |
| 105 | +The best way is to use `Arduino Library Manager`. Search for `AsyncDNSServer_ESP32_ENC`, then select / install the latest version. You can also use this link [](https://www.ardu-badge.com/AsyncDNSServer_ESP32_ENC) for more detailed instructions. |
| 106 | + |
| 107 | +### Manual Install |
| 108 | + |
| 109 | +1. Navigate to [AsyncDNSServer_ESP32_ENC](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC) page. |
| 110 | +2. Download the latest release `AsyncDNSServer_ESP32_ENC-main.zip`. |
| 111 | +3. Extract the zip file to `AsyncDNSServer_ESP32_ENC-main` directory |
| 112 | +4. Copy the whole `AsyncDNSServer_ESP32_ENC-main` folder to Arduino libraries' directory such as `~/Arduino/libraries/`. |
| 113 | + |
| 114 | +### VS Code & PlatformIO: |
| 115 | + |
| 116 | +1. Install [VS Code](https://code.visualstudio.com/) |
| 117 | +2. Install [PlatformIO](https://platformio.org/platformio-ide) |
| 118 | +3. Install [**AsyncDNSServer_ESP32_ENC** library](https://registry.platformio.org/libraries/AsyncDNSServer_ESP32_ENC) by using [Library Manager](https://registry.platformio.org/libraries/AsyncDNSServer_ESP32_ENC/installation). Search for AsyncDNSServer_ESP32_ENC in [Platform.io Author's Libraries](https://platformio.org/lib/search?query=author:%22Khoi%20Hoang%22) |
| 119 | +4. Use included [platformio.ini](platformio/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](https://docs.platformio.org/page/projectconf.html) |
| 120 | + |
| 121 | +--- |
| 122 | +--- |
| 123 | + |
| 124 | +### Libraries' Patches |
| 125 | + |
| 126 | +#### 1. For fixing ESP32 compile error |
| 127 | + |
| 128 | +To fix [`ESP32 compile error`](https://github.com/espressif/arduino-esp32), just copy the following file into the [`ESP32`](https://github.com/espressif/arduino-esp32) cores/esp32 directory (e.g. ./arduino-1.8.19/hardware/espressif/cores/esp32) to overwrite the old file: |
| 129 | +- [Server.h](LibraryPatches/esp32/cores/esp32/Server.h) |
| 130 | + |
| 131 | +--- |
| 132 | +--- |
| 133 | + |
| 134 | +### HOWTO Fix `Multiple Definitions` Linker Error |
| 135 | + |
| 136 | +The current library implementation, using `xyz-Impl.h` instead of standard `xyz.cpp`, possibly creates certain `Multiple Definitions` Linker error in certain use cases. |
| 137 | + |
| 138 | +You can include this `.hpp` file |
| 139 | + |
| 140 | +```cpp |
| 141 | +// Can be included as many times as necessary, without `Multiple Definitions` Linker Error |
| 142 | +#include "AsyncDNSServer_ESP32_ENC.hpp" //https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC |
| 143 | +``` |
| 144 | + |
| 145 | +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 |
| 146 | + |
| 147 | +```cpp |
| 148 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 149 | +#include "AsyncDNSServer_ESP32_ENC.h" //https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC |
| 150 | +``` |
| 151 | + |
| 152 | +Check the new [**multiFileProject** example](examples/multiFileProject) for a `HOWTO` demo. |
| 153 | + |
| 154 | +--- |
| 155 | +--- |
| 156 | + |
| 157 | +### HOWTO Use analogRead() with ESP32 running WiFi and/or BlueTooth (BT/BLE) |
| 158 | + |
| 159 | +Please have a look at [**ESP_WiFiManager Issue 39: Not able to read analog port when using the autoconnect example**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to have more detailed description and solution of the issue. |
| 160 | + |
| 161 | +#### 1. ESP32 has 2 ADCs, named ADC1 and ADC2 |
| 162 | + |
| 163 | +#### 2. ESP32 ADCs functions |
| 164 | + |
| 165 | +- `ADC1` controls ADC function for pins **GPIO32-GPIO39** |
| 166 | +- `ADC2` controls ADC function for pins **GPIO0, 2, 4, 12-15, 25-27** |
| 167 | + |
| 168 | +#### 3.. ESP32 WiFi uses ADC2 for WiFi functions |
| 169 | + |
| 170 | +Look in file [**adc_common.c**](https://github.com/espressif/esp-idf/blob/master/components/driver/adc_common.c#L61) |
| 171 | + |
| 172 | +> In `ADC2`, there're two locks used for different cases: |
| 173 | +> 1. lock shared with app and Wi-Fi: |
| 174 | +> ESP32: |
| 175 | +> When Wi-Fi using the `ADC2`, we assume it will never stop, so app checks the lock and returns immediately if failed. |
| 176 | +> ESP32S2: |
| 177 | +> The controller's control over the ADC is determined by the arbiter. There is no need to control by lock. |
| 178 | +> |
| 179 | +> 2. lock shared between tasks: |
| 180 | +> when several tasks sharing the `ADC2`, we want to guarantee |
| 181 | +> all the requests will be handled. |
| 182 | +> Since conversions are short (about 31us), app returns the lock very soon, |
| 183 | +> we use a spinlock to stand there waiting to do conversions one by one. |
| 184 | +> |
| 185 | +> adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock. |
| 186 | +
|
| 187 | + |
| 188 | +- In order to use `ADC2` for other functions, we have to **acquire complicated firmware locks and very difficult to do** |
| 189 | +- So, it's not advisable to use `ADC2` with WiFi/BlueTooth (BT/BLE). |
| 190 | +- Use `ADC1`, and pins GPIO32-GPIO39 |
| 191 | +- 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**](https://github.com/khoih-prog/ESP_WiFiManager/issues/39) to work with ESP32 WiFi/BlueTooth (BT/BLE) |
| 192 | + |
| 193 | +--- |
| 194 | +--- |
| 195 | + |
| 196 | +## HOWTO Setting up the Async DNS Server |
| 197 | + |
| 198 | +```cpp |
| 199 | +#include <AsyncDNSServer_ESP32_ENC.h> |
| 200 | +#include <ESPAsyncWebServer.h> |
| 201 | + |
| 202 | +const byte DNS_PORT = 53; |
| 203 | + |
| 204 | +IPAddress apIP; |
| 205 | + |
| 206 | +AsyncDNSServer dnsServer; |
| 207 | + |
| 208 | +AsyncWebServer server(80); |
| 209 | + |
| 210 | +void setup() |
| 211 | +{ |
| 212 | + ... |
| 213 | + |
| 214 | + /////////////////////////////////// |
| 215 | + |
| 216 | + // To be called before ETH.begin() |
| 217 | + ESP32_ENC_onEvent(); |
| 218 | + |
| 219 | + //bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ, |
| 220 | + // int SPI_HOST, uint8_t *ENC28J60_Mac = ENC28J60_Default_Mac); |
| 221 | + ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, SPI_HOST ); |
| 222 | + |
| 223 | + // Static IP, leave without this line to get IP via DHCP |
| 224 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 225 | + //ETH.config(myIP, myGW, mySN, myDNS); |
| 226 | + |
| 227 | + ESP32_ENC_waitForConnect(); |
| 228 | + |
| 229 | + apIP = ETH.localIP(); |
| 230 | + |
| 231 | + /////////////////////////////////// |
| 232 | + |
| 233 | + // modify TTL associated with the domain name (in seconds) |
| 234 | + // default is 60 seconds |
| 235 | + dnsServer.setTTL(300); |
| 236 | + // set which return code will be used for all other domains (e.g. sending |
| 237 | + // ServerFailure instead of NonExistentDomain will reduce number of queries |
| 238 | + // sent by clients) |
| 239 | + // default is AsyncDNSReplyCode::NonExistentDomain |
| 240 | + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); |
| 241 | + |
| 242 | + // start DNS server for a specific domain name |
| 243 | + dnsServer.start(DNS_PORT, "*", apIP); |
| 244 | + |
| 245 | + ... |
| 246 | +} |
| 247 | + |
| 248 | +void loop() |
| 249 | +{ |
| 250 | +} |
| 251 | + |
| 252 | +``` |
| 253 | +
|
| 254 | +--- |
| 255 | +--- |
| 256 | +
|
| 257 | +### Examples |
| 258 | +
|
| 259 | + 1. [AsyncCaptivePortalAdvanced_ESP32_ENC](examples/AsyncCaptivePortalAdvanced_ESP32_ENC) |
| 260 | + 2. [AsyncCaptivePortal_ESP32_ENC](examples/AsyncCaptivePortal_ESP32_ENC) |
| 261 | + 3. [AsyncDNServerFull_ESP32_ENC](examples/AsyncDNServerFull_ESP32_ENC) |
| 262 | + 4. [AsyncDNSServer_ESP32_ENC](examples/AsyncDNSServer_ESP32_ENC) |
| 263 | + |
| 264 | +--- |
| 265 | +--- |
| 266 | +
|
| 267 | +### Example [AsyncDNSServer_ESP32_ENC](examples/AsyncDNSServer_ESP32_ENC) |
| 268 | +
|
| 269 | +#### 1. File [AsyncDNSServer_ESP32_ENC.ino](examples/AsyncDNSServer_ESP32_ENC/AsyncDNSServer_ESP32_ENC.ino) |
| 270 | +
|
| 271 | +https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC/blob/dc4ae2473b2e323cb6bc57c8c13710554ed68948/examples/AsyncDNSServer_ESP32_ENC/AsyncDNSServer_ESP32_ENC.ino#L11-L164 |
| 272 | +
|
| 273 | +--- |
| 274 | +--- |
| 275 | +
|
| 276 | +### Debug |
| 277 | +
|
| 278 | +Debug is enabled by default on Serial. To disable, use level 0 |
| 279 | +
|
| 280 | +```cpp |
| 281 | +#define ASYNC_DNS_ESP32_ENC_DEBUG_PORT Serial |
| 282 | +
|
| 283 | +// Use from 0 to 4. Higher number, more debugging messages and memory usage. |
| 284 | +#define _ASYNC_DNS_ESP32_ENC_LOGLEVEL_ 0 |
| 285 | +``` |
| 286 | + |
| 287 | +You can also change the debugging level from 0 to 4 |
| 288 | + |
| 289 | +```cpp |
| 290 | +#define ASYNC_DNS_ESP32_ENC_DEBUG_PORT Serial |
| 291 | + |
| 292 | + |
| 293 | +// Use from 0 to 4. Higher number, more debugging messages and memory usage. |
| 294 | +#define _ASYNC_DNS_ESP32_ENC_LOGLEVEL_ 4 |
| 295 | +``` |
| 296 | + |
| 297 | +--- |
| 298 | + |
| 299 | +### Troubleshooting |
| 300 | + |
| 301 | +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. |
| 302 | + |
| 303 | +Sometimes, the library will only work if you update the `ESP32` core to the latest version because I am always using the latest cores /libraries. |
| 304 | + |
| 305 | + |
| 306 | +--- |
| 307 | +--- |
| 308 | + |
| 309 | + |
| 310 | +### Issues |
| 311 | + |
| 312 | +Submit issues to: [AsyncDNSServer_ESP32_ENC issues](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC/issues) |
| 313 | + |
| 314 | +--- |
| 315 | + |
| 316 | + |
| 317 | +## TO DO |
| 318 | + |
| 319 | + 1. Fix bug. Add enhancement |
| 320 | + |
| 321 | + |
| 322 | +## DONE |
| 323 | + |
| 324 | + 1. Add support to **ESP32 boards using LwIP ENC28J60 Ethernet** |
| 325 | + 2. Add more examples. |
| 326 | + 3. Add debugging features |
| 327 | + 4. Add astyle using `allman` style. Restyle the library |
| 328 | + |
| 329 | +--- |
| 330 | +--- |
| 331 | + |
| 332 | +### Contributions and Thanks |
| 333 | + |
| 334 | +1. Based on and modified from [Develo's ESPAsyncDNSServer Library](https://github.com/devyte/ESPAsyncDNSServer). |
| 335 | + |
| 336 | + |
| 337 | +<table> |
| 338 | + <tr> |
| 339 | + <td align="center"><a href="https://github.com/devyte"><img src="https://github.com/devyte.png" width="100px;" alt="devyte"/><br/><sub><b>⭐️ Develo</b></sub></a><br/></td> |
| 340 | + </tr> |
| 341 | +</table> |
| 342 | + |
| 343 | +--- |
| 344 | + |
| 345 | +### Contributing |
| 346 | + |
| 347 | +If you want to contribute to this project: |
| 348 | +- Report bugs and errors |
| 349 | +- Ask for enhancements |
| 350 | +- Create issues and pull requests |
| 351 | +- Tell other people about this library |
| 352 | + |
| 353 | +--- |
| 354 | +--- |
| 355 | + |
| 356 | +### License |
| 357 | + |
| 358 | +- The library is licensed under [GPLv3](https://github.com/khoih-prog/AsyncDNSServer_ESP32_ENC/blob/main/LICENSE) |
| 359 | + |
| 360 | +--- |
| 361 | + |
| 362 | +## Copyright |
| 363 | + |
| 364 | +- Copyright (c) 2016- Develo |
| 365 | +- Copyright (c) 2022- Khoi Hoang |
| 366 | + |
| 367 | + |
0 commit comments