The new v1.6.2+
has added a new and powerful feature to permit using CString
to save heap to send very large data
.
Check the marvelleous
PRs of @salasidis in Portenta_H7_AsyncWebServer library
and these new examples
If using Arduino String, to send a buffer around 30 KBytes, the used Max Heap
is around 144,124 bytes
If using CString in regular memory, with the same 30 KBytes, the used Max Heap
is around 111,828 bytes, saving around a buffer size (30 KBytes)
This is very critical in use-cases where sending very large data
is necessary, without heap-allocation-error
.
Arduino String
isvoid send(int code, const String& contentType = String(), const String& content = String());
such as
request->send(200, textPlainStr, ArduinoStr);
The required additional HEAP is about 3 times of the String size
CString
with copying while sending. Use functionvoid send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD
such as
request->send(200, textPlainStr, cStr);
The required additional HEAP is also about 2 times of the CString size because of unnecessary copies
of the CString in HEAP. Avoid this unefficient
way.
CString
without copying while sending. Use functionvoid send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD
such as
request->send(200, textPlainStr, cStr, false);
The required additional HEAP is about 1 times of the CString size. This way is the best and most efficient way to use by avoiding of unnecessary copies
of the CString in HEAP
This library is based on, modified from:
to apply the better and faster asynchronous feature of the powerful ESPAsyncWebServer Library into (ESP32 + W5500).
Arduino IDE 1.8.19+
for Arduino. ESP32 Core 2.0.6+
for ESP32-based boards. ESP32 Latest Core AsyncTCP library v1.1.1+
.The best and easiest way is to use Arduino Library Manager
. Search for AsyncWebServer_ESP32_W5500
, then select / install the latest version. You can also use this link for more detailed instructions.
AsyncWebServer_ESP32_W5500-main.zip
.AsyncWebServer_ESP32_W5500-main
directory AsyncWebServer_ESP32_W5500-main
folder to Arduino libraries' directory such as ~/Arduino/libraries/
.loop()
thread.yield()
or delay()
or any function that uses them inside the callbacksRequest
Rewrites
and apply them on the request urlHandlers
and attaches them to RequestsRequest
objectRewrites
(in the order they were added) to rewrite the url and inject query parameters,
next, it goes through all attached Handlers
(in the order they were added) trying to find one
that canHandle
the given request. If none are found, the default(catch-all) handler is attached.handleUpload
or handleBody
methods of the Handler
if they are needed (POST+File/Body)handleRequest
method of the Handler
and is ready to be responded tohandleRequest
method, to the Request
is attached a Response
object (see below) that will serve the response data back to the clientResponse
is sent, the client is closed and freed from the memoryRewrites
are used to rewrite the request url and/or inject get parameters for a specific request url path.Rewrites
are evaluated on the request in the order they have been added to the server.Rewrite
will change the request url only if the request url (excluding get parameters) is fully match
the rewrite url, and when the optional Filter
callback return true.Filter
to the Rewrite
enables to control when to apply the rewrite, decision can be based on
request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP.Rewrite
can specify a target url with optional get parameters, e.g. /to-url?with=params
Handlers
are used for executing specific actions to particular requestsHandler
instance can be attached to any request and lives together with the serverFilter
to the Handler
enables to control when to apply the handler, decision can be based on
request url, http version, request host/port/target host, get parameters or the request client's localIP or remoteIP.canHandle
method is used for handler specific control on whether the requests can be handled
and for declaring any interesting headers that the Request
should parse. Decision can be based on request
method, request url, http version, request host/port/target host and get parametersHandler
is attached to given Request
(canHandle
returned true)
that Handler
takes care to receive any file/data upload and attach a Response
once the Request
has been fully parsedHandlers
are evaluated in the order they are attached to the server. The canHandle
is called only
if the Filter
that was set to the Handler
return true.Handler
that can handle the request is selected, not further Filter
and canHandle
are called.Response
objects are used to send the response data back to the clientResponse
object lives with the Request
and is freed on end or disconnectAsyncWebServer_ESP32_W5500
contains simple template processing engine.%
symbols. Like this: %TEMPLATE_PLACEHOLDER%
.request->version(); // uint8_t: 0 = HTTP/1.0, 1 = HTTP/1.1
request->method(); // enum: HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_PUT, HTTP_PATCH, HTTP_HEAD, HTTP_OPTIONS
request->url(); // String: URL of the request (not including host, port or GET parameters)
request->host(); // String: The requested host (can be used for virtual hosting)
request->contentType(); // String: ContentType of the request (not available in Handler::canHandle)
request->contentLength(); // size_t: ContentLength of the request (not available in Handler::canHandle)
request->multipart(); // bool: True if the request has content type "multipart"
//List all collected headers
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());
}
//get specific header by name
if (request->hasHeader("MyHeader"))
{
AsyncWebHeader* h = request->getHeader("MyHeader");
Serial.printf("MyHeader: %s\n", h->value().c_str());
}
//List all collected headers (Compatibility)
int headers = request->headers();
int i;
for (i=0;i<headers;i++)
{
Serial.printf("HEADER[%s]: %s\n", request->headerName(i).c_str(), request->header(i).c_str());
}
//get specific header by name (Compatibility)
if (request->hasHeader("MyHeader"))
{
Serial.printf("MyHeader: %s\n", request->header("MyHeader").c_str());
}
//List all parameters
int params = request->params();
for (int i=0;i<params;i++)
{
AsyncWebParameter* p = request->getParam(i);
if (p->isFile())
{
//p->isPost() is also true
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());
}
}
//Check if GET parameter exists
if (request->hasParam("download"))
AsyncWebParameter* p = request->getParam("download");
//Check if POST (but not File) parameter exists
if (request->hasParam("download", true))
AsyncWebParameter* p = request->getParam("download", true);
//Check if FILE was uploaded
if (request->hasParam("download", true, true))
AsyncWebParameter* p = request->getParam("download", true, true);
//List all parameters (Compatibility)
int args = request->args();
for (int i=0;i<args;i++)
{
Serial.printf("ARG[%s]: %s\n", request->argName(i).c_str(), request->arg(i).c_str());
}
//Check if parameter exists (Compatibility)
if (request->hasArg("download"))
String arg = request->arg("download");
Endpoints which consume JSON can use a special handler to get ready to use JSON data in the request callback:
#include "AsyncJson.h"
#include "ArduinoJson.h"
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/rest/endpoint", [](AsyncWebServerRequest *request, JsonVariant &json)
{
JsonObject& jsonObj = json.as<JsonObject>();
// ...
});
server.addHandler(handler);
//to local url
request->redirect("/login");
//to external url
request->redirect("http://esp8266.com");
request->send(404); //Sends 404 File Not Found
AsyncWebServerResponse *response = request->beginResponse(404); //Sends 404 File Not Found
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
request->send(200, "text/plain", "Hello World!");
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "Hello World!");
response->addHeader("Server","AsyncWebServer");
request->send(response);
//read 12 bytes from Serial and send them as Content Type text/plain
request->send(Serial, "text/plain", 12);
//read 12 bytes from Serial and send them as Content Type text/plain
AsyncWebServerResponse *response = request->beginResponse(Serial, "text/plain", 12);
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
String processor(const String& var)
{
if (var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
//read 12 bytes from Serial and send them as Content Type text/plain
request->send(Serial, "text/plain", 12, processor);
String processor(const String& var)
{
if (var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
//read 12 bytes from Serial and send them as Content Type text/plain
AsyncWebServerResponse *response = request->beginResponse(Serial, "text/plain", 12, processor);
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
//send 128 bytes as plain text
request->send("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will not be asked for more bytes once the content length has been reached.
//Keep in mind that you can not delay or yield waiting for more data!
//Send what you currently have and you will be asked for more again
return mySource.read(buffer, maxLen);
});
//send 128 bytes as plain text
AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will not be asked for more bytes once the content length has been reached.
//Keep in mind that you can not delay or yield waiting for more data!
//Send what you currently have and you will be asked for more again
return mySource.read(buffer, maxLen);
});
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
String processor(const String& var)
{
if (var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
//send 128 bytes as plain text
request->send("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will not be asked for more bytes once the content length has been reached.
//Keep in mind that you can not delay or yield waiting for more data!
//Send what you currently have and you will be asked for more again
return mySource.read(buffer, maxLen);
}, processor);
String processor(const String& var)
{
if (var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
//send 128 bytes as plain text
AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will not be asked for more bytes once the content length has been reached.
//Keep in mind that you can not delay or yield waiting for more data!
//Send what you currently have and you will be asked for more again
return mySource.read(buffer, maxLen);
}, processor);
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
Used when content length is unknown. Works best if the client supports HTTP/1.1
AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will be asked for more data until 0 is returned
//Keep in mind that you can not delay or yield waiting for more data!
return mySource.read(buffer, maxLen);
});
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
Used when content length is unknown. Works best if the client supports HTTP/1.1
String processor(const String& var)
{
if (var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}
// ...
AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//index equals the amount of bytes that have been already sent
//You will be asked for more data until 0 is returned
//Keep in mind that you can not delay or yield waiting for more data!
return mySource.read(buffer, maxLen);
}, processor);
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
request->send(response);
AsyncResponseStream *response = request->beginResponseStream("text/html");
response->addHeader("Server","AsyncWebServer_ESP32_W5500");
response->printf("<!DOCTYPE html><html><head><title>Webpage at %s</title></head><body>", request->url().c_str());
response->print("<h2>Hello ");
response->print(request->client()->remoteIP());
response->print("</h2>");
response->print("<h3>General</h3>");
response->print("<ul>");
response->printf("<li>Version: HTTP/1.%u</li>", request->version());
response->printf("<li>Method: %s</li>", request->methodToString());
response->printf("<li>URL: %s</li>", request->url().c_str());
response->printf("<li>Host: %s</li>", request->host().c_str());
response->printf("<li>ContentType: %s</li>", request->contentType().c_str());
response->printf("<li>ContentLength: %u</li>", request->contentLength());
response->printf("<li>Multipart: %s</li>", request->multipart()?"true":"false");
response->print("</ul>");
response->print("<h3>Headers</h3>");
response->print("<ul>");
int headers = request->headers();
for (int i=0;i<headers;i++)
{
AsyncWebHeader* h = request->getHeader(i);
response->printf("<li>%s: %s</li>", h->name().c_str(), h->value().c_str());
}
response->print("</ul>");
response->print("<h3>Parameters</h3>");
response->print("<ul>");
int params = request->params();
for (int i=0;i<params;i++)
{
AsyncWebParameter* p = request->getParam(i);
if (p->isFile())
{
response->printf("<li>FILE[%s]: %s, size: %u</li>", p->name().c_str(), p->value().c_str(), p->size());
}
else if (p->isPost())
{
response->printf("<li>POST[%s]: %s</li>", p->name().c_str(), p->value().c_str());
}
else
{
response->printf("<li>GET[%s]: %s</li>", p->name().c_str(), p->value().c_str());
}
}
response->print("</ul>");
response->print("</body></html>");
//send the response last
request->send(response);
This way of sending Json is great for when the result is below 4KB
#include "AsyncJson.h"
#include "ArduinoJson.h"
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
root.printTo(*response);
request->send(response);
This response can handle really large Json objects (tested to 40KB)
There isn't any noticeable speed decrease for small results with the method above
Since ArduinoJson does not allow reading parts of the string, the whole Json has to be passed every time a chunks needs to be sent, which shows speed decrease proportional to the resulting json packets
#include "AsyncJson.h"
#include "ArduinoJson.h"
AsyncJsonResponse * response = new AsyncJsonResponse();
response->addHeader("Server","AsyncWebServer");
JsonObject& root = response->getRoot();
root["IP"] = Ethernet.localIP();
response->setLength();
request->send(response);
It is possible to rewrite the request url with parameter matchg. Here is an example with one parameter: Rewrite for example "/radio/{frequence}" -> "/radio?f={frequence}"
class OneParamRewrite : public AsyncWebRewrite
{
protected:
String _urlPrefix;
int _paramIndex;
String _paramsBackup;
public:
OneParamRewrite(const char* from, const char* to)
: AsyncWebRewrite(from, to)
{
_paramIndex = _from.indexOf('{');
if ( _paramIndex >=0 && _from.endsWith("}"))
{
_urlPrefix = _from.substring(0, _paramIndex);
int index = _params.indexOf('{');
if (index >= 0)
{
_params = _params.substring(0, index);
}
}
else
{
_urlPrefix = _from;
}
_paramsBackup = _params;
}
bool match(AsyncWebServerRequest *request) override
{
if (request->url().startsWith(_urlPrefix))
{
if (_paramIndex >= 0)
{
_params = _paramsBackup + request->url().substring(_paramIndex);
}
else
{
_params = _paramsBackup;
}
return true;
}
else
{
return false;
}
}
};
Usage:
server.addRewrite( new OneParamRewrite("/radio/{frequence}", "/radio?f={frequence}") );
Filters can be set to Rewrite
or Handler
in order to control when to apply the rewrite and consider the handler.
A filter is a callback function that evaluates the request and return a boolean true
to include the item
or false
to exclude it.
Some responses are implemented, but you should not use them, because they do not conform to HTTP. The following example will lead to unclean close of the connection and more time wasted than providing the length of the content
//This is used as fallback for chunked responses to HTTP/1.0 Clients
request->send("text/plain", 0, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t
{
//Write up to "maxLen" bytes into "buffer" and return the amount written.
//You will be asked for more data until 0 is returned
//Keep in mind that you can not delay or yield waiting for more data!
return mySource.read(buffer, maxLen);
});
The server includes a web socket plugin which lets you define different WebSocket locations to connect to without starting another listening service or using different port
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)
{
if (type == WS_EVT_CONNECT)
{
//client connected
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("Hello Client %u :)", client->id());
client->ping();
}
else if (type == WS_EVT_DISCONNECT)
{
//client disconnected
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
}
else if (type == WS_EVT_ERROR)
{
//error was received from the other end
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
}
else if (type == WS_EVT_PONG)
{
//pong message was received (in response to a ping request maybe)
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
}
else if (type == WS_EVT_DATA)
{
//data packet
AwsFrameInfo * info = (AwsFrameInfo*)arg;
if (info->final && info->index == 0 && info->len == len)
{
//the whole message is in a single frame and we got all of it's data
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
if (info->opcode == WS_TEXT)
{
data[len] = 0;
Serial.printf("%s\n", (char*)data);
}
else
{
for (size_t i=0; i < info->len; i++)
{
Serial.printf("%02x ", data[i]);
}
Serial.printf("\n");
}
if (info->opcode == WS_TEXT)
client->text("I got your text message");
else
client->binary("I got your binary message");
}
else
{
//message is comprised of multiple frames or the frame is split into multiple packets
if (info->index == 0)
{
if (info->num == 0)
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
}
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT)?"text":"binary", info->index, info->index + len);
if (info->message_opcode == WS_TEXT)
{
data[len] = 0;
Serial.printf("%s\n", (char*)data);
}
else
{
for (size_t i=0; i < len; i++){
Serial.printf("%02x ", data[i]);
}
Serial.printf("\n");
}
if ((info->index + len) == info->len)
{
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
if (info->final)
{
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT)?"text":"binary");
if (info->message_opcode == WS_TEXT)
client->text("I got your text message");
else
client->binary("I got your binary message");
}
}
}
}
}
//Server methods
AsyncWebSocket ws("/ws");
//printf to a client
ws.printf((uint32_t)client_id, arguments...);
//printf to all clients
ws.printfAll(arguments...);
//send text to a client
ws.text((uint32_t)client_id, (char*)text);
ws.text((uint32_t)client_id, (uint8_t*)text, (size_t)len);
//send text to all clients
ws.textAll((char*)text);
ws.textAll((uint8_t*)text, (size_t)len);
//send binary to a client
ws.binary((uint32_t)client_id, (char*)binary);
ws.binary((uint32_t)client_id, (uint8_t*)binary, (size_t)len);
ws.binary((uint32_t)client_id, flash_binary, 4);
//send binary to all clients
ws.binaryAll((char*)binary);
ws.binaryAll((uint8_t*)binary, (size_t)len);
//HTTP Authenticate before switch to Websocket protocol
ws.setAuthentication("user", "pass");
//client methods
AsyncWebSocketClient * client;
//printf
client->printf(arguments...);
//send text
client->text((char*)text);
client->text((uint8_t*)text, (size_t)len);
//send binary
client->binary((char*)binary);
client->binary((uint8_t*)binary, (size_t)len);
When sending a web socket message using the above methods a buffer is created. Under certain circumstances you might want to manipulate or populate this buffer directly from your application, for example to prevent unnecessary duplications of the data. This example below shows how to create a buffer and print data to it from an ArduinoJson object then send it.
void sendDataWs(AsyncWebSocketClient * client)
{
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["a"] = "abc";
root["b"] = "abcd";
root["c"] = "abcde";
root["d"] = "abcdef";
root["e"] = "abcdefg";
size_t len = root.measureLength();
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer)
{
root.printTo((char *)buffer->get(), len + 1);
if (client)
{
client->text(buffer);
}
else
{
ws.textAll(buffer);
}
}
}
Browsers sometimes do not correctly close the websocket connection, even when the close()
function is called in javascript. This will eventually exhaust the web server's resources and will cause the server to crash. Periodically calling the cleanClients()
function from the main loop()
function limits the number of clients by closing the oldest client when the maximum number of clients has been exceeded. This can called be every cycle, however, if you wish to use less power, then calling as infrequently as once per second is sufficient.
void loop()
{
ws.cleanupClients();
}
The server includes EventSource
(Server-Sent Events) plugin which can be used to send short text events to the browser.
Difference between EventSource
and WebSockets
is that EventSource
is single direction, text-only protocol.
AsyncWebServer server(80);
AsyncEventSource events("/events");
void setup()
{
// setup ......
events.onConnect([](AsyncEventSourceClient *client)
{
if (client->lastId())
{
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
//send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!",NULL,millis(),1000);
});
//HTTP Basic authentication
events.setAuthentication("user", "pass");
server.addHandler(&events);
// setup ......
}
void loop()
{
if (eventTriggered)
{
// your logic here
//send event "myevent"
events.send("my event content","myevent",millis());
}
}
if (!!window.EventSource)
{
var source = new EventSource('/events');
source.addEventListener('open', function(e)
{
console.log("Events Connected");
}, false);
source.addEventListener('error', function(e)
{
if (e.target.readyState != EventSource.OPEN)
{
console.log("Events Disconnected");
}
}, false);
source.addEventListener('message', function(e)
{
console.log("message", e.data);
}, false);
source.addEventListener('myevent', function(e)
{
console.log("myevent", e.data);
}, false);
}
Server goes through handlers in same order as they were added. You can't simple add handler with same path to override them. To remove handler:
// save callback for particular URL path
auto handler = server.on("/some/path", [](AsyncWebServerRequest *request)
{
//do something useful
});
// when you don't need handler anymore remove it
server.removeHandler(&handler);
// same with rewrites
server.removeRewrite(&someRewrite);
server.onNotFound([](AsyncWebServerRequest *request)
{
request->send(404);
});
// remove server.onNotFound handler
server.onNotFound(NULL);
// remove all rewrites, handlers and onNotFound/onFileUpload/onRequestBody callbacks
server.reset();
#if !( defined(ESP32) )
#error This code is designed for (ESP32 + W5500) to run on ESP32 platform! Please check your Tools->Board setting.
#endif
#include <Arduino.h>
#define _ASYNC_WEBSERVER_LOGLEVEL_ 2
// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC 20
byte mac[][NUMBER_OF_MAC] =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
};
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
//////////////////////////////////////////////////////////
// Optional values to override default settings
// Don't change unless you know what you're doing
//#define ETH_SPI_HOST SPI3_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
//#define INT_GPIO 4
//#define MISO_GPIO 19
//#define MOSI_GPIO 23
//#define SCK_GPIO 18
//#define CS_GPIO 5
//////////////////////////////////////////////////////////
#include <AsyncTCP.h>
#include <AsyncWebServer_ESP32_W5500.h>
AsyncWebServer server(80);
void handleRoot(AsyncWebServerRequest *request)
{
request->send(200, "text/plain", String("Hello from Async_HelloServer on ") + ARDUINO_BOARD );
}
void handleNotFound(AsyncWebServerRequest *request)
{
String message = "File Not Found\n\n";
message += "URI: ";
//message += server.uri();
message += request->url();
message += "\nMethod: ";
message += (request->method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += request->args();
message += "\n";
for (uint8_t i = 0; i < request->args(); i++)
{
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
}
request->send(404, "text/plain", message);
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print(F("\nStart Async_HelloServer on "));
Serial.print(ARDUINO_BOARD);
Serial.print(F(" with "));
Serial.println(SHIELD_TYPE);
Serial.println(ASYNC_WEBSERVER_ESP32_W5500_VERSION);
AWS_LOGWARN(F("Default SPI pinout:"));
AWS_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
AWS_LOGWARN1(F("MOSI:"), MOSI_GPIO);
AWS_LOGWARN1(F("MISO:"), MISO_GPIO);
AWS_LOGWARN1(F("SCK:"), SCK_GPIO);
AWS_LOGWARN1(F("CS:"), CS_GPIO);
AWS_LOGWARN1(F("INT:"), INT_GPIO);
AWS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
AWS_LOGWARN(F("========================="));
///////////////////////////////////
// To be called before ETH.begin()
ESP32_W5500_onEvent();
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[index] );
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
//ETH.config(myIP, myGW, mySN, myDNS);
ESP32_W5500_waitForConnect();
///////////////////////////////////
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
handleRoot(request);
});
server.on("/inline", [](AsyncWebServerRequest * request)
{
request->send(200, "text/plain", "This works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(ETH.localIP());
}
void loop()
{
}
#include <Arduino.h>
#include <AsyncWebServer_ESP32_W5500.h>
...
void handleRequest(AsyncWebServerRequest *request){}
class WebClass
{
public :
AsyncWebServer classWebServer = AsyncWebServer(81);
WebClass(){};
void classRequest (AsyncWebServerRequest *request){}
void begin()
{
// attach global request handler
classWebServer.on("/example", HTTP_ANY, handleRequest);
// attach class request handler
classWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, this, std::placeholders::_1));
}
};
AsyncWebServer globalWebServer(80);
WebClass webClassInstance;
void setup()
{
// attach global request handler
globalWebServer.on("/example", HTTP_ANY, handleRequest);
// attach class request handler
globalWebServer.on("/example", HTTP_ANY, std::bind(&WebClass::classRequest, webClassInstance, std::placeholders::_1));
}
void loop()
{
}
// Disable client connections if it was activated
if ( ws.enabled() )
ws.enable(false);
// enable client connections if it was disabled
if ( !ws.enabled() )
ws.enable(true);
In some cases, such as when working with CORS, or with some sort of custom authentication system, you might need to define a header that should get added to all responses (including static, websocket and EventSource). The DefaultHeaders singleton allows you to do this.
Example:
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
webServer.begin();
NOTE: You will still need to respond to the OPTIONS method for CORS pre-flight in most cases. (unless you are only using GET)
This is one option:
webServer.onNotFound([](AsyncWebServerRequest *request)
{
if (request->method() == HTTP_OPTIONS)
{
request->send(200);
}
else
{
request->send(404);
}
});
With path variable you can create a custom regex rule for a specific parameter in a route.
For example we want a sensorId
parameter in a route rule to match only a integer.
server.on("^\\/sensor\\/([0-9]+)$", HTTP_GET, [] (AsyncWebServerRequest *request)
{
String sensorId = request->pathArg(0);
});
NOTE: All regex patterns starts with ^
and ends with $
To enable the Path variable
support, you have to define the buildflag -DASYNCWEBSERVER_REGEX
.
For Arduino IDE create/update platform.local.txt
:
Windows
: C:\Users(username)\AppData\Local\Arduino15\packages\{espxxxx}
\hardware\espxxxx
\{version}
\platform.local.txt
Linux
: ~/.arduino15/packages/{espxxxx}
/hardware/{espxxxx}
/{version}
/platform.local.txt
Add/Update the following line:
compiler.cpp.extra_flags=-DDASYNCWEBSERVER_REGEX
For platformio modify platformio.ini
:
[env:myboard]
build_flags =
-DASYNCWEBSERVER_REGEX
NOTE: By enabling ASYNCWEBSERVER_REGEX
, <regex>
will be included. This will add an 100k to your binary.
You can change the INT
pin to another one. Default is GPIO4
// Must connect INT to GPIOxx or not working
#define INT_GPIO 4
W5500 | <---> | ESP32 |
---|---|---|
MOSI | <---> | GPIO23 |
MISO | <---> | GPIO19 |
SCK | <---> | GPIO18 |
SS | <---> | GPIO5 |
INT | <---> | GPIO4 |
RST | <---> | RST |
GND | <---> | GND |
3.3V | <---> | 3.3V |
You can access the Async Advanced WebServer @ the server IP
Following are debug terminal output and screen shots when running example AsyncMultiWebServer_ESP32_W5500 on ESP32_DEV with LwIP W5500
, using ESP32 core v2.0.0+
, to demonstrate the operation of 3 independent AsyncWebServers on 3 different ports and how to handle the complicated AsyncMultiWebServers.
Start AsyncMultiWebServer_ESP32_W5500 on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
Connected to network. IP = 192.168.2.88
Initialize multiServer OK, serverIndex = 0, port = 8080
HTTP server started at ports 8080
Initialize multiServer OK, serverIndex = 1, port = 8081
HTTP server started at ports 8081
Initialize multiServer OK, serverIndex = 2, port = 8082
HTTP server started at ports 8082
You can access the Async Advanced WebServers @ the server IP and corresponding ports (8080, 8081 and 8082)
Following is the debug terminal and screen shot when running example Async_AdvancedWebServer_MemoryIssues_Send_CString, on ESP32_DEV with LwIP W5500
, to demonstrate the new and powerful HEAP-saving
feature
Start Async_AdvancedWebServer_MemoryIssues_Send_CString on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
HTTP EthernetWebServer is @ IP : 192.168.2.88
HEAP DATA - Pre Create Arduino String Max heap: 323016 Free heap: 222076 Used heap: 100940
.
HEAP DATA - Pre Send Max heap: 323016 Free heap: 217656 Used heap: 105360
HEAP DATA - Post Send Max heap: 323016 Free heap: 209648 Used heap: 111828
.
While using Arduino String
, the HEAP usage is very large
Start Async_AdvancedWebServer_MemoryIssues_SendArduinoString on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
HTTP EthernetWebServer is @ IP : 192.168.2.88
HEAP DATA - Pre Create Arduino String Max heap: 323288 Free heap: 262348 Used heap: 60940
.
HEAP DATA - Pre Send Max heap: 323288 Free heap: 218444 Used heap: 104844
HEAP DATA - Post Send Max heap: 323288 Free heap: 180644 Used heap: 144124
.
You can access the Async Advanced WebServers at the displayed server IP, e.g. 192.168.2.88
Following is debug terminal output when running example Async_AdvancedWebServer_SendChunked on ESP32_DEV with LwIP W5500
, using ESP32 core v2.0.0+
, to demo how to use beginChunkedResponse()
to send large html
in chunks
Start Async_AdvancedWebServer_SendChunked on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
AsyncWebServer is @ IP : 192.168.2.88
.[AWS] Total length to send in chunks = 31259
[AWS] Bytes sent in chunk = 5620
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 4300
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 4203
[AWS] Bytes sent in chunk = 0
.[AWS] Total length to send in chunks = 31279
[AWS] Bytes sent in chunk = 5620
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2787
[AWS] Bytes sent in chunk = 0
You can access the Async Advanced WebServers @ the server IP
Following is debug terminal output when running example AsyncWebServer_SendChunked on ESP32_DEV with LwIP W5500
, using ESP32 core v2.0.0+
, to demo how to use beginChunkedResponse()
to send large html
in chunks
Start AsyncWebServer_SendChunked on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
AsyncWebServer is @ IP : 192.168.2.88
.[AWS] Total length to send in chunks = 47802
[AWS] Bytes sent in chunk = 5624
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 4300
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 1428
[AWS] Bytes sent in chunk = 4300
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2864
[AWS] Bytes sent in chunk = 2122
[AWS] Bytes sent in chunk = 0
Following is debug terminal output when running example Async_WebSocketsServer on ESP32_DEV with LwIP W5500
, using ESP32 core v2.0.0+
, to demo how to use Async_WebSocketsServer
feature
Starting Async_WebSocketsServer on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
ws[Server: /ws][ClientID: 1] WSClient connected
ws[Server: /ws][ClientID: 1] WSClient disconnected
ws[Server: /ws][ClientID: 2] WSClient connected
ws[Server: /ws][ClientID: 3] WSClient connected
Following is debug terminal output when running example Async_HTTPBasicAuth on ESP32_DEV with LwIP W5500
, using ESP32 core v2.0.0+
, to demo how to use Async_Auth
feature
Start Async_HTTPBasicAuth on ESP32_DEV with ESP32_W5500
AsyncWebServer_ESP32_W5500 v1.6.4 for core v2.0.0+
[AWS] Default SPI pinout:
[AWS] SPI_HOST: 2
[AWS] MOSI: 23
[AWS] MISO: 19
[AWS] SCK: 18
[AWS] CS: 5
[AWS] INT: 4
[AWS] SPI Clock (MHz): 25
[AWS] =========================
ETH Started
ETH Connected
ETH MAC: DE:AD:BE:EF:BE:08, IPv4: 192.168.2.88
FULL_DUPLEX, 100Mbps
Async_HttpBasicAuth started @ IP : 192.168.2.88
Open http://192.168.2.88/ in your browser to see it working
Login using username = admin and password = esp32_w5500
Debug is enabled by default on Serial. Debug Level from 0 to 4. To disable, change the _ETHERNET_WEBSERVERLOGLEVEL to 0
// Use this to output debug msgs to Serial
#define DEBUG_ASYNC_WEBSERVER_PORT Serial
// Use this to disable all output debug msgs
// Debug Level from 0 to 4
#define _ASYNC_WEBSERVER_LOGLEVEL_ 0
If you get compilation errors, more often than not, you may need to install a newer version of Arduino IDE, the Arduino ESP32
core or depending libraries.
Sometimes, the library will only work if you update the ESP32
core to the latest version because I'm always using the latest cores /libraries.
Submit issues to: AsyncWebServer_ESP32_W5500 issues
LittleFS
support to use with new coresESP32
boards using LwIP W5500
Ethernet.#warning
only when DEBUG_LEVEL
is 3+AsyncWebSocket
bugCString
to save heap to send very large data
. Check request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8beginChunkedResponse()
to send large html
in chunksallman astyle
and add utils
Async_WebSocketsServer
, Async_HttpBasicAuth
and MQTT
examples![]() ⭐️⭐️ Hristo Gochkov |
If you want to contribute to this project:
Copyright (c) 2016- Hristo Gochkov
Copyright (c) 2022- Khoi Hoang