I am using this guide to send UDP packets between two ESP32's. When I try to send a string, I get an error. I looked into the documentation in the IDE and it says that only uint8_t type data can be sent. Is there any way I can send strings/char arrays?
UDP is a generic protocol where the payload is an array of bytes (aka uint8_t) and it means if you have a string (e.g. do you have "String" or "C strings" or "char array"?) yes, you can do it you just need to convert it into a byte array.
But there are some ways to do it, so if you show us your code we can help you more than that.
Currently, I am having trouble receiving data from UDP and was hoping sending it as a string would fix it. I am trying to send the MAC address of a ESP32C3 to an ESP32 acting as a WiFi access point. I am able to convert the MAC address to a byte array (I think), but I am not receiving the sent MAC address.
Yep, that's correct. You are NOT sending a "string" you're still sending a packet with its 6 byte values.
From the picture:
don't you get why you see there a ";" and the ending "L"? What are the ASCII codes of those characters?
Yes, they are exactly 0x3B and 0x4C:
There's no need to do that, you're sending a byte array containing the 6 MAC address bytes and it's the correct way to do that.
If you want to see the MAC address you can just do this:
Serial.print("Packet received: ");
for(int b=0; b<6; ++b) {
Serial.print(packet[b], HEX);
// Add ":" as needed
if (b<5) Serial.print(":");
}
Serial.println();
to get exactly this:
Packet received: EC:DA:3B:BB:AE:4C
Or, if you need a string (a "C string"...) you can add this:
char mac[20] = "";
char val0[3];
for(int b=0; b<6; ++b) {
// convert a byte into single 2-char hex string
sprintf(val0, "%02X", packet[b]);
// Add it to the full MAC address string:
strcat(mac, val0);
// Add ":" as needed
if (b<5) strcat(mac, ":");
}
Serial.print("Packet received (converted to string): ");
Serial.println(mac);
PS: why do you need to send a MAC address to the other party?
I need to communicate the MAC address to the other party so that when a UDP message gets broadcasted to all clients connected to the other party (it is acting as an AP), the one who's MAC address matches up will start communicating with the AP.
For a similar solution I just used a single byte as a device ID (I don't need more than 255 clients and 255 servers), set for each and stored in EEPROM. I understand the MAC isn't a thing you need to pre-configure, but I found it unnecessarily long for my scope.
In my protocol any packet has the same format, with a simple header block before the payload, with a prefix with the protocol version (just to let me distinguish protocol versions, and create more protocols in the future, e.g. "P1") then a byte with the character "S" if the packet detination is a server or "C" for a client, then the ID of the client/server (or 255 if a broadcast), followed by a command byte and related parameters (if any).
For example (I'm using now characters instead of byte values, to make things clearer), if a client with ID 3 needs to "announce" itself to the servers, could send a packet "P1SC3": "P1" protocol, to any Server ("S"), "C" is "client announce" command, and ID is 3. The server(s) adds the client to its list, then can now send to it a command to set LED 2 ("L02") on (H): "P1C3L02H". And so on...
Anyway, it's obviously ok if you used MAC as the identifier... But your last question is a problem that is a directly consequence of that... Compare a byte is simpler than an byte array...
Well, ask yourself what does WiFi.macAddress() returns? It's a byte array, right?
That means you need to compare the received MAC with the 6 bytes you just received from UDP. What is the format of that UDP data (aka what is "packet[]" array)? Here:
char packet[255];
So you better first change it to "byte" (or "uint8_t"):
byte packet[255];
then, remove this ad it isn't needed anymore (we're not dealing with strings):
if (len > 0) {
packet[len] = '\0';
}
Once you have your 6-bytes array, just compare it byte-per-byte with the target (e.g. a "byte target[];") . Example:
if (memcmp(packet, target, 6) == 0) {
// OK, the MAC is right!!!
...
}