I'd be highly suspect of that "solution". From AsyncUDP.cpp:
uint8_t * AsyncUDPPacket::data()
{
return _data;
}
So, the returned pointer points to an array of uint8_t. But, since a UDP packet doesn't have to contain ASCII, the array won't be null-terminated. That's why you need the length() function. So, the String object you generate will probably contain garbage characters past the end of the array up to the first null. You're flirting with a memory problem.
I'd do something like the following. It compiles but is untested. Also, for brevity, I left out all the stuff that's not necessary to demonstrate the technique ... WiFi connection, etc.
#include "Arduino.h"
#include "AsyncUDP.h"
AsyncUDP udp;
void processPacket(AsyncUDPPacket &packet);
bool comparePacketString(AsyncUDPPacket &packet, const char *str);
const uint16_t udpPort = 1234;
void setup() {
if (udp.listen(udpPort)) {
udp.onPacket(processPacket);
} else {
Serial.println("Failed to initiate UDP Packet Listening");
}
}
void loop() {
}
void processPacket(AsyncUDPPacket &packet) {
if (comparePacketString(packet, "Intruder")) {
Serial.println("Intruder");
} else if (comparePacketString(packet, "Heartbeat")) {
Serial.println("Heartbeat");
}
}
bool comparePacketString(AsyncUDPPacket &packet, const char *str) {
size_t len = strlen(str);
if (packet.length() < len) {
return false;
}
if (memcmp(str, packet.data(), len) == 0) {
return true;
}
return false;
}
But, this whole topic begs the question … why are you bothering to send a multi-byte ASCII string just to convey so little information. Why don't you encode the state into a single-byte value and send just that? For example, "Heartbeat" = 1 and "Intruder" = 2.