Giter Club home page Giter Club logo

khoih-prog / portenta_h7_asyncwebserver Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 3.0 831 KB

Asynchronous WebServer Library for STM32H7-based Portenta_H7 using mbed_portenta core. This library, which is relied on Portenta_H7_AsyncTCP, is part of a series of advanced Async libraries, such as AsyncTCP, AsyncUDP, AsyncWebSockets, AsyncHTTPRequest, AsyncHTTPSRequest, etc. Now supporting using CString in optional SDRAM to save heap to send very large data

License: GNU Lesser General Public License v3.0

C++ 57.22% Shell 1.47% C 41.31%
asynchronous webserver websockets wifi tcp-server tcp-client ethernet tcp-ip async-tcp stm32-h7

portenta_h7_asyncwebserver's Introduction

Portenta_H7_AsyncWebServer

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of contents



Important Note from v1.4.0

The new v1.4.0+ has added a new and powerful feature to permit using CString in optional SDRAM to save heap to send very large data.

Check the marvelleous PRs of @salasidis

and these new examples

  1. Async_AdvancedWebServer_MemoryIssues_Send_CString for Ethernet
  2. Async_AdvancedWebServer_MemoryIssues_SendArduinoString for Ethernet
  3. Async_AdvancedWebServer_MemoryIssues_Send_CString for WiFi

If using Arduino String, to send a buffer around 40 KBytes, the used Max Heap is around 111,500 bytes (~3 times)

If using CString in SDRAM, with the same 40 KByte, the used Max Heap is around 14,314 bytes

If using CString in regular memory, with the same 40 KByte, the used Max Heap is around 51,823 bytes (~1 times)

This is very critical in use-cases where sending very large data is necessary, without heap-allocation-error.

  1. The traditional function used to send Arduino String is

void send(int code, const String& contentType = String(), const String& content = String());

void send(int code, const String& contentType = String(), const String& content = String());

such as

request->send(200, textPlainStr, ArduinoStr);

The required HEAP is about 3 times of the String size

  1. To use CString but don't destroy it after sending. Use function

void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD

void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true);    // RSMOD

such as

request->send(200, textPlainStr, cStr);

The required HEAP is also about 3 times of the CString size because of many unnecessary copies of the CString in HEAP. Avoid this unefficient way.

  1. To use CString without destroying it after sending. Use function

void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true); // RSMOD

void send(int code, const String& contentType, const char *content, bool nonDetructiveSend = true);    // RSMOD

such as

request->send(200, textPlainStr, cStr, false);

The required HEAP is also about 1 times of the CString size without using SDRAM, or none if using SDRAM. This way is the best and most efficient way to use by avoiding of many unnecessary copies of the CString in HEAP



Features

This library is based on, modified from:

  1. Hristo Gochkov's ESPAsyncWebServer

to apply the better and faster asynchronous feature of the powerful ESPAsyncWebServer Library into Portenta_H7. Thus Portenta_H7_AsyncWebServer is part of a series of advanced Async libraries, such as AsyncTCP, AsyncUDP, AsyncWebSockets, AsyncHTTPRequest, AsyncHTTPSRequest, etc. to be written or modified to support Portenta_H7, using either Vision-shield Ethernet or Murata WiFi.

Why Async is better

  • Using asynchronous network means that you can handle more than one connection at the same time
  • You are called once the request is ready and parsed
  • When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background
  • Speed is OMG
  • Easy to use API, HTTP Basic and Digest MD5 Authentication (default), ChunkedResponse
  • Easily extensible to handle any type of content
  • Supports Continue 100
  • Async WebSocket plugin offering different locations without extra servers or ports
  • Async EventSource (Server-Sent Events) plugin to send events to the browser
  • URL Rewrite plugin for conditional and permanent url rewrites
  • ServeStatic plugin that supports cache, Last-Modified, default index and more
  • Simple template processing engine to handle templates

Currently supported Boards

  1. Portenta_H7 boards such as Portenta_H7 Rev2 ABX00042, etc., using ArduinoCore-mbed mbed_portenta core using Vision-shield Ethernet or Murata WiFi



Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. ArduinoCore-mbed mbed_portenta core 3.5.4+ for Arduino Portenta_H7 boards, such as Portenta_H7 Rev2 ABX00042, etc.. GitHub release.
  3. Portenta_H7_AsyncTCP library v1.4.0+ for Portenta_H7 using Vision-shield Ethernet or Murata WiFi. GitHub release

Installation

Use Arduino Library Manager

The best and easiest way is to use Arduino Library Manager. Search for Portenta_H7_AsyncWebServer, then select / install the latest version. You can also use this link arduino-library-badge for more detailed instructions.

Manual Install

  1. Navigate to Portenta_H7_AsyncWebServer page.
  2. Download the latest release Portenta_H7_AsyncWebServer-main.zip.
  3. Extract the zip file to Portenta_H7_AsyncWebServer-main directory
  4. Copy the whole Portenta_H7_AsyncWebServer-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO:

  1. Install VS Code
  2. Install PlatformIO
  3. Install Portenta_H7_AsyncWebServer library by using Library Manager. Search for Portenta_H7_AsyncWebServer in Platform.io Author's Libraries
  4. Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File


Packages' Patches

1. For Portenta_H7 boards using Arduino IDE in Linux

To be able to upload firmware to Portenta_H7 using Arduino IDE in Linux (Ubuntu, etc.), you have to copy the file portenta_post_install.sh into mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh).

Then run the following command using sudo

$ cd ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1
$ chmod 755 portenta_post_install.sh
$ sudo ./portenta_post_install.sh

This will create the file /etc/udev/rules.d/49-portenta_h7.rules as follows:

# Portenta H7 bootloader mode UDEV rules

SUBSYSTEMS=="usb", ATTRS{idVendor}=="2341", ATTRS{idProduct}=="035b", GROUP="plugdev", MODE="0666"

Supposing the ArduinoCore-mbed core version is 3.4.1. Now only one file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/3.4.1/portenta_post_install.sh

Whenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz

This file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/x.yy.zz/portenta_post_install.sh

2. To fix compile error relating to dns_gethostbyname and LwIP stack

Notes: Only for cores v2.5.2-. This fix is not necessary for core v3.4.1+

To be able to compile, run on Portenta_H7 boards, you have to copy the whole mbed_portenta Packages_Patches directory into Arduino mbed_portenta directory (~/.arduino15/packages/arduino/hardware/mbed_portenta/2.5.2).

Supposing the Arduino mbed_portenta version is 2.5.2. These file must be copied into the directory:

  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/2.5.2/libraries/SocketWrapper/src/MbedUdp.h
  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/2.5.2/libraries/SocketWrapper/src/MbedUdp.cpp
  • ~/.arduino15/packages/arduino/hardware/mbed_portenta/2.5.2/cores/arduino/mbed/connectivity/lwipstack/include/lwipstack/lwipopts.h


Important things to remember

  • This is fully asynchronous server and as such does not run on the loop thread.
  • You can not use yield() or delay() or any function that uses them inside the callbacks
  • The server is smart enough to know when to close the connection and free resources
  • You can not send more than one response to a single request

Principles of operation

The Async Web server

  • Listens for connections
  • Wraps the new clients into Request
  • Keeps track of clients and cleans memory
  • Manages Rewrites and apply them on the request url
  • Manages Handlers and attaches them to Requests

Request Life Cycle

  • TCP connection is received by the server
  • The connection is wrapped inside Request object
  • When the request head is received (type, url, get params, http version and host), the server goes through all Rewrites (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.
  • The rest of the request is received, calling the handleUpload or handleBody methods of the Handler if they are needed (POST+File/Body)
  • When the whole request is parsed, the result is given to the handleRequest method of the Handler and is ready to be responded to
  • In the handleRequest method, to the Request is attached a Response object (see below) that will serve the response data back to the client
  • When the Response is sent, the client is closed and freed from the memory

Rewrites and how do they work

  • The Rewrites are used to rewrite the request url and/or inject get parameters for a specific request url path.
  • All Rewrites are evaluated on the request in the order they have been added to the server.
  • The 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.
  • Setting a 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.
  • The Rewrite can specify a target url with optional get parameters, e.g. /to-url?with=params

Handlers and how do they work

  • The Handlers are used for executing specific actions to particular requests
  • One Handler instance can be attached to any request and lives together with the server
  • Setting a Filter 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.
  • The 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 parameters
  • Once a Handler 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 parsed
  • Handlers 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.
  • The first Handler that can handle the request is selected, not further Filter and canHandle are called.

Responses and how do they work

  • The Response objects are used to send the response data back to the client
  • The Response object lives with the Request and is freed on end or disconnect
  • Different techniques are used depending on the response type to send the data in packets returning back almost immediately and sending the next packet when this one is received. Any time in between is spent to run the user loop and handle other network packets
  • Responding asynchronously is probably the most difficult thing for most to understand
  • Many different options exist for the user to make responding a background task

Template processing

  • Portenta_H7_AsyncWebServer contains simple template processing engine.
  • Template processing can be added to most response types.
  • Currently it supports only replacing template placeholders with actual values. No conditional processing, cycles, etc.
  • Placeholders are delimited with % symbols. Like this: %TEMPLATE_PLACEHOLDER%.
  • It works by extracting placeholder name from response text and passing it to user provided function which should return actual value to be used instead of placeholder.
  • Since it's user provided function, it is possible for library users to implement conditional processing and cycles themselves.
  • Since it's impossible to know the actual response size after template processing step in advance (and, therefore, to include it in response headers), the response becomes chunked.

Request Variables

Common Variables

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"

Headers

//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());
}

GET, POST and FILE parameters

//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");

JSON body handling with ArduinoJson

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);

Responses

Redirect to another URL

//to local url
request->redirect("/login");

//to external url
request->redirect("http://esp8266.com");

Basic response with HTTP Code

request->send(404); //Sends 404 File Not Found

Basic response with HTTP Code and extra headers

AsyncWebServerResponse *response = request->beginResponse(404); //Sends 404 File Not Found
response->addHeader("Server","Portenta_H7_AsyncWebServer");
request->send(response);

Basic response with string content

request->send(200, "text/plain", "Hello World!");

Basic response with string content and extra headers

AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "Hello World!");
response->addHeader("Server","AsyncWebServer");
request->send(response);

Respond with content coming from a Stream

//read 12 bytes from Serial and send them as Content Type text/plain
request->send(Serial, "text/plain", 12);

Respond with content coming from a Stream and extra headers

//read 12 bytes from Serial and send them as Content Type text/plain
AsyncWebServerResponse *response = request->beginResponse(Serial, "text/plain", 12);
response->addHeader("Server","Portenta_H7_AsyncWebServer");
request->send(response);

Respond with content coming from a Stream containing templates

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);

Respond with content coming from a Stream containing templates and extra headers

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","Portenta_H7_AsyncWebServer");
request->send(response);

Respond with content using a callback

//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);
});

Respond with content using a callback and extra headers

//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","Portenta_H7_AsyncWebServer");
request->send(response);

Respond with content using a callback containing templates

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);

Respond with content using a callback containing templates and extra headers

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","Portenta_H7_AsyncWebServer");
request->send(response);

Chunked 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","Portenta_H7_AsyncWebServer");
request->send(response);

Chunked Response containing templates

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","Portenta_H7_AsyncWebServer");
request->send(response);

Print to response

AsyncResponseStream *response = request->beginResponseStream("text/html");
response->addHeader("Server","Portenta_H7_AsyncWebServer");
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);

ArduinoJson Basic 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);

ArduinoJson Advanced 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);

Param Rewrite With Matching

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}") );

Using filters

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.


Bad Responses

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

Respond with content using a callback without content length to HTTP/1.0 clients

//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);
});

Async WebSocket Plugin

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

Async WebSocket Event

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");
        }
      }
    }
  }
}

Methods for sending data to a socket client

//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);

Direct access to web socket message buffer

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);
        }
    }
}

Limiting the number of web socket clients

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();
}

Async Event Source Plugin

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.

Setup Event Source on the server

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());
  }
}

Setup Event Source in the browser

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);
}

Remove handlers and rewrites

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();

Setting up the server

#define USE_ETHERNET_PORTENTA_H7        true

#include <Portenta_Ethernet.h>
#include <Ethernet.h>
#warning Using Portenta_Ethernet lib for Portenta_H7.

#include <Portenta_H7_AsyncWebServer.h>

// 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, 0x32, 0x01 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
};

// Select the IP address according to your local network
IPAddress ip(192, 168, 2, 232);

AsyncWebServer    server(80);

#define LED_OFF             HIGH
#define LED_ON              LOW

#define BUFFER_SIZE         64
char temp[BUFFER_SIZE];

void handleRoot(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);

  snprintf(temp, BUFFER_SIZE - 1, "Hello from Async_HelloServer on %s\n", BOARD_NAME);
  request->send(200, "text/plain", temp);
  
  digitalWrite(LED_BUILTIN, LED_OFF);
}

void handleNotFound(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);
  
  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);
  digitalWrite(LED_BUILTIN, LED_OFF);
}

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LED_OFF);

  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print("\nStart Async_HelloServer on "); Serial.print(BOARD_NAME);
  Serial.print(" with "); Serial.println(SHIELD_TYPE);
  Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
  Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);

  ///////////////////////////////////
  
  // start the ethernet connection and the server
  // Use random mac
  uint16_t index = millis() % NUMBER_OF_MAC;

  // Use Static IP
  //Ethernet.begin(mac[index], ip);
  // Use DHCP dynamic IP and random mac
  Ethernet.begin(mac[index]);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) 
  {
    Serial.println("No Ethernet found. Stay here forever");
    
    while (true) 
    {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  
  if (Ethernet.linkStatus() == LinkOFF) 
  {
    Serial.println("Not connected Ethernet cable");
  }

  Serial.print(F("Using mac index = "));
  Serial.println(index);

  Serial.print(F("Connected! IP address: "));
  Serial.println(Ethernet.localIP());

  ///////////////////////////////////

  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(Ethernet.localIP());
}

void loop(void)
{
}

Setup global and class functions as request handlers

#include <Arduino.h>

#define USE_ETHERNET_PORTENTA_H7        true

#include <Portenta_Ethernet.h>
#include <Ethernet.h>
#warning Using Portenta_Ethernet lib for Portenta_H7.

#include <Portenta_H7_AsyncWebServer.h>

#include <functional>

...

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() 
{

}

Methods for controlling websocket connections

  // 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);

Adding Default Headers

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);
  }
});

Path variable

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.



Examples

1. For Vision-shield Ethernet

  1. Async_AdvancedWebServer
  2. Async_HelloServer
  3. Async_HelloServer2
  4. Async_HttpBasicAuth
  5. Async_PostServer
  6. MQTTClient_Auth
  7. MQTTClient_Basic
  8. MQTT_ThingStream
  9. WebClient
  10. WebClientRepeating
  11. Async_AdvancedWebServer_MemoryIssues_SendArduinoString New
  12. Async_AdvancedWebServer_MemoryIssues_Send_CString New
  13. Async_AdvancedWebServer_SendChunked New
  14. AsyncWebServer_SendChunked New

2. For Murata WiFi

  1. Async_AdvancedWebServer
  2. Async_HelloServer
  3. Async_HelloServer2
  4. Async_HttpBasicAuth
  5. Async_PostServer
  6. MQTTClient_Auth
  7. MQTTClient_Basic
  8. MQTT_ThingStream
  9. WebClient
  10. WebClientRepeating
  11. Async_AdvancedWebServer_MemoryIssues_Send_CString New
  12. Async_AdvancedWebServer_SendChunked New
  13. AsyncWebServer_SendChunked New


#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )
#error For Portenta_H7 only
#endif
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
#define USE_WIFI_PORTENTA_H7 true
#include <WiFi.h>
#warning Using WiFi for Portenta_H7.
#include <Portenta_H7_AsyncWebServer.h>
char ssid[] = "your_ssid"; // your network SSID (name)
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+
int status = WL_IDLE_STATUS;
AsyncWebServer server(80);
int reqCount = 0; // number of requests received
#define LED_OFF HIGH
#define LED_ON LOW
#define BUFFER_SIZE 512
char temp[BUFFER_SIZE];
void handleRoot(AsyncWebServerRequest *request)
{
digitalWrite(LED_BUILTIN, LED_ON);
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
int day = hr / 24;
snprintf(temp, BUFFER_SIZE - 1,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AsyncWebServer-%s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>AsyncWebServer_Portenta_H7!</h2>\
<h3>running WiFi on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
request->send(200, "text/html", temp);
digitalWrite(LED_BUILTIN, LED_OFF);
}
void handleNotFound(AsyncWebServerRequest *request)
{
digitalWrite(LED_BUILTIN, LED_ON);
String message = "File Not Found\n\n";
message += "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);
digitalWrite(LED_BUILTIN, LED_OFF);
}
void drawGraph(AsyncWebServerRequest *request)
{
String out;
out.reserve(4000);
char temp[70];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"blue\">\n";
int y = rand() % 130;
for (int x = 10; x < 300; x += 10)
{
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
request->send(200, "image/svg+xml", out);
}
void printWifiStatus()
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("Local IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OFF);
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print("\nStart Async_AdvancedWebServer on ");
Serial.print(BOARD_NAME);
Serial.print(" with ");
Serial.println(SHIELD_TYPE);
Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);
///////////////////////////////////
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(1000);
// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);
// Connect to WPA/WPA2 network
status = WiFi.status();
}
printWifiStatus();
///////////////////////////////////
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
handleRoot(request);
});
server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
{
drawGraph(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(WiFi.localIP());
}
void heartBeatPrint()
{
static int num = 1;
Serial.print(F("."));
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define STATUS_CHECK_INTERVAL 10000L
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void loop()
{
check_status();
}

You can access the Async Advanced WebServer @ the server IP



Debug Terminal Output Samples

1. MQTT_ThingStream on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example MQTT_ThingStream on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library

Start MQTT_ThingStream on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 17
Connected! IP address: 192.168.2.87
***************************************
STM32_Pub
***************************************
Attempting MQTT connection to broker.emqx.io
...connected
Published connection message successfully!
Subscribed to: STM32_Sub
MQTT Message Send : STM32_Pub => Hello from MQTT_ThingStream on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
MQTT Message receive [STM32_Pub] Hello from MQTT_ThingStream on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
MQTT Message Send : STM32_Pub => Hello from MQTT_ThingStream on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
MQTT Message receive [STM32_Pub] Hello from MQTT_ThingStream on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library

2. WebClientRepeating on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example WebClient on on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library

Start WebClientRepeating on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 16
Connected! IP address: 192.168.2.87

Connecting...
HTTP/1.1 200 OK
Server: nginx/1.4.2
Date: Thu, 07 Oct 2021 05:16:59 GMT
Content-Type: text/plain
Content-Length: 2263
Last-Modified: Wed, 02 Oct 2013 13:46:47 GMT
Connection: close
Vary: Accept-Encoding
ETag: "524c23c7-8d7"
Accept-Ranges: bytes


           `:;;;,`                      .:;;:.           
        .;;;;;;;;;;;`                :;;;;;;;;;;:     TM 
      `;;;;;;;;;;;;;;;`            :;;;;;;;;;;;;;;;      
     :;;;;;;;;;;;;;;;;;;         `;;;;;;;;;;;;;;;;;;     
    ;;;;;;;;;;;;;;;;;;;;;       .;;;;;;;;;;;;;;;;;;;;    
   ;;;;;;;;:`   `;;;;;;;;;     ,;;;;;;;;.`   .;;;;;;;;   
  .;;;;;;,         :;;;;;;;   .;;;;;;;          ;;;;;;;  
  ;;;;;;             ;;;;;;;  ;;;;;;,            ;;;;;;. 
 ,;;;;;               ;;;;;;.;;;;;;`              ;;;;;; 
 ;;;;;.                ;;;;;;;;;;;`      ```       ;;;;;`
 ;;;;;                  ;;;;;;;;;,       ;;;       .;;;;;
`;;;;:                  `;;;;;;;;        ;;;        ;;;;;
,;;;;`    `,,,,,,,,      ;;;;;;;      .,,;;;,,,     ;;;;;
:;;;;`    .;;;;;;;;       ;;;;;,      :;;;;;;;;     ;;;;;
:;;;;`    .;;;;;;;;      `;;;;;;      :;;;;;;;;     ;;;;;
.;;;;.                   ;;;;;;;.        ;;;        ;;;;;
 ;;;;;                  ;;;;;;;;;        ;;;        ;;;;;
 ;;;;;                 .;;;;;;;;;;       ;;;       ;;;;;,
 ;;;;;;               `;;;;;;;;;;;;                ;;;;; 
 `;;;;;,             .;;;;;; ;;;;;;;              ;;;;;; 
  ;;;;;;:           :;;;;;;.  ;;;;;;;            ;;;;;;  
   ;;;;;;;`       .;;;;;;;,    ;;;;;;;;        ;;;;;;;:  
    ;;;;;;;;;:,:;;;;;;;;;:      ;;;;;;;;;;:,;;;;;;;;;;   
    `;;;;;;;;;;;;;;;;;;;.        ;;;;;;;;;;;;;;;;;;;;    
      ;;;;;;;;;;;;;;;;;           :;;;;;;;;;;;;;;;;:     
       ,;;;;;;;;;;;;;,              ;;;;;;;;;;;;;;       
         .;;;;;;;;;`                  ,;;;;;;;;:         
                                                         
                                                         
                                                         
                                                         
    ;;;   ;;;;;`  ;;;;:  .;;  ;; ,;;;;;, ;;. `;,  ;;;;   
    ;;;   ;;:;;;  ;;;;;; .;;  ;; ,;;;;;: ;;; `;, ;;;:;;  
   ,;:;   ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;,`;, ;;  ;;  
   ;; ;:  ;;  ;;  ;;  ;; .;;  ;;   ,;,   ;;;;`;, ;;  ;;. 
   ;: ;;  ;;;;;:  ;;  ;; .;;  ;;   ,;,   ;;`;;;, ;;  ;;` 
  ,;;;;;  ;;`;;   ;;  ;; .;;  ;;   ,;,   ;; ;;;, ;;  ;;  
  ;;  ,;, ;; .;;  ;;;;;:  ;;;;;: ,;;;;;: ;;  ;;, ;;;;;;  
  ;;   ;; ;;  ;;` ;;;;.   `;;;:  ,;;;;;, ;;  ;;,  ;;;; 

3. MQTTClient_Auth on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example MQTTClient_Auth on on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library

Start MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 9
Connected! IP address: 192.168.2.87
Attempting MQTT connection to broker.emqx.io...connected
Message Send : MQTT_Pub => Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message arrived [MQTT_Pub] Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message Send : MQTT_Pub => Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message arrived [MQTT_Pub] Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message Send : MQTT_Pub => Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message arrived [MQTT_Pub] Hello from MQTTClient_Auth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library

4. MQTTClient_Basic on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example MQTTClient_Basic on on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library

Start MQTTClient_Basic on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 8
Connected! IP address: 192.168.2.87
Attempting MQTT connection to broker.emqx.io...connected
Message Send : MQTT_Pub => Hello from MQTTClient_Basic on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message arrived [MQTT_Pub] Hello from MQTTClient_Basic on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message Send : MQTT_Pub => Hello from MQTTClient_Basic on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Message arrived [MQTT_Pub] Hello from MQTTClient_Basic on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library

5. Async_HTTPBasicAuth on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example Async_HTTPBasicAuth on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library

Start Async_HTTPBasicAuth on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 16
Connected! IP address: 192.168.2.87
Async_HttpBasicAuth started @ IP : 192.168.2.87
Open http://192.168.2.87/ in your browser to see it working
Login using username = admin and password = ethernet
[AWS] getMD5: Success
[AWS] genRandomMD5: res =  795d0e2e77f0bd1ac56d88a223e30cc8
[AWS] getMD5: Success
[AWS] genRandomMD5: res =  0bc073c1bf61fa0ae678fa5892cfd2a6
[AWS] requestDigestAuthentication: header =  realm="asyncesp", qop="auth", nonce="795d0e2e77f0bd1ac56d88a223e30cc8", opaque="0bc073c1bf61fa0ae678fa5892cfd2a6"
[AWS] getMD5: Success
[AWS] stringMD5: res =  9384b554a02515c9481a13b3787821da
[AWS] getMD5: Success
[AWS] stringMD5: res =  71998c64aea37ae77020c49c00f73fa8
[AWS] getMD5: Success
[AWS] stringMD5: res =  5908212c923b4d99fd278772873a142f
[AWS] AUTH SUCCESS

6. Async_AdvancedWebServer on PORTENTA_H7_M7 using Ethernet

Following are debug terminal output and screen shots when running example Async_AdvancedWebServer on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library to demonstrate the complex AsyncWebServer feature.

Start Async_AdvancedWebServer on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 4
Connected! IP address: 192.168.2.87
HTTP EthernetWebServer is @ IP : 192.168.2.87

You can access the Async Advanced WebServers at the displayed server IP, e.g. 192.168.2.87

Using Firefox Browser


7. Async_AdvancedWebServer on PORTENTA_H7_M7 using WiFi

Following is the debug terminal and screen shot when running example Async_AdvancedWebServer on Portenta_H7 WiFi to demonstrate the operation of Portenta_H7_AsyncWebServer, based on this Portenta_H7_AsyncTCP Library.

Start Async_AdvancedWebServer on PORTENTA_H7_M7 with Portenta_H7 WiFi
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Connecting to SSID: HueNet1
SSID: HueNet1
Local IP Address: 192.168.2.94
signal strength (RSSI):-31 dBm
HTTP EthernetWebServer is @ IP : 192.168.2.94
..........

You can access the Async Advanced WebServers at the displayed server IP, e.g. 192.168.2.94


8. Async_AdvancedWebServer_MemoryIssues_Send_CString on PORTENTA_H7_M7 using Ethernet

Following is the debug terminal and screen shot when running example Async_AdvancedWebServer_MemoryIssues_Send_CString on Portenta_H7 Ethernet to demonstrate the new and powerful HEAP-saving feature

Using SDRAM => not using HEAP (14,314 bytes)
Start Async_AdvancedWebServer_MemoryIssues_Send_CString using SDRAM on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 2
Connected! IP address: 192.168.2.123
HTTP EthernetWebServer is @ IP : 192.168.2.123

HEAP DATA - Pre Create Arduino String  Cur heap: 8458  Res Size: 451648  Max heap: 8472
.
HEAP DATA - Pre Send  Cur heap: 9634  Res Size: 451648  Max heap: 10196

HEAP DATA - Post Send  Cur heap: 9734  Res Size: 451648  Max heap: 12847
......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31200
.......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31219
.......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31255
.......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31198
.......... .......... .......... .......... .......... .......... .......... ........
HEAP DATA - Post Send  Cur heap: 11201  Res Size: 451648  Max heap: 14314
..
Out String Length=31224
.......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31235
Not using SDRAM ===> small heap (51,833 bytes)
Start Async_AdvancedWebServer_MemoryIssues_Send_CString on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 7
Connected! IP address: 192.168.2.38
HTTP EthernetWebServer is @ IP : 192.168.2.38

HEAP DATA - Pre Create Arduino String  Cur heap: 47434  Res Size: 451760  Max heap: 47448
.
HEAP DATA - Pre Send  Cur heap: 48607  Res Size: 451760  Max heap: 49811

HEAP DATA - Post Send  Cur heap: 48720  Res Size: 451760  Max heap: 51833
......... .......... .......... .......... .......... ....
Out String Length=31200
.......... .......... .......... .......... .......... .......... .......... ..........
Out String Length=31291

While using Arduino String, the HEAP usage is very large

Async_AdvancedWebServer_MemoryIssues_SendArduinoString ===> very large heap (111,387 bytes)

Start Async_AdvancedWebServer_MemoryIssues_SendArduinoString on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 0
Connected! IP address: 192.168.2.123
HTTP EthernetWebServer is @ IP : 192.168.2.123

HEAP DATA - Pre Create Arduino String  Cur heap: 7434  Res Size: 452016  Max heap: 7448
.
HEAP DATA - Pre Send  Cur heap: 48611  Res Size: 452016  Max heap: 48611

HEAP DATA - Post Send  Cur heap: 79009  Res Size: 452016  Max heap: 111347
.
HEAP DATA - Post Send  Cur heap: 79029  Res Size: 452016  Max heap: 111387
...
HEAP DATA - Post Send  Cur heap: 79037  Res Size: 452016  Max heap: 111403
....
HEAP DATA - Post Send  Cur heap: 79041  Res Size: 452016  Max heap: 111411
. ..
Out String Length=31247
.......
HEAP DATA - Post Send  Cur heap: 79047  Res Size: 452016  Max heap: 111423
. ....
Out String Length=31233
...... ......
Out String Length=31243
.... .......
Out String Length=31251
... .......
HEAP DATA - Post Send  Cur heap: 79054  Res Size: 452016  Max heap: 111437
..
Out String Length=31280
. ......

You can access the Async Advanced WebServers at the displayed server IP, e.g. 192.168.2.123


9. Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 using WiFi

Following is debug terminal output when running example Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 using WiFi, to demo how to use beginChunkedResponse() to send large html in chunks

Start Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 with Portenta_H7 WiFi
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Connecting to SSID: HueNet1
SSID: HueNet1
Local IP Address: 192.168.2.113
signal strength (RSSI):-31 dBm
AsyncWebServer is @ IP : 192.168.2.113
.[AWS] Total length to send in chunks = 31259
[AWS] Bytes sent in chunk = 948
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
.[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
.[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
.[AWS] Bytes sent in chunk = 519
[AWS] Bytes sent in chunk = 0

You can access the Portenta_H7_AsyncWebServer at the displayed server IP


10. Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 using Ethernet

Following is debug terminal output when running example Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 using Ethernet and Portenta_Ethernet Library , to demo how to use beginChunkedResponse() to send large html in chunks

Start Async_AdvancedWebServer_SendChunked on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.5.0
Using mac index = 3
Connected! IP address: 192.168.2.89
AsyncWebServer is @ IP : 192.168.2.89
.[AWS] Total length to send in chunks = 31259
[AWS] Bytes sent in chunk = 948
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 519
[AWS] Bytes sent in chunk = 0
.[AWS] Total length to send in chunks = 31279
[AWS] Bytes sent in chunk = 948
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 1064
[AWS] Bytes sent in chunk = 539
[AWS] Bytes sent in chunk = 0

You can access the Portenta_H7_AsyncWebServer at the displayed server IP



Debug

Debug is enabled by default on Serial.

You can also change the debugging level _PORTENTA_H7_AWS_LOGLEVEL_ from 0 to 4 in the library cpp files

#define _PORTENTA_H7_AWS_LOGLEVEL_     1

Troubleshooting

If you get compilation errors, more often than not, you may need to install a newer version of Arduino IDE, the Arduino mbed_portenta core or depending libraries.

Sometimes, the library will only work if you update the mbed_portenta core to the latest version because I'm always using the latest cores /libraries.


Issues

Submit issues to: Portenta_H7_AsyncWebServer issues



TO DO

  1. Fix bug. Add enhancement
  2. Add support to Murata WiFi

DONE

  1. Add support to Portenta_H7 using Vision-shield Ethernet
  2. Add Table of Contents
  3. Fix issue with slow browsers or network. Check Target stops responding after variable time when using Firefox on Windows 10 #3
  4. Support using CString in optional SDRAM to save heap to send very large data. Check request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8
  5. Add examples Async_AdvancedWebServer_SendChunked and AsyncWebServer_SendChunked to demo how to use beginChunkedResponse() to send large html in chunks
  6. Use allman astyle and add utils
  7. Fix _catchAllHandler not working bug.
  8. Improve README.md so that links can be used in other sites, such as PIO


Contributions and Thanks

  1. Based on and modified from Hristo Gochkov's ESPAsyncWebServer. Many thanks to Hristo Gochkov for great ESPAsyncWebServer Library
  2. Thanks to rusty-bit to initiate the Discussion in AsyncWebserver for Portenta H7 #6 leading to these Portenta_H7_AsyncTCP and Portenta_H7_AsyncWebServer libraries
  3. Thanks to Jeremy Ellis to test and report the compile error and crash issue with mbed_portenta core v2.6.1, leading to v1.2.0
  4. Thanks to salasidis aka rs77can to discuss and make the following marvellous PRs
  1. Thanks to roma2580 to report and help fix the bug in
me-no-dev
⭐️⭐️ Hristo Gochkov

rusty-bit
rusty-bit

hpssjellis
Jeremy Ellis

salasidis
⭐️ salasidis

roma2580
>⭐️ roma2580


Contributing

If you want to contribute to this project:

  • Report bugs and errors
  • Ask for enhancements
  • Create issues and pull requests
  • Tell other people about this library

License

  • The library is licensed under GPLv3

Copyright

Copyright 2021- Khoi Hoang

portenta_h7_asyncwebserver's People

Contributors

khoih-prog avatar salasidis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

portenta_h7_asyncwebserver's Issues

Compiling Library into application

I posted this in the arduino forum, but not sure if it should go here as well

If I compile the example, it works OK

However, if I try to use the async library in my application, the moment I issue a

#include <Portenta_H7_AsyncWebServer.h>

I get the following errors

WARNING: library MRI claims to run on mbed architecture(s) and may be incompatible with your current board which runs on mbed_portenta architecture(s).
In file included from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EthernetInterface.ino:15:0:
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:343:15: error: reference to 'Stream' is ambiguous
     void send(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
               ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/ArduinoAPI.h:36:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/Arduino.h:27,
                 from sketch\EnviroWatch.ino.cpp:1:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/Stream.h:50:7: note: candidates are: class arduino::Stream
 class Stream : public Print
       ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed/mbed.h:109:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed.h:8,
                 from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EnviroWatch.ino:16:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed/platform/include/platform/Stream.h:42:7: note:                 class mbed::Stream
 class Stream : public FileLike, private NonCopyable<Stream> {
       ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/ArduinoAPI.h:36:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/Arduino.h:27,
                 from sketch\EnviroWatch.ino.cpp:1:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/Stream.h:50:7: note:                 class arduino::Stream
 class Stream : public Print
       ^~~~~~
In file included from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EthernetInterface.ino:15:0:
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:343:15: error: 'Stream' has not been declared
     void send(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
               ^~~~~~
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:349:29: error: expected ';' at end of member declaration
     AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
                             ^~~~~~~~~~~~~
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:349:43: error: 'AsyncWebServerResponse* AsyncWebServerRequest::beginResponse' conflicts with a previous declaration
     AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
                                           ^~~~~~
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:347:29: note: previous declaration 'AsyncWebServerResponse* AsyncWebServerRequest::beginResponse(int, const arduino::String&, const arduino::String&)'
     AsyncWebServerResponse *beginResponse(int code, const String& contentType = String(), const String& content = String());
                             ^~~~~~~~~~~~~
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:349:50: error: expected ')' before '&' token
     AsyncWebServerResponse *beginResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
                                                  ^
In file included from C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:634:0,
                 from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EthernetInterface.ino:15:
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebResponseImpl.h:94:5: error: reference to 'Stream' is ambiguous
     Stream *_content;
     ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/ArduinoAPI.h:36:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/Arduino.h:27,
                 from sketch\EnviroWatch.ino.cpp:1:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/Stream.h:50:7: note: candidates are: class arduino::Stream
 class Stream : public Print
       ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed/mbed.h:109:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed.h:8,
                 from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EnviroWatch.ino:16:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/mbed/platform/include/platform/Stream.h:42:7: note:                 class mbed::Stream
 class Stream : public FileLike, private NonCopyable<Stream> {
       ^~~~~~
In file included from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/ArduinoAPI.h:36:0,
                 from C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/Arduino.h:27,
                 from sketch\EnviroWatch.ino.cpp:1:
C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\cores\arduino/api/Stream.h:50:7: note:                 class arduino::Stream
 class Stream : public Print
       ^~~~~~
In file included from C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebServer.h:634:0,
                 from \\SERVER846C\DS_846a\Projects\EnviroWatch\Code\MainCode\EnviroWatch\EthernetInterface.ino:15:
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebResponseImpl.h:97:32: error: expected ')' before '&' token
     AsyncStreamResponse(Stream &stream, const String& contentType, size_t len, AwsTemplateProcessor callback = nullptr);
                                ^
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebResponseImpl.h: In member function 'virtual bool AsyncStreamResponse::_sourceValid() const':
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebResponseImpl.h:101:17: error: '_content' was not declared in this scope
       return !!(_content);
                 ^~~~~~~~
C:\Users\Mydir\Documents\Arduino\libraries\Portenta_H7_AsyncWebServer-1.2.1\src/Portenta_H7_AsyncWebResponseImpl.h:101:17: note: suggested alternative: '_code'
       return !!(_content);
                 ^~~~~~~~
                 _code
Multiple libraries were found for "WiFiClient.h"
 Used: C:\Users\Mydir\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\libraries\WiFi
 Not used: C:\Program Files (x86)\Arduino\libraries\WiFi
 Not used: C:\Users\Mydir\Documents\Arduino\libraries\WiFiEspAT
exit status 1
Error compiling for board Arduino Portenta H7 (M7 core).

Mostly there seems to be am issue with the Stream class

I have tried adding

using namespace mbed;

or

using namespace arduino;

before the #include <Portenta_H7_AsyncWebServer.h> but that did not work.

Any ideas how to resolve this? I am using some mbed specific features, such as threads and the chrono library.

Thanks

PortentaH7 WiFi can you explain why it doesn't work on the M4 core

Does Wifi, BLE, and Ethernet not work on the M4 core from a hardware issue?

@khoih-prog I love your libraries which I just found today. I am the maker of the portenta-pro-community-solutions library. My twitter is aka rocksetta and I would really like to connect somehow.

I have not had much luck making the Portneta WiFi, BLE and Ethernet Async so I will check out this library. I recently posted on the Arduino mbed core a similar question

arduino/ArduinoCore-mbed#360

Keep up the great work!

Weird HTTP_POST hangup

HTTP_POST hangs up PortentaH7 when is use it to send a file name to the server and save said file on SD card but strangely only the second time i send the request. I have attached a sketch that should reproduce the issue and reflects what i'm trying to do.

I've tried this both using Arduino IDE 2.0.0 and VSCode + PlatformIO.
I'm using a PortentaH7 Rev2 and breakout.

#include <WiFi.h>
#warning Using WiFi for Portenta_H7.
#include <Portenta_H7_AsyncWebServer.h>
#include <SDMMCBlockDevice.h>
#include <FATFileSystem.h>
#include <ArduinoJson.h>

SDMMCBlockDevice block_device;
mbed::FATFileSystem fs("fs");
char ssid[] = "SSIDname";      
char pass[] = "123456789";    // I use it this way since i want it to be a WiFi A.P.    

int status = WL_IDLE_STATUS;
AsyncWebServer    server(80);

void handleRoot(AsyncWebServerRequest *request)
{
  String html ="<!DOCTYPE html> <html> <head> </head> <body> <label for=\"fname\">File Name:</label><br> <input type=\"text\" id=\"fname\" name=\"fname\" value=\"\"><br> <label for=\"temperature\" name=\"tname\">Temp:</label><p id=\"temp\"></p><br> <button class=\"button\" type=\"button\" onclick=logTemp()>LogTemp</button> <script> function logTemp(){ var xhr = new XMLHttpRequest(); var filename=document.getElementById(\"fname\").value; const params={\"fname\":filename}; xhr.open(\"POST\", \"/logTemp\",true); xhr.setRequestHeader(\"Content-Type\",\"application/json;charset=UTF-8\"); xhr.send(JSON.stringify(params)); } setInterval(function(){getTemperature();},2000); function getTemperature(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange=function(){ if(this.readyState==4 && this.status==200){ document.getElementById(\"temp\").innerText=this.responseText;} }; xhr.open(\"GET\",\"/temperature\",true); xhr.send(); } </script> </body> </html>";
  request->send(200, "text/html",html);
}

void handleNotFound(AsyncWebServerRequest *request)
{
  String message = "File Not Found\n\n";

  message += "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);
}

String readTemperature(){
  int lower=20,upper=50;
  int num=(rand()%(upper-lower+1))+lower;
  return String(num);
} // A random val in range function since no sensor is connected
void printWifiStatus()
{
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("Local IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
void saveTestFile(String file, String fileName){
  FILE *testFile;
  String fspref ="/fs/";
  fspref+=fileName;
  fspref+=".json";
  testFile=fopen(fspref.c_str(),"w+");
  fprintf(testFile, file.c_str());
  fclose(testFile);
}
void setup() {
  // put your setup code here, to run once:
#pragma region WIFI_SETUP&CARD_MOUNT
  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print("\nStart Async_AdvancedWebServer on "); Serial.print(BOARD_NAME);
  Serial.print(" with "); Serial.println(SHIELD_TYPE);
  Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
  Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);

  ///////////////////////////////////

  status = WiFi.beginAP(ssid,pass);
  if(status != WL_AP_LISTENING){
    Serial.println("Creating access point failed");
    // don't continue
    while (true);
  }

  printWifiStatus();

  Serial.println("Mounting SDCARD...");
  int err =  fs.mount(&block_device);
  if (err) {
    // Reformat if we can't mount the filesystem
    // this should only happen on the first boot
    Serial.println("No filesystem found, formatting... ");
    err = fs.reformat(&block_device);
  }
  if (err) {
     Serial.println("Error formatting SDCARD ");
     while(1);
  }
  
  DIR *dir;
  struct dirent *ent;
  int dirIndex = 0;

  Serial.println("List SDCARD content: ");
  if ((dir = opendir("/fs")) != NULL) {
    // Print all the files and directories within directory (not recursively)
    while ((ent = readdir (dir)) != NULL) {
      Serial.println(ent->d_name);
      dirIndex++;
    }
    closedir (dir);
  } else {
    // Could not open directory
    Serial.println("Error opening SDCARD\n");
    while(1);
  }
  if(dirIndex == 0) {
    Serial.println("Empty SDCARD");
  }
  ///////////////////////////////////
  #pragma endregion

server.on("/logTemp", 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];
      }
      DynamicJsonDocument doc(100);
      deserializeJson(doc,jsondata);
      String testFile_name=doc["fname"];
      doc["temp"]=readTemperature().c_str();
      String testSer;
      serializeJson(doc,testSer);
      saveTestFile(testSer,testFile_name);
      request->send(200);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    request->send(200,"text/plain",readTemperature().c_str());
  });
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    handleRoot(request);
  });
  server.onNotFound(handleNotFound);
  server.begin();
}
void check_status()
{
  static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL     10000L

  // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
  if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
  {
    checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  check_status();
}

request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap

Posted by @salasidis as request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #2 in Portenta_H7_AsyncTCP library


Is your feature request related to a problem? Please describe.

I have a relatively web page - about 100kBytes. In addition, I need to send large amounts of Json data - about 1Mbyte sensor data via the async web interface.

I save all the data in the SDRAM (the web page as well as the Json data string (created from SD Card logged data).

When responding to the request via
request->send(200, textPlainStr, jsonChartDataCharStr); // jsonChartDataCharStr is a C type char * string

The library converts the (char *) array (JSon or the web page) into a String class variable before sending. This results in large amounts of heap use, and basically I cannot send more than 4 months of telemetry data (whereas the design calls for at least 2 years).

Describe the solution you'd like

I would like to have a request->send call that looks at the variable passed, and if it is a char * leaves it as such, without changing it to a String. Not only will this be faster, but it will also use a LOT less memory

Describe alternatives you've considered

My alternatives of making many calls to retrieve the data in small batches would add needless complexity, and also require multiple accesses to the SD card, which I want to avoid

Additional context

This would not be an issue if heap space was not so critical, but the portenta only has a small finite amount, and this library consumes about 80+% of it in my application in a single function call.


It would be nice if any mallocs could be limited, or at least made to optionally use the SDRAM, and not the main RAM.

Also any string manipulations (adding a header etc) could be done in place - just having to ensure that the passed string is at least a minimum size larger than the contents it holds.


On the default program, I added some code to print the heap size, and I increased the graph array from 50 to 500 points.

On the modified program, I created a C string instead of an Arduino String, and sent the same 500 line graph svg file

There is a word file that shows the outputs, and the outputs demonstrate that if sending an Arduino String, the software requires 2x extra heap space on top of the the actual string size.

If sending a C string, it takes 3x.

My proposal is to have an option (separate call, or modify the current ones), that can use the c string as is, and simply pre and post append to it any headers and footers required, without creating new Arduino Strings in the library


File Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino

/****************************************************************************************************************************
  Async_AdvancedWebServer.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet

  For Portenta_H7 (STM32H7) with Vision-Shield Ethernet

  Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet

  Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
  Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer
  Licensed under GPLv3 license

  Copyright (c) 2015, Majenko Technologies
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

  Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

  Neither the name of Majenko Technologies nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************************************************************/

#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )
  #error For Portenta_H7 only
#endif

#define _PORTENTA_H7_ATCP_LOGLEVEL_     1
#define _PORTENTA_H7_AWS_LOGLEVEL_      1

#define USE_ETHERNET_PORTENTA_H7        true

#include <Portenta_Ethernet.h>
#include <Ethernet.h>
#warning Using Portenta_Ethernet lib for Portenta_H7.

#include <Portenta_H7_AsyncWebServer.h>

// 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, 0x32, 0x01 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
};
// Select the IP address according to your local network
IPAddress ip(192, 168, 2, 232);

AsyncWebServer    server(80);

int reqCount = 0;                // number of requests received

#define LED_OFF             HIGH
#define LED_ON              LOW


#define BUFFER_SIZE         512
char temp[BUFFER_SIZE];

void handleRoot(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int day = hr / 24;

  snprintf(temp, BUFFER_SIZE - 1,
           "<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>AsyncWebServer-%s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>AsyncWebServer_Portenta_H7!</h2>\
<h3>running on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);

  request->send(200, "text/html", temp);

  digitalWrite(LED_BUILTIN, LED_OFF);
}

void handleNotFound(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);
  String message = "File Not Found\n\n";

  message += "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);
  digitalWrite(LED_BUILTIN, LED_OFF);
}

void PrintHeapData(String hIn){
  mbed_stats_heap_t heap_stats;
  
  Serial.print("HEAP DATA - ");
  Serial.print(hIn);

  mbed_stats_heap_get(&heap_stats);
  Serial.print("  Cur heap: ");
  Serial.print(heap_stats.current_size);
  Serial.print("  Res Size: ");
  Serial.print(heap_stats.reserved_size);
  Serial.print("  Max heap: ");
  Serial.println(heap_stats.max_size);
}

void drawGraph(AsyncWebServerRequest *request)
{
  String out;

  out.reserve(40000);
  char temp[70];

  out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n";
  out += "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
  out += "<g stroke=\"blue\">\n";
  int y = rand() % 130;

  for (int x = 10; x < 5000; x += 10)
  {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
  }
  out += "</g>\n</svg>\n";

  PrintHeapData("Pre Send");

  Serial.print("Out String Length=");
  Serial.println(out.length());

  request->send(200, "image/svg+xml", out);

  PrintHeapData("Post Send");
}


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LED_OFF);

  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print("\nStart Async_AdvancedWebServer on "); Serial.print(BOARD_NAME);
  Serial.print(" with "); Serial.println(SHIELD_TYPE);
  Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
  Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);

  ///////////////////////////////////

  // start the ethernet connection and the server
  // Use random mac
  uint16_t index = millis() % NUMBER_OF_MAC;

  // Use Static IP
  //Ethernet.begin(mac[index], ip);
  // Use DHCP dynamic IP and random mac
  Ethernet.begin(mac[index]);

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    Serial.println("No Ethernet found. Stay here forever");

    while (true)
    {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF)
  {
    Serial.println("Not connected Ethernet cable");
  }

  Serial.print(F("Using mac index = "));
  Serial.println(index);

  Serial.print(F("Connected! IP address: "));
  Serial.println(Ethernet.localIP());

  ///////////////////////////////////
 
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    handleRoot(request);
  });

  server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    drawGraph(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(Ethernet.localIP());

  
  PrintHeapData("Pre Create Arduino String");

}

void heartBeatPrint()
{
  static int num = 1;

  Serial.print(F("."));

  if (num == 80)
  {
    Serial.println();
    num = 1;
  }
  else if (num++ % 10 == 0)
  {
    Serial.print(F(" "));
  }
}

void check_status()
{
  static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL     10000L

  // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
  if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
  {
    heartBeatPrint();
    checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
  }
}

void loop()
{
  check_status();
}

File Async_AdvancedWebServer_MemoryIssues_Send_CString.ino

/****************************************************************************************************************************
  Async_AdvancedWebServer.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet

  For Portenta_H7 (STM32H7) with Vision-Shield Ethernet

  Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet

  Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
  Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer
  Licensed under GPLv3 license

  Copyright (c) 2015, Majenko Technologies
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

  Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

  Neither the name of Majenko Technologies nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************************************************************/

#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )
  #error For Portenta_H7 only
#endif

#define _PORTENTA_H7_ATCP_LOGLEVEL_     1
#define _PORTENTA_H7_AWS_LOGLEVEL_      1

#define USE_ETHERNET_PORTENTA_H7        true

#include <Portenta_Ethernet.h>
#include <Ethernet.h>
#warning Using Portenta_Ethernet lib for Portenta_H7.

#include <Portenta_H7_AsyncWebServer.h>

#include "SDRAM.h"

// 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, 0x32, 0x01 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
};
// Select the IP address according to your local network
IPAddress ip(192, 168, 2, 232);

AsyncWebServer    server(80);

int reqCount = 0;                // number of requests received

#define LED_OFF             HIGH
#define LED_ON              LOW


#define BUFFER_SIZE         512
char temp[BUFFER_SIZE];

void handleRoot(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);

  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
  int day = hr / 24;

  snprintf(temp, BUFFER_SIZE - 1,
           "<html>\
<head>\
<title>AsyncWebServer-%s</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h2>AsyncWebServer_Portenta_H7!</h2>\
<h3>running on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);

  request->send(200, "text/html", temp);

  digitalWrite(LED_BUILTIN, LED_OFF);
}

void handleNotFound(AsyncWebServerRequest *request)
{
  digitalWrite(LED_BUILTIN, LED_ON);
  String message = "File Not Found\n\n";

  message += "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);
  digitalWrite(LED_BUILTIN, LED_OFF);
}

void PrintHeapData(String hIn){
  mbed_stats_heap_t heap_stats;
  
  Serial.print("HEAP DATA - ");
  Serial.print(hIn);

  mbed_stats_heap_get(&heap_stats);
  Serial.print("  Cur heap: ");
  Serial.print(heap_stats.current_size);
  Serial.print("  Res Size: ");
  Serial.print(heap_stats.reserved_size);
  Serial.print("  Max heap: ");
  Serial.println(heap_stats.max_size);
}


char *cStr;


void drawGraph(AsyncWebServerRequest *request) {
  char temp[80];
  String out;

  out.reserve(4000);

  cStr[0] = '\0';

  strcat(cStr, "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n");
  strcat(cStr, "<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n");
  strcat(cStr, "<g stroke=\"blue\">\n");
  int y = rand() % 130;

  for (int x = 10; x < 5000; x += 10)
  {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
    strcat(cStr, temp);
    y = y2;
  }
  strcat(cStr, "</g>\n</svg>\n");

  PrintHeapData("Pre Send");

  Serial.print("Out String Length=");
  Serial.println(strlen(cStr));

  request->send(200, "image/svg+xml", cStr);

  PrintHeapData("Post Send");
}


void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LED_OFF);

  Serial.begin(115200);
  while (!Serial);

  delay(200);

  Serial.print("\nStart Async_AdvancedWebServer on "); Serial.print(BOARD_NAME);
  Serial.print(" with "); Serial.println(SHIELD_TYPE);
  Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION);
  Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);



  SDRAM.begin();

  cStr = (char *)SDRAM.malloc(100000);    // make a little larger than required

  if (cStr == NULL) {
    Serial.println("Unable top Allocate RAM");
    for(;;);
  }

  ///////////////////////////////////

  // start the ethernet connection and the server
  // Use random mac
  uint16_t index = millis() % NUMBER_OF_MAC;

  // Use Static IP
  //Ethernet.begin(mac[index], ip);
  // Use DHCP dynamic IP and random mac
  Ethernet.begin(mac[index]);

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    Serial.println("No Ethernet found. Stay here forever");

    while (true)
    {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF)
  {
    Serial.println("Not connected Ethernet cable");
  }

  Serial.print(F("Using mac index = "));
  Serial.println(index);

  Serial.print(F("Connected! IP address: "));
  Serial.println(Ethernet.localIP());

  ///////////////////////////////////
 
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    handleRoot(request);
  });

  server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    drawGraph(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(Ethernet.localIP());

    
  PrintHeapData("Pre Create Arduino String");

}

void heartBeatPrint()
{
  static int num = 1;

  Serial.print(F("."));

  if (num == 80)
  {
    Serial.println();
    num = 1;
  }
  else if (num++ % 10 == 0)
  {
    Serial.print(F(" "));
  }
}

void check_status()
{
  static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL     10000L

  // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
  if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
  {
    heartBeatPrint();
    checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
  }
}

void loop()
{
  check_status();
}

Heap Data when Sending a 500 line string

Heap Data when Sending a 500 line string (string is 31.2k long).
Max Heap = 7.4k – before executing graph routine
Max Heap = 48.5k – after reserving 40k for out string
Max Heap = 111.2k – after send
When sending am Arduino String, the library requires 2x the String length in extra heap.

Using mac index = 14
Connected! IP address: 192.168.0.72
HTTP EthernetWebServer is @ IP : 192.168.0.72
HEAP DATA - Pre Create Arduino String  Cur heap: 7434  Res Size: 452112  Max heap: 7448
.HEAP DATA - Pre Send  Cur heap: 48566  Res Size: 452112  Max heap: 48566
Out String Length=31257
HEAP DATA - Post Send  Cur heap: 78958  Res Size: 452112  Max heap: 111294
HEAP DATA - Pre Send  Cur heap: 48566  Res Size: 452112  Max heap: 111294

The second program modifies the source code, so that instead of creating the out String, I create a C string called cStr. This has a buffer allocated in SDRAM. I then create the cStr in a similar fashion as before, except using strcat etc. (I remove the meta .. refresh – just to make the output cleaner)

When running the code – a 500 line graph string is sent here as well – the string is again 31.2k long
Max Heap = 8.4k – before executing graph routine
Max Heap = 13.5k after creating the string, but before send *** note that there is no heap increase as before
Max Heap = 107.5k – after send

When sending a c string, the web server uses 3x the c string size iin heap space (1x more than before, as it makes a String out of the c string before proceeding

Start Async_AdvancedWebServer on PORTENTA_H7_M7 with Ethernet using Portenta_Ethernet Library
Portenta_H7_AsyncTCP v1.4.0
Portenta_H7_AsyncWebServer v1.3.0
Using mac index = 6
Connected! IP address: 192.168.0.72
HTTP EthernetWebServer is @ IP : 192.168.0.72
HEAP DATA - Pre Create Arduino String  Cur heap: 8458  Res Size: 452000  Max heap: 8472
.HEAP DATA - Pre Send  Cur heap: 13590  Res Size: 452000  Max heap: 13590
Out String Length=31257
HEAP DATA - Post Send  Cur heap: 43982  Res Size: 452000  Max heap: 107576

Unable upload file

I need to save (and subsequently read) a zip file on sd card by calling POST, after multiple attempts I was unable to obtain the contents of the file. (To test I used Postman by uploading a zip file in the body as binary). I checked

typedef std::function<void(AsyncWebServerRequest *request, /*const String& filename,*/ size_t index, uint8_t *data, size_t len, bool final)> ArUploadHandlerFunction;

where I find an inconsistency with the documentation as a parameter is commented out.

Would you like some suggestions?

Information

Please ensure to specify the following:

  • Arduino IDE version (e.g. 1.8.19)
  • Arduino Portenta H7 Core version 3.01

Example

/****************************************************************************************************************************
  Async_HelloServer.h

  For Portenta_H7 (STM32H7) with Vision-Shield Ethernet

  Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with with Vision-Shield Ethernet

  Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
  Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer
  Licensed under GPLv3 license
 *****************************************************************************************************************************/
#define isDebug true
#define SERIAL_BOUDRATE 9600

#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )
#error For Portenta_H7 only
#endif

#define _PORTENTA_H7_AWS_LOGLEVEL_     1

#define USE_ETHERNET_PORTENTA_H7        true


#include "SDMMCBlockDevice.h"
#include "FATFileSystem.h"

SDMMCBlockDevice block_device;
mbed::FATFileSystem fs("fs");

#include <Portenta_Ethernet.h>
#include <Ethernet.h>
#warning Using Portenta_Ethernet lib for Portenta_H7.
#include <Portenta_H7_AsyncWebServer.h>

// Enter a MAC address and IP address for your controller below.
#define NUMBER_OF_MAC      20

// Select the IP address according to your local network
#define DHCP_ENABLE false
IPAddress ip(10, 1, 10, 2);
AsyncWebServer server(80);

byte mac[][NUMBER_OF_MAC] =
{
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
  { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
};

#define ON          LOW
#define OFF         HIGH
#define LED_OFF     0
#define LED_CGREEN  1
#define LED_CRED    2
#define LED_CBLUE   3
#define LED_CYELLOW 4

#define BUFFER_SIZE 64
char temp[BUFFER_SIZE];

void setupLed() {
  pinMode(LEDG, OUTPUT);
  pinMode(LEDR, OUTPUT);
  pinMode(LEDB, OUTPUT);
}
void setLed(int mode) {
  switch (mode) {
    case LED_OFF:
      digitalWrite(LEDG, OFF);
      digitalWrite(LEDR, OFF);
      digitalWrite(LEDB, OFF);
      break;
    case LED_CGREEN:
      digitalWrite(LEDG, ON);
      digitalWrite(LEDR, OFF);
      digitalWrite(LEDB, OFF);
      break;
    case LED_CRED:
      digitalWrite(LEDG, OFF);
      digitalWrite(LEDR, ON);
      digitalWrite(LEDB, OFF);
      break;
    case LED_CBLUE:
      digitalWrite(LEDG, OFF);
      digitalWrite(LEDR, OFF);
      digitalWrite(LEDB, ON);
      break;
    case LED_CYELLOW:
      digitalWrite(LEDG, ON);
      digitalWrite(LEDR, ON);
      digitalWrite(LEDB, OFF);
      break;
  }
}
template <typename T>
void serialPrintln(T msg) {
  if (isDebug) {
    Serial.println(msg);
  }
}
template <typename T>
void serialPrint(T msg) {
  if (isDebug) {
    Serial.print(msg);
  }
}

void handleUpload(AsyncWebServerRequest *request, size_t index, uint8_t *data, size_t len, bool final) {
  if (!index) {
    //serialPrintln("UploadStart: %s\n", filename.c_str());
    serialPrintln("UploadStart");
  }
  for (size_t i = 0; i < len; i++) {
    serialPrint(data[i]);
  }
  if (final) {
    //serialPrintln("UploadEnd: %s, %u B\n", filename.c_str(), index+len);
    serialPrintln("UploadEnd");
  }
}

void handleRoot(AsyncWebServerRequest *request)
{
  setLed(LED_CBLUE);
  serialPrintln(request->args());
  snprintf(temp, BUFFER_SIZE - 1, "Hello from Async_HelloServer on %s\n", BOARD_NAME);
  request->send(200, "text/plain", temp);
  setLed(LED_OFF);
}
void handleNotFound(AsyncWebServerRequest *request)
{
  setLed(LED_CBLUE);
  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);
  setLed(LED_OFF);
}

void setup()
{
  setupLed();
  if (isDebug) {
    Serial.begin(SERIAL_BOUDRATE);
    while (!Serial) {
      setLed(LED_CYELLOW);
      delay(100);
      setLed(LED_OFF);
      delay(100);
    }
  }
  setLed(LED_OFF);

  serialPrint("\nStart Async_HelloServer on "); serialPrint(BOARD_NAME);
  serialPrint(" with "); serialPrintln(SHIELD_TYPE);
  serialPrintln(PORTENTA_H7_ASYNC_TCP_VERSION);
  serialPrintln(PORTENTA_H7_ASYNC_WEBSERVER_VERSION);

  ///////////////////////////////////
  serialPrintln("Mounting SDCARD...");
  int err =  fs.mount(&block_device);
  if (err) {
    // Reformat if we can't mount the filesystem
    // this should only happen on the first boot
    serialPrintln("No filesystem found, formatting... ");
    err = fs.reformat(&block_device);
  }
  if (err) {
    serialPrintln("Error formatting SDCARD ");
    while (true) {
      setLed(LED_CRED);
      delay(100);
      setLed(LED_OFF);
      delay(100);
    }
  }
  ///////////////////////////////////
  // start the ethernet connection and the server
  // Use random mac
  uint16_t index = millis() % NUMBER_OF_MAC;

  if (!DHCP_ENABLE) {
    Ethernet.begin(mac[index], ip);
  } else {
    Ethernet.begin(mac[index]);
  }

  if (Ethernet.hardwareStatus() == EthernetNoHardware)
  {
    serialPrintln("No Ethernet found. Stay here forever");

    while (true)
    {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF)
  {
    serialPrintln("Not connected Ethernet cable");
    while (Ethernet.linkStatus() == LinkOFF) {
      setLed(LED_CRED);
      delay(100);
      setLed(LED_OFF);
      delay(100);
    }
  }

  serialPrint(F("Using mac index = "));
  serialPrintln(index);

  serialPrint(F("Connected! IP address: "));
  serialPrintln(Ethernet.localIP());

  ///////////////////////////////////

  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
  {
    handleRoot(request);
  });
  server.on("/post", HTTP_POST, [](AsyncWebServerRequest * request) {
    serialPrintln(request->params());
    request->send(200);
  }, handleUpload
           );
  server.on("/inline", [](AsyncWebServerRequest * request)
  {
    request->send(200, "text/plain", "This works as well");
  });

  server.onNotFound(handleNotFound);
  server.begin();

  serialPrint(F("HTTP EthernetWebServer is @ IP : "));
  serialPrintln(Ethernet.localIP());
}

void heartBeatPrint()
{
  static int num = 1;

  serialPrint(F("."));

  if (num == 80)
  {
    serialPrintln("");
    num = 1;
  }
  else if (num++ % 10 == 0)
  {
    serialPrint(F(" "));
  }
}

void check_status()
{
  static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL     10000L

  // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
  if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
  {
    heartBeatPrint();
    checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
  }
}

void loop()
{
  check_status();
}

Mqtt and Async Webserver can't co-exsist.

Describe the bug

Combining the Wifi MQTT and the Async Webserver example results in crashed OS (red leds blinking)

Code : https://github.com/javos65/AsyncWebServer_plus_MQTT

Expected behavior

Should co-exsist ?

Actual behavior

Mbed OS crashes as soon as Webserver is approached by http-get call : Red leds flashing.

Screenshots

n.a.

Information

Arduino IDE 1.8.18
Arduino IDE 2.0
Portenta H7 rev2
lib Portenta_H7_AsyncWebServer 1.4.2
lib Portenta_H7_AsyncTCP 1.4.0
lib PubSubClient V2.8 ---> should we use another PubSub ???

Cannot send more than 4 characters from server -> client

I am unable to send a message to the client that is more than 4X characters long. I am able to reliably receive/decode on my my client (python+websockets), however, if the message from Portenta_H7_AsyncWebServer is longer than 4 characters, I get a ProtocolError: reserved bits must be 0 error which triggers a disconnect.

to reproduce:

void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
  if(type== WS_EVT_CONNECT){
    Serial.println("WS Connected!");
    client->text("Hey"); // works
  }
    else if(type == WS_EVT_DISCONNECT)
  {
    //client disconnected
    Serial.println("ws disconnect");
  }
  else if(type == WS_EVT_DATA){
    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
      
      if(info->opcode == WS_TEXT)
      {
        data[len] = 0;
        Serial.println("New MSG");
        client->text("12535");  // fails
      } 
      

There is no problem on the server receiving arbitrarly long messages, so it doesn't seem like a buffer issue?

Currently using:

Arduino_MachineControl v.1.1.0
Portenta_H7_AsyncTCP v 1.3.2
Portenta_H7_AsyncWebServer v. 1.2.1

The Portenta is using Arduino Mbed OS Portenta Boards v 3.0.0

#include "resources.h"

I am likely not doing something right, but I am trying to compile the Async_advancedWebServer.ino example for a Portenta H7, and I get a compile error

C:\Users\RS\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\libraries\WiFi\src\WiFi.cpp:220:10: fatal error: resources.h: No such file or directory
Multiple libraries were found for "WiFiClient.h"
#include "resources.h"
Used: C:\Users\RS\AppData\Local\Arduino15\packages\arduino\hardware\mbed_portenta\3.0.1\libraries\WiFi
^~~~~~~~~~~~~
Not used: C:\Users\RS\Documents\Arduino\libraries\WiFiEspAT
compilation terminated.
exit status 1
Error compiling for board Arduino Portenta H7 (M7 core).

Any idea how to fix this?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.