|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncCaptivePortal.ino |
| 3 | +
|
| 4 | + AsyncDNSServer_RP2040W is an Async DNS_Server library for the RP2040W with CYW43439 WiFi |
| 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_RP2040W |
| 8 | + *****************************************************************************************************************************/ |
| 9 | + |
| 10 | +#include "defines.h" |
| 11 | + |
| 12 | +#include <AsyncDNSServer_RP2040W.h> |
| 13 | +#include <AsyncWebServer_RP2040W.h> |
| 14 | + |
| 15 | +const byte DNS_PORT = 53; |
| 16 | +IPAddress apIP(192, 168, 100, 1); |
| 17 | + |
| 18 | +AsyncDNSServer dnsServer; |
| 19 | +AsyncWebServer server(80); |
| 20 | + |
| 21 | +int status = WL_IDLE_STATUS; |
| 22 | + |
| 23 | +String responseHTML = "" |
| 24 | + "<!DOCTYPE html><html lang='en'><head>" |
| 25 | + "<meta name='viewport' content='width=device-width'>" |
| 26 | + "<title>RP2040W-CaptivePortal</title></head><body>" |
| 27 | + "<h1>Hello World from RP2040W!</h1><p>This is a captive portal example." |
| 28 | + " All requests will be redirected here.</p></body></html>"; |
| 29 | + |
| 30 | +void handleNotFound(AsyncWebServerRequest *request) |
| 31 | +{ |
| 32 | + request->send(200, "text/html", responseHTML); |
| 33 | +} |
| 34 | + |
| 35 | +void printWifiStatus() |
| 36 | +{ |
| 37 | + // print the SSID of the network you're attached to: |
| 38 | + Serial.print("SSID: "); |
| 39 | + Serial.println(WiFi.SSID()); |
| 40 | + |
| 41 | + // print your board's IP address: |
| 42 | + IPAddress ip = WiFi.localIP(); |
| 43 | + Serial.print("Local IP Address: "); |
| 44 | + Serial.println(ip); |
| 45 | + |
| 46 | + // print the received signal strength: |
| 47 | + long rssi = WiFi.RSSI(); |
| 48 | + Serial.print("signal strength (RSSI):"); |
| 49 | + Serial.print(rssi); |
| 50 | + Serial.println(" dBm"); |
| 51 | +} |
| 52 | + |
| 53 | +void setup() |
| 54 | +{ |
| 55 | + Serial.begin(115200); |
| 56 | + while (!Serial && millis() < 5000); |
| 57 | + |
| 58 | + Serial.print("\nStart AsyncCaptivePortal on "); Serial.println(BOARD_NAME); |
| 59 | + Serial.println(ASYNC_DNS_SERVER_RP2040W_VERSION); |
| 60 | + |
| 61 | +#if defined(ASYNC_DNS_SERVER_RP2040W_VERSION_MIN) |
| 62 | + if (ASYNC_DNS_SERVER_RP2040W_VERSION_INT < ASYNC_DNS_SERVER_RP2040W_VERSION_MIN) |
| 63 | + { |
| 64 | + Serial.print("Warning. Must use this example on Version equal or later than : "); |
| 65 | + Serial.println(ASYNC_DNS_SERVER_RP2040W_VERSION_MIN_TARGET); |
| 66 | + } |
| 67 | +#endif |
| 68 | + |
| 69 | + /////////////////////////////////// |
| 70 | + |
| 71 | + // check for the WiFi module: |
| 72 | + if (WiFi.status() == WL_NO_MODULE) |
| 73 | + { |
| 74 | + Serial.println("Communication with WiFi module failed!"); |
| 75 | + // don't continue |
| 76 | + while (true); |
| 77 | + } |
| 78 | + |
| 79 | + Serial.print(F("Connecting to SSID: ")); |
| 80 | + Serial.println(ssid); |
| 81 | + |
| 82 | + status = WiFi.begin(ssid, pass); |
| 83 | + |
| 84 | + delay(1000); |
| 85 | + |
| 86 | + // attempt to connect to WiFi network |
| 87 | + while ( status != WL_CONNECTED) |
| 88 | + { |
| 89 | + delay(500); |
| 90 | + |
| 91 | + // Connect to WPA/WPA2 network |
| 92 | + status = WiFi.status(); |
| 93 | + } |
| 94 | + |
| 95 | + printWifiStatus(); |
| 96 | + |
| 97 | + /////////////////////////////////// |
| 98 | + |
| 99 | + // modify TTL associated with the domain name (in seconds) |
| 100 | + // default is 60 seconds |
| 101 | + dnsServer.setTTL(300); |
| 102 | + // set which return code will be used for all other domains (e.g. sending |
| 103 | + // ServerFailure instead of NonExistentDomain will reduce number of queries |
| 104 | + // sent by clients) |
| 105 | + // default is AsyncDNSReplyCode::NonExistentDomain |
| 106 | + dnsServer.setErrorReplyCode(AsyncDNSReplyCode::ServerFailure); |
| 107 | + |
| 108 | + // if DNSServer is started with "*" for domain name, it will reply with |
| 109 | + // provided IP to all DNS request |
| 110 | + dnsServer.start(DNS_PORT, "*", apIP); |
| 111 | + |
| 112 | + server.onNotFound(handleNotFound); |
| 113 | + |
| 114 | + server.begin(); |
| 115 | + |
| 116 | + Serial.print(F("HTTP DNSServer is @ IP : ")); |
| 117 | + Serial.println(apIP); |
| 118 | +} |
| 119 | + |
| 120 | +void loop() |
| 121 | +{ |
| 122 | +} |
0 commit comments