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-07-02 20:06:33 +02:00
|
|
|
|
#include <AsyncTCP.h>
|
|
|
|
|
#include <WiFi.h>
|
2024-02-06 17:39:53 +01:00
|
|
|
|
#elif defined(ESP8266)
|
2024-07-02 20:06:33 +02:00
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
|
#elif defined(TARGET_RP2040)
|
|
|
|
|
#include <WebServer.h>
|
|
|
|
|
#include <WiFi.h>
|
2024-02-06 17:39:53 +01:00
|
|
|
|
#endif
|
2024-07-30 13:52:59 +02:00
|
|
|
|
|
2024-02-06 17:39:53 +01:00
|
|
|
|
#include <ESPAsyncWebServer.h>
|
2024-01-27 09:56:20 +01:00
|
|
|
|
|
2024-07-30 13:52:59 +02:00
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
#include <AsyncJson.h>
|
|
|
|
|
#include <AsyncMessagePack.h>
|
2024-09-02 06:59:11 +02:00
|
|
|
|
#include <LittleFS.h>
|
2024-01-27 09:56:20 +01:00
|
|
|
|
|
2024-07-30 13:52:59 +02:00
|
|
|
|
AsyncWebServer server(80);
|
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-07-02 20:06:33 +02:00
|
|
|
|
void notFound(AsyncWebServerRequest* request) {
|
|
|
|
|
request->send(404, "text/plain", "Not found");
|
2024-02-06 17:39:53 +01:00
|
|
|
|
}
|
2024-01-27 09:56:20 +01:00
|
|
|
|
|
2024-07-30 13:52:59 +02:00
|
|
|
|
AsyncCallbackJsonWebHandler* jsonHandler = new AsyncCallbackJsonWebHandler("/json2");
|
|
|
|
|
AsyncCallbackMessagePackWebHandler* msgPackHandler = new AsyncCallbackMessagePackWebHandler("/msgpack2");
|
|
|
|
|
|
2024-01-27 09:56:20 +01:00
|
|
|
|
void setup() {
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
Serial.begin(115200);
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-09-02 06:59:11 +02:00
|
|
|
|
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
2024-07-30 13:52:59 +02:00
|
|
|
|
// WiFi.mode(WIFI_STA);
|
|
|
|
|
// WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
|
|
|
|
|
// if (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
|
|
|
|
// Serial.printf("WiFi Failed!\n");
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
// Serial.print("IP Address: ");
|
|
|
|
|
// Serial.println(WiFi.localIP());
|
|
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
|
WiFi.softAP("esp-captive");
|
2024-09-02 06:59:11 +02:00
|
|
|
|
#endif
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
|
request->send(200, "text/plain", "Hello, world");
|
|
|
|
|
});
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-09-02 06:59:11 +02:00
|
|
|
|
server.on("/file", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
|
request->send(LittleFS, "/index.html");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
❯ curl -I -X HEAD http://192.168.4.1/download
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
|
Content-Length: 1024
|
|
|
|
|
Content-Type: application/octet-stream
|
|
|
|
|
Connection: close
|
|
|
|
|
Accept-Ranges: bytes
|
|
|
|
|
*/
|
|
|
|
|
// Ref: https://github.com/mathieucarbou/ESPAsyncWebServer/pull/80
|
|
|
|
|
server.on("/download", HTTP_HEAD | HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
|
if (request->method() == HTTP_HEAD) {
|
|
|
|
|
AsyncWebServerResponse* response = request->beginResponse(200, "application/octet-stream");
|
|
|
|
|
response->setContentLength(1024); // myFile.getSize()
|
|
|
|
|
response->addHeader("Accept-Ranges", "bytes");
|
|
|
|
|
// ...
|
|
|
|
|
request->send(response);
|
|
|
|
|
} else {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
// 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);
|
|
|
|
|
});
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
// 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);
|
|
|
|
|
});
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-07-30 13:52:59 +02:00
|
|
|
|
// JSON
|
|
|
|
|
|
|
|
|
|
// receives JSON and sends JSON
|
|
|
|
|
jsonHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
|
|
|
|
JsonObject jsonObj = json.as<JsonObject>();
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse();
|
|
|
|
|
JsonObject root = response->getRoot().to<JsonObject>();
|
|
|
|
|
root["hello"] = "world";
|
|
|
|
|
response->setLength();
|
|
|
|
|
request->send(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// sends JSON
|
|
|
|
|
server.on("/json1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse();
|
|
|
|
|
JsonObject root = response->getRoot().to<JsonObject>();
|
|
|
|
|
root["hello"] = "world";
|
|
|
|
|
response->setLength();
|
|
|
|
|
request->send(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// MessagePack
|
|
|
|
|
|
|
|
|
|
// receives MessagePack and sends MessagePack
|
|
|
|
|
msgPackHandler->onRequest([](AsyncWebServerRequest* request, JsonVariant& json) {
|
|
|
|
|
JsonObject jsonObj = json.as<JsonObject>();
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
|
AsyncMessagePackResponse* response = new AsyncMessagePackResponse();
|
|
|
|
|
JsonObject root = response->getRoot().to<JsonObject>();
|
|
|
|
|
root["hello"] = "world";
|
|
|
|
|
response->setLength();
|
|
|
|
|
request->send(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// sends MessagePack
|
|
|
|
|
server.on("/msgpack1", HTTP_GET, [](AsyncWebServerRequest* request) {
|
|
|
|
|
AsyncMessagePackResponse* response = new AsyncMessagePackResponse();
|
|
|
|
|
JsonObject root = response->getRoot().to<JsonObject>();
|
|
|
|
|
root["hello"] = "world";
|
|
|
|
|
response->setLength();
|
|
|
|
|
request->send(response);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.addHandler(jsonHandler);
|
|
|
|
|
server.addHandler(msgPackHandler);
|
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
server.onNotFound(notFound);
|
2024-02-06 17:39:53 +01:00
|
|
|
|
|
2024-07-02 20:06:33 +02:00
|
|
|
|
server.begin();
|
2024-01-27 09:56:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
}
|