void sendWiegandIDtoServer(String id) {
if (checkWiegandIDforErrors(id))
return;
logOutput("Access Code '" + id + "' has been entered.");
ws.textAll("access_code");
String key = "multicontroller, key=" + id;
uint8_t bufferLengh = key.length();
uint8_t buffer[bufferLengh];
key.toCharArray((char *)buffer, bufferLengh);
Serial.print("To be sent to server: ");
Serial.println(key);
if (!wiegand_udp_on || wiegand_state.ip_wiegand == "not set" ||
wiegand_state.ip_wiegand.length() == 0) {
logOutput("ERROR ! Invalid IP Address for Wiegand. Please enter a valid IP.");
return;
}
// send packet to server
wiegand_udp.beginPacket(wiegand_state.ip_wiegand.c_str(),
wiegand_state.port_wiegand.toInt());
wiegand_udp.write(buffer, sizeof(buffer));
delay(30);
logOutput(wiegand_udp.endPacket()
? (String) "Wiegand ID: " + id + " was sent over UDP."
: "WARNING: Wiegand ID not sent.");
memset(buffer, 0, bufferLengh);
}
// for context:
void logOutput(String string1)
{
circle.push(string1);
Serial.println(string1);
}
I have String key, which I place in a buffer uint8_t buffer[bufferLengh]; using key.toCharArray((char *)buffer, bufferLengh); and then I send it with wiegand_udp.write(buffer, sizeof(buffer));.
The issue is that even if logOutput(wiegand_udp.endPacket() ? (String) "Wiegand ID: " + id + " was sent over UDP." : "WARNING: Wiegand ID not sent."); logs a correct id, which is a 6 digit code, wiegand_udp.write(buffer, sizeof(buffer)); only sends "multicontroller, key=" + id with 5 digit instead of 6.
void sendWiegandIDtoServer(String& id) { // <== add & to pass the parameter by reference instead of copy
...
// send packet to server
const char * header = "multicontroller, key=";
wiegand_udp.beginPacket(wiegand_state.ip_wiegand.c_str(), wiegand_state.port_wiegand.toInt());
wiegand_udp.write(header, strlen(header) );
wiegand_udp.write(id.c_str(), id.length()); // or id.length() + 1 if you want to send the trailing null char
and if you could use a const char * for your id instead of a String, that would be even better for the memory