|
| 1 | +#include <WiFi.h> |
| 2 | +#include <NetworkClient.h> |
| 3 | +#include <WebServer.h> |
| 4 | +#include <ESPmDNS.h> |
| 5 | + |
| 6 | +// Your STA WiFi Credentials |
| 7 | +// ( This is the AP your ESP will connect to ) |
| 8 | +const char *ssid = "........"; |
| 9 | +const char *password = "........"; |
| 10 | + |
| 11 | +// Your AP WiFi Credentials |
| 12 | +// ( This is the AP your ESP will broadcast ) |
| 13 | +const char *ap_ssid = "ESP32_Demo"; |
| 14 | +const char *ap_password = ""; |
| 15 | + |
| 16 | +WebServer server(80); |
| 17 | + |
| 18 | +const int led = 13; |
| 19 | + |
| 20 | +// ON_STA_FILTER - Accept requests coming only from the STA interface |
| 21 | +bool ON_STA_FILTER(WebServer &server) { |
| 22 | + return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP(); |
| 23 | +} |
| 24 | + |
| 25 | +// ON_AP_FILTER - Accept requests coming only from the AP interface |
| 26 | +bool ON_AP_FILTER(WebServer &server) { |
| 27 | + return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP(); |
| 28 | +} |
| 29 | + |
| 30 | +void handleNotFound() { |
| 31 | + digitalWrite(led, 1); |
| 32 | + String message = "File Not Found\n\n"; |
| 33 | + message += "URI: "; |
| 34 | + message += server.uri(); |
| 35 | + message += "\nMethod: "; |
| 36 | + message += (server.method() == HTTP_GET) ? "GET" : "POST"; |
| 37 | + message += "\nArguments: "; |
| 38 | + message += server.args(); |
| 39 | + message += "\n"; |
| 40 | + for (uint8_t i = 0; i < server.args(); i++) { |
| 41 | + message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
| 42 | + } |
| 43 | + server.send(404, "text/plain", message); |
| 44 | + digitalWrite(led, 0); |
| 45 | +} |
| 46 | + |
| 47 | +void setup(void) { |
| 48 | + pinMode(led, OUTPUT); |
| 49 | + digitalWrite(led, 0); |
| 50 | + Serial.begin(115200); |
| 51 | + WiFi.mode(WIFI_AP_STA); |
| 52 | + // Connect to STA |
| 53 | + WiFi.begin(ssid, password); |
| 54 | + // Start AP |
| 55 | + WiFi.softAP(ap_ssid, ap_password); |
| 56 | + Serial.println(""); |
| 57 | + |
| 58 | + // Wait for connection |
| 59 | + while (WiFi.status() != WL_CONNECTED) { |
| 60 | + delay(500); |
| 61 | + Serial.print("."); |
| 62 | + } |
| 63 | + Serial.println(""); |
| 64 | + Serial.print("Connected to "); |
| 65 | + Serial.println(ssid); |
| 66 | + Serial.print("IP address: "); |
| 67 | + Serial.println(WiFi.localIP()); |
| 68 | + |
| 69 | + if (MDNS.begin("esp32")) { |
| 70 | + Serial.println("MDNS responder started"); |
| 71 | + } |
| 72 | + |
| 73 | + // This route will be accessible by STA clients only |
| 74 | + server.on("/sta", [&]() { |
| 75 | + digitalWrite(led, 1); |
| 76 | + server.send(200, "text/plain", "Hi!, This route is accessible for STA clients only"); |
| 77 | + digitalWrite(led, 0); |
| 78 | + }).setFilter(ON_STA_FILTER); |
| 79 | + |
| 80 | + // This route will be accessible by AP clients only |
| 81 | + server.on("/ap", [&]() { |
| 82 | + digitalWrite(led, 1); |
| 83 | + server.send(200, "text/plain", "Hi!, This route is accessible for AP clients only"); |
| 84 | + digitalWrite(led, 0); |
| 85 | + }).setFilter(ON_AP_FILTER); |
| 86 | + |
| 87 | + server.on("/inline", []() { |
| 88 | + server.send(200, "text/plain", "this works as well"); |
| 89 | + }); |
| 90 | + |
| 91 | + server.onNotFound(handleNotFound); |
| 92 | + |
| 93 | + server.begin(); |
| 94 | + Serial.println("HTTP server started"); |
| 95 | +} |
| 96 | + |
| 97 | +void loop(void) { |
| 98 | + server.handleClient(); |
| 99 | + delay(2); //allow the cpu to switch to other tasks |
| 100 | +} |
0 commit comments