2024-02-06 17:39:53 +01:00
|
|
|
//
|
|
|
|
// A simple server implementation showing how to:
|
|
|
|
// * serve static messages
|
|
|
|
// * read GET and POST parameters
|
|
|
|
// * handle missing pages / 404s
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#ifdef ESP32
|
2024-01-27 09:56:20 +01:00
|
|
|
#include <WiFi.h>
|
2024-02-06 17:39:53 +01:00
|
|
|
#include <AsyncTCP.h>
|
|
|
|
#elif defined(ESP8266)
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#endif
|
|
|
|
#include <ESPAsyncWebServer.h>
|
2024-01-27 09:56:20 +01:00
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
2024-02-06 17:39:53 +01:00
|
|
|
const char* ssid = "YOUR_SSID";
|
|
|
|
const char* password = "YOUR_PASSWORD";
|
2024-01-27 09:56:20 +01:00
|
|
|
|
2024-02-06 17:39:53 +01:00
|
|
|
const char* PARAM_MESSAGE = "message";
|
2024-01-27 09:56:20 +01:00
|
|
|
|
2024-02-06 17:39:53 +01:00
|
|
|
void notFound(AsyncWebServerRequest *request) {
|
|
|
|
request->send(404, "text/plain", "Not found");
|
|
|
|
}
|
2024-01-27 09:56:20 +01:00
|
|
|
|
|
|
|
void setup() {
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
|
|
Serial.printf("WiFi Failed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.print("IP Address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
|
|
|
request->send(200, "text/plain", "Hello, world");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send a GET request to <IP>/get?message=<message>
|
|
|
|
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
|
|
|
String message;
|
|
|
|
if (request->hasParam(PARAM_MESSAGE)) {
|
|
|
|
message = request->getParam(PARAM_MESSAGE)->value();
|
|
|
|
} else {
|
|
|
|
message = "No message sent";
|
|
|
|
}
|
|
|
|
request->send(200, "text/plain", "Hello, GET: " + message);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send a POST request to <IP>/post with a form field message set to <message>
|
|
|
|
server.on("/post", HTTP_POST, [](AsyncWebServerRequest *request){
|
|
|
|
String message;
|
|
|
|
if (request->hasParam(PARAM_MESSAGE, true)) {
|
|
|
|
message = request->getParam(PARAM_MESSAGE, true)->value();
|
|
|
|
} else {
|
|
|
|
message = "No message sent";
|
|
|
|
}
|
|
|
|
request->send(200, "text/plain", "Hello, POST: " + message);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.onNotFound(notFound);
|
|
|
|
|
|
|
|
server.begin();
|
2024-01-27 09:56:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
}
|