I am still learning to NOT use String class. My project is now over 400 lines of code, so just a warning if you want to see the whole sketch.
The question-
When do I want to use memcpy or strcpy?
Here's my function where the question appears. Near the bottom is both strcpy and memcpy. They both do the same result. When should I use one over the other?
(incomingMessage is a global char[20])
Thanks
// ======================== mqtt callback ========================
// This function is executed when some device publishes a message to a topic that this ESP8266 is subscribed to.
//
void callback(String topic, byte * payload, unsigned int length) {
char message[length + 1];
// convert the payload from a byte array to a char array
memcpy(message, payload, length);
// add NULL terminator to message, making it a correct c-string
message[length] = '\0';
Serial.println();
Serial.println();
Serial.print(F("Message arrived on topic: "));
Serial.print(topic);
Serial.println(F("."));
Serial.print("message: ");
Serial.println(message);
Serial.print(F("Length= "));
Serial.print(length);
Serial.println();
//strcpy(incomingMessage, message);
memcpy(incomingMessage, message, length);
if (topic == incomingTopic) {
ringFlag = true; // Start ringing the phone
}
}