|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncCaptivePortalAdvanced_WT32_ETH01.ino |
| 3 | +
|
| 4 | + AsyncDNSServer_WT32_ETH01 is a Async DNS Server library for WT32_ETH01 (ESP32 + LAN8720) |
| 5 | +
|
| 6 | + Based on and modified from ESPAsyncDNSServer Library (https://github.com/devyte/ESPAsyncDNSServer) |
| 7 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncDNSServer_WT32_ETH01 |
| 8 | + Licensed under GPLv3 license |
| 9 | + *****************************************************************************************************************************/ |
| 10 | + |
| 11 | +/* |
| 12 | + This example serves a "hello world". |
| 13 | +
|
| 14 | + Now the WT32_ETH01 is in your network. You can reach it through http://192.168.x.x/ |
| 15 | +
|
| 16 | + This is a captive portal because it will redirect any http request to http://192.168.x.x/ |
| 17 | +*/ |
| 18 | + |
| 19 | +#define ASYNC_DNS_WT32_ETH01_DEBUG_PORT Serial |
| 20 | + |
| 21 | +// Use from 0 to 4. Higher number, more debugging messages and memory usage. |
| 22 | +#define _ASYNC_DNS_WT32_ETH01_LOGLEVEL_ 4 |
| 23 | + |
| 24 | +///////////////////////////////////////////// |
| 25 | + |
| 26 | +// Select the IP address according to your local network |
| 27 | +IPAddress myIP(192, 168, 2, 232); |
| 28 | +IPAddress myGW(192, 168, 2, 1); |
| 29 | +IPAddress mySN(255, 255, 255, 0); |
| 30 | + |
| 31 | +// Google DNS Server IP |
| 32 | +IPAddress myDNS(8, 8, 8, 8); |
| 33 | + |
| 34 | +///////////////////////////////////////////// |
| 35 | + |
| 36 | +#include <AsyncDNSServer_WT32_ETH01.h> |
| 37 | +#include <ESPAsyncWebServer.h> |
| 38 | + |
| 39 | + |
| 40 | +// DNS server |
| 41 | +const byte DNS_PORT = 53; |
| 42 | + |
| 43 | +AsyncDNSServer dnsServer; |
| 44 | + |
| 45 | +// Web server |
| 46 | +AsyncWebServer server(80); |
| 47 | + |
| 48 | +IPAddress apIP; |
| 49 | + |
| 50 | +/** Is this an IP? */ |
| 51 | +bool isIp(String str) |
| 52 | +{ |
| 53 | + for (size_t i = 0; i < str.length(); i++) |
| 54 | + { |
| 55 | + int c = str.charAt(i); |
| 56 | + |
| 57 | + if (c != '.' && (c < '0' || c > '9')) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +/** IP to String? */ |
| 67 | +String toStringIp(IPAddress ip) |
| 68 | +{ |
| 69 | + String res = ""; |
| 70 | + |
| 71 | + for (int i = 0; i < 3; i++) |
| 72 | + { |
| 73 | + res += String((ip >> (8 * i)) & 0xFF) + "."; |
| 74 | + } |
| 75 | + |
| 76 | + res += String(((ip >> 8 * 3)) & 0xFF); |
| 77 | + |
| 78 | + return res; |
| 79 | +} |
| 80 | + |
| 81 | +/** Handle root or redirect to captive portal */ |
| 82 | +void handleRoot(AsyncWebServerRequest * request) |
| 83 | +{ |
| 84 | + if (captivePortal(request)) |
| 85 | + { |
| 86 | + // If captive portal redirect instead of displaying the page. |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + String Page = F( |
| 91 | + "<!DOCTYPE html><html lang='en'><head>" |
| 92 | + "<meta name='viewport' content='width=device-width'>" |
| 93 | + "<title>WT32_ETH01-CaptivePortal</title></head><body>" |
| 94 | + "<h1>HELLO WORLD!!</h1>"); |
| 95 | + |
| 96 | + Page += "<h2>From " + String(BOARD_NAME) + " using LAN8720A</h2>"; |
| 97 | + |
| 98 | + AsyncWebServerResponse *response = request->beginResponse(200, "text/html", Page); |
| 99 | + response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); |
| 100 | + response->addHeader("Pragma", "no-cache"); |
| 101 | + response->addHeader("Expires", "-1"); |
| 102 | + |
| 103 | + request->send(response); |
| 104 | +} |
| 105 | + |
| 106 | +// Redirect to captive portal if we got a request for another domain. |
| 107 | +// Return true in that case so the page handler do not try to handle the request again. |
| 108 | +bool captivePortal(AsyncWebServerRequest * request) |
| 109 | +{ |
| 110 | + if (!isIp(request->host())) |
| 111 | + { |
| 112 | + Serial.println("Request redirected to captive portal"); |
| 113 | + |
| 114 | + // Empty content inhibits Content-length header so we have to close the socket ourselves. |
| 115 | + AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", ""); |
| 116 | + response->addHeader("Location", String("http://") + toStringIp(request->client()->localIP())); |
| 117 | + |
| 118 | + request->send(response); |
| 119 | + |
| 120 | + request->client()->stop(); // Stop is needed because we sent no content length |
| 121 | + |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + return false; |
| 126 | +} |
| 127 | + |
| 128 | +void handleNotFound(AsyncWebServerRequest * request) |
| 129 | +{ |
| 130 | + if (captivePortal(request)) |
| 131 | + { |
| 132 | + // If captive portal redirect instead of displaying the error page. |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + String message = F("File Not Found\n\n"); |
| 137 | + |
| 138 | + message += F("URI: "); |
| 139 | + message += request->url(); |
| 140 | + message += F("\nMethod: "); |
| 141 | + message += (request->method() == HTTP_GET) ? "GET" : "POST"; |
| 142 | + message += F("\nArguments: "); |
| 143 | + message += request->args(); |
| 144 | + message += F("\n"); |
| 145 | + |
| 146 | + for (uint8_t i = 0; i < request->args(); i++) |
| 147 | + { |
| 148 | + message += String(F(" ")) + request->argName(i) + F(": ") + request->arg(i) + F("\n"); |
| 149 | + } |
| 150 | + |
| 151 | + AsyncWebServerResponse *response = request->beginResponse(404, "text/plain", message); |
| 152 | + response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); |
| 153 | + response->addHeader("Pragma", "no-cache"); |
| 154 | + response->addHeader("Expires", "-1"); |
| 155 | + |
| 156 | + request->send(response); |
| 157 | +} |
| 158 | + |
| 159 | +void setup() |
| 160 | +{ |
| 161 | + Serial.begin(115200); |
| 162 | + |
| 163 | + while (!Serial && (millis() < 5000)); |
| 164 | + |
| 165 | + Serial.print("\nStarting AsyncCaptivePortalAdvanced_WT32_ETH01 on " + String(ARDUINO_BOARD)); |
| 166 | + Serial.println(" with " + String(SHIELD_TYPE)); |
| 167 | + Serial.println(WEBSERVER_WT32_ETH01_VERSION); |
| 168 | + Serial.println(ASYNC_UDP_WT32_ETH01_VERSION); |
| 169 | + Serial.println(ASYNC_DNS_SERVER_WT32_ETH01_VERSION); |
| 170 | + |
| 171 | + Serial.setDebugOutput(true); |
| 172 | + |
| 173 | + /////////////////////////////////// |
| 174 | + |
| 175 | + /// To be called before ETH.begin() |
| 176 | + WT32_ETH01_onEvent(); |
| 177 | + |
| 178 | + //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, |
| 179 | + // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); |
| 180 | + //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); |
| 181 | + ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); |
| 182 | + |
| 183 | + // Static IP, leave without this line to get IP via DHCP |
| 184 | + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); |
| 185 | + ETH.config(myIP, myGW, mySN, myDNS); |
| 186 | + |
| 187 | + WT32_ETH01_waitForConnect(); |
| 188 | + |
| 189 | + apIP = ETH.localIP(); |
| 190 | + |
| 191 | + /////////////////////////////////// |
| 192 | + |
| 193 | + // modify TTL associated with the domain name (in seconds) |
| 194 | + // default is 60 seconds |
| 195 | + dnsServer.setTTL(300); |
| 196 | + // set which return code will be used for all other domains (e.g. sending |
| 197 | + // ServerFailure instead of NonExistentDomain will reduce number of queries |
| 198 | + // sent by clients) |
| 199 | + // default is AsyncDNSReplyCode::NonExistentDomain |
| 200 | + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); |
| 201 | + |
| 202 | + dnsServer.start(DNS_PORT, "*", apIP); |
| 203 | + |
| 204 | + /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */ |
| 205 | + // simple HTTP server to see that DNS server is working |
| 206 | + server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) |
| 207 | + { |
| 208 | + handleRoot(request); |
| 209 | + }); |
| 210 | + |
| 211 | + //Android captive portal. Maybe not needed. Might be handled by notFound handler. |
| 212 | + server.on("/generate_204", HTTP_GET, [](AsyncWebServerRequest * request) |
| 213 | + { |
| 214 | + handleRoot(request); |
| 215 | + }); |
| 216 | + |
| 217 | + //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler. |
| 218 | + server.on("/fwlink", HTTP_GET, [](AsyncWebServerRequest * request) |
| 219 | + { |
| 220 | + handleRoot(request); |
| 221 | + }); |
| 222 | + |
| 223 | + server.onNotFound(handleNotFound); |
| 224 | + |
| 225 | + server.begin(); // Web server start |
| 226 | + |
| 227 | + Serial.print(F("AsyncDNSServer is @ IP : ")); |
| 228 | + Serial.println(apIP); |
| 229 | +} |
| 230 | + |
| 231 | +void loop() |
| 232 | +{ |
| 233 | +} |
0 commit comments