Now my question: after receiving data from Master there are this lines:
// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
Serial.printf(" Message: %s\n", (char *)data);
}
So this works all well. But how do i get the "(char *)data" into my own char array. I have read that this could be done by the vector functions but didn't get a clue how this should work. Can anyone give me a link or a sample code how i get this data to work with it in the sketch. That would be nice.
hape
I assume you do need it to survive the function call. (if not just use the data pointer)
let's say you have a global buffer
const size_t maxMessageSize = 100;
char message[maxMessageSize]; // remember the need for the trailing null
bool messageAvailable = false;
then if you want to copy the content of data into message you can use memcpy()
// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
if (len < maxMessageSize) { // or could use the macro min(x,y)
memcpy ( message, data, len );
message[len] = '\0';
} else {
memcpy ( message, data, maxMessageSize-1 );
message[maxMessageSize-1] = '\0';
}
...; // you can do stuff here on the message
messageAvailable = true; // or later in the loop if you set a flag
}
then in your loop you can do something like
void loop() {
if (messageAvailable) {
// deal with the message
...
messageAvailable = false;
}
}
thx a lot for this really usefull answers i will try it and come back with my experience. I think it will work well so i will write my thoughts and flag it than as resolved.
hi
works nice. On the solution from @ilguargua a get a compile error so i tried the other solution from @J-M-L and this works like a charm. So i'm happy and thx to all helping me :-).
hape