Reading data from inside server.on

Apple Mac OS
Firefox 124.0.1
Arduino IDE 2.3.3-nightly-20240328
Arduino Nano ESP32
AsyncTCP
AsyncUDP
Arduino C++

I am using ESPAsyncWebServer. I went through the library and made a list of Methods and Properties, which is obviously incomplete.

How do I;
read the incoming data

  1. read the incoming data,
  2. get the length of the incoming data string ,
  3. get the source IP?

there is some documentation on the main page of the GitHub

look at the request-variables for example

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 avaiable in Handler::canHandle)
request->contentLength(); // size_t:  ContentLength of the request (not avaiable in Handler::canHandle)
request->multipart();     // bool:    True if the request has content type "multipart"

What data?

Do yo mean something like this example:

What I'm looking for would look something like this;

server.on("/", HTTP_GET, []() {
    extern int bufferIndex;

    // Extract source_IP and destination_IP from the request headers
    source_IP = WiFi.remoteIP();
    destination_IP = WiFi.localIP();
    payload_length = WiFi.length();

    //returns bufferIndex
    acquire_a_buffer(payload_length); 

    //buffer[bufferIndex]is allocated to be big enough to handle payload_length
    //copy incoming data to the memory location beginning at buffer[bufferIndex] 
    buffer[bufferIndex]* = WiFi.data();
}


What data?

I'm starting to think maybe you have some basic misconception about how things work and you're looking for a solution in the wrong place.

Back up a little and describe the project instead of your proposed solution.

An http request follows a protocol - you don’t get just a payload of data from your web page but also other stuff

Print the various variables listed previously and you’ll see

I think you are completely missing the point of the AsyncWebServer, and what the server.on() function actually does. server.on() simply sets up a callback to process requests sent to a specific URL. YOU define the callback function. The callback function you pass as the third argument to server.on() is called any time a request to read the page in the first argument is sent by the client. The callback processes the server request passed to it in the AsyncWebServerRequest object, which tells you everything you need to know about the specifics of the request. server.on() does NOT process any message. It ONLY sets up YOUR callback function so it will be called when the corresponding request is received.

You need to study the example programs and understand how to use server.on(), and how to write a callback function to process the messages you expect to receive.

I'm working on a robot. I expect the project will involve 3 or 4, or maybe more, microcontrollers (Nano ESP32). Mostly the microcontrollers will talk to each other. I will also need an HMI to interact with the thing. The part I'm working on now is a communications interface. It accepts what comms traffic comes in (TCP/HTTP or UDP), reads the header to determine where it's supposed to go, changes the transmission to the protocol appropriate for the destination, then resends it.
The example command I'm working with now goes: (From a browser) IPaddress/ 0 debug on (return). The command is received by my comms module. My comms module reads it and trips server.on. I collect the source IP, and the length of the transmission (I already know the destination IP). Buffer space is allocated dynamically to be big enough to handle the incoming payload. The "data" (payload) part is: '0 debug on (return)'. (This tells the main processor (comms) to turn on debug.) The payload is copied into the buffer. The call ends and the stack space is released. The payload in the buffer is dealt with in the application layer.
All I need here is how to collect the source IP and the payload length, then copy the payload ("data") into the buffer.

AsyncWebServer is for serving webpages. If you just want to do straight communication over TCP or something then it's the wrong library.

I started with ESPAsyncWebServer. Somehow I ended up with AsyncWebServer in the code. What library should I be using?

Not a web server. You're not making a web page. Just use the WiFi library. There are tons of examples of TCP or UDP bridges.

1 Like

Those two sentences prove you do not understand the purpose of server.on. Nobody "trips server.on". It is typically called during setup, to inform the web server of the pages to be served, their URLs, and what function will process the requests when they are received. server.on is NEVER called as a result of a message being received, so your whole approach is badly flawed, and will not work. If you want to do your own message routing and handling, then you either need to make significant changes to the AsyncWebServer code, or, more likely, toss it, and write your own message handling code from scratch, to suit your actual needs. What you describe is NOT a normal web server.

IMO, much confusion is caused by the library's examples using lambda expressions for callbacks instead of free functions in the .on() functions.

1 Like

On one positive note though, that example was where I learned lambdas in C++.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.