ESP32/simple_web/explanation.md

40 lines
1.2 KiB
Markdown
Raw Normal View History

2023-09-29 18:37:52 +02:00
# An explanation for the code in this dir
```
AsyncWebServer server(80);
```
Start the Web Server on port 80
2023-10-31 12:30:59 +01:00
```
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
```
This will handle requests on wrong uri's with this code `server.onNotFound(notFound);`
2023-09-29 18:37:52 +02:00
```
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
```
2023-10-31 12:09:50 +01:00
Start the WIFI with `WiFi.begin(ssid, password);` and wait for connection.
2023-09-29 18:37:52 +02:00
```
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", "<a href=\"/on\">Turn the LED ON</a><a href=\"/off\">Turn the LED OFF</a>");
});
```
2023-10-31 12:09:50 +01:00
`server.on` Defines an endpoint for a request (e.g /index.html)
2023-09-29 18:37:52 +02:00
2023-10-31 12:09:50 +01:00
`request->send([..]"<button onclick=\"fetch('http://192.168.178.198/on')\">Turn LED ON</button>[..]");` on Request answer with 200 in the format text/html with this string
2023-09-29 18:37:52 +02:00
```
server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "Was there ever anything here?");
digitalWrite(LED_BUILTIN, HIGH);
});
```
This is for when the user clicks on the ON button and fetches the /on url it turns the LED on!