Skip to content

Commit 686b977

Browse files
committed
fix: more filters to their own example
1 parent e92f4e6 commit 686b977

File tree

3 files changed

+105
-15
lines changed

3 files changed

+105
-15
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"targets": {
3+
"esp32h2": false
4+
}
5+
}

libraries/WebServer/src/WebServer.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
*/
2222

2323
#include <Arduino.h>
24-
25-
#if SOC_WIFI_SUPPORTED
26-
#include "WiFi.h"
27-
#endif
28-
2924
#include <esp32-hal-log.h>
3025
#include <libb64/cdecode.h>
3126
#include <libb64/cencode.h>
@@ -803,13 +798,3 @@ String WebServer::_responseCodeToString(int code) {
803798
default: return F("");
804799
}
805800
}
806-
807-
#if SOC_WIFI_SUPPORTED
808-
bool ON_STA_FILTER(WebServer &server) {
809-
return WiFi.STA.hasIP() && WiFi.STA.localIP() == server.client().localIP();
810-
}
811-
812-
bool ON_AP_FILTER(WebServer &server) {
813-
return WiFi.AP.hasIP() && WiFi.AP.localIP() == server.client().localIP();
814-
}
815-
#endif

0 commit comments

Comments
 (0)