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