parsing data AsyncUDP ESP32

Sorry for the insistence, but I'm stuck, I should take the single bytes and send them to a 2400baud serial. I can visualize them on the terminal, but I can't understand how to read them in an array so as to manage them.

Can anyone help me figure out how to take data from this function?

I can't get the bytes from the function:

packet.data()

I would like to put the received bytes in an array.
char buf[10];
buf = packet.data();
does not work
Thanks in advance

#include "WiFi.h"
#include "AsyncUDP.h"

const char * ssid = "***********";
const char * password = "***********";

AsyncUDP udp;

void setup()
{
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid, password);
    if (WiFi.waitForConnectResult() != WL_CONNECTED) {
        Serial.println("WiFi Failed");
        while(1) {
            delay(1000);
        }
    }
    if(udp.listen(1234)) {
        Serial.print("UDP Listening on IP: ");
        Serial.println(WiFi.localIP());
        udp.onPacket([](AsyncUDPPacket packet) {
            Serial.print("UDP Packet Type: ");
            Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
            Serial.print(", From: ");
            Serial.print(packet.remoteIP());
            Serial.print(":");
            Serial.print(packet.remotePort());
            Serial.print(", To: ");
            Serial.print(packet.localIP());
            Serial.print(":");
            Serial.print(packet.localPort());
            Serial.print(", Length: ");
            Serial.print(packet.length());
            Serial.print(", Data: ");
            Serial.write(packet.data(), packet.length());
            Serial.println();
            //reply to the client
            packet.printf("Got %u bytes of data", packet.length());
        });
    }
}

Help

Hi Giuseppe,
if I understood you correctly you already received data through AsyncUDP, but now you want to write the buffered data in a separate array.
The fact is, you don’t actually have to!
packet.data() already is of type (uint8_t*), so initially something like uint8 packet.data()[ ]
One way to copy an array to another is by simply iterating through each element:

//your own buffer
uint8_t buf[20];


for (int i=0;i<packet.length();i++){
    buf[i]= *(packet.data()+i);
}
//If you want "char", then just cast packet to it like so:

char buf[20];

for (int i=0;i<packet.length();i++){
    buf[i]= (char)*(packet.data()+i);
}

You can also use memcpy() for this, but be aware of your arraysizes.

1 Like