Skip to content

Instantly share code, notes, and snippets.

@wirwolf
Last active November 13, 2023 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wirwolf/19d06178d8dd3779f62b9699b17222bb to your computer and use it in GitHub Desktop.
Save wirwolf/19d06178d8dd3779f62b9699b17222bb to your computer and use it in GitHub Desktop.
Arduino AsyncWebServer examples
server.on("/test", HTTP_GET, [](AsyncWebServerRequest *request){
ArduinoJson::StaticJsonDocument<200> doc;
// Add values in the document
//
doc["sensor"] = "gps";
doc["time"] = 1351824120;
// Add an array.
//
JsonArray data = doc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
String responceBody;
ArduinoJson::serializeJson(doc, responceBody);
// request->send(SPIFFS, "/index.html", String(), false, processor);
request->send(200, "application/json", responceBody);
});
void onNotFound(AsyncWebServerRequest *request) {
Serial.printf("NOT_FOUND: ");
if(request->method() == HTTP_GET)
Serial.printf("GET");
else if(request->method() == HTTP_POST)
Serial.printf("POST");
else if(request->method() == HTTP_DELETE)
Serial.printf("DELETE");
else if(request->method() == HTTP_PUT)
Serial.printf("PUT");
else if(request->method() == HTTP_PATCH)
Serial.printf("PATCH");
else if(request->method() == HTTP_HEAD)
Serial.printf("HEAD");
else if(request->method() == HTTP_OPTIONS)
Serial.printf("OPTIONS");
else
Serial.printf("UNKNOWN");
Serial.printf(" http://%s%s\n", request->host().c_str(), request->url().c_str());
if(request->contentLength()) {
Serial.printf("_CONTENT_TYPE: %s\n", request->contentType().c_str());
Serial.printf("_CONTENT_LENGTH: %u\n", request->contentLength());
}
int headers = request->headers();
int i;
for(i=0; i<headers; i++) {
AsyncWebHeader* h = request->getHeader(i);
Serial.printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
}
int params = request->params();
for(i=0; i<params; i++) {
AsyncWebParameter* p = request->getParam(i);
if(p->isFile()) {
Serial.printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
} else if(p->isPost()) {
Serial.printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
} else {
Serial.printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
request->send(404, "text/plain", "Page not found");
}
server.on("/planEditorHandler", HTTP_POST,
[](AsyncWebServerRequest * request) {},
NULL,
[](AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) {
String jsondata;
for (size_t i = 0; i < len; i++) {
jsondata+=(char)data[i];
}
Serial.printf("size: %d, total: %d\n", len, total);
Serial.println(jsondata);
request->send(200, "application/json", jsondata);
}
);
void web_init() {
server.addHandler(&events); // attach AsyncEventSource
server.serveStatic("/", SPIFFS, "/www").setDefaultFile("index.html");
server.onNotFound(onNotFound);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment