Hello. I am using ESP-NOW to communicate wireleslly between remote ESP32 nodes. I have found this library :
https://github.com/yoursunny/WifiEspNow/blob/master/src/WifiEspNow.cpp
In order to send data:
and receiving data:
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf));
}
digitalWrite(ONBOARD_LED,HIGH);
Serial.println();
}
I have a few questions:
1. Is it possible to simplify the sending data function ? I dont really understand the way he is suggesting to cast the message data.
2. Lets say I send a string to one ESP32 device:
and my reciver ESP32 device will collect the data that I have sent byte by byte. :
After I receive a full message, I would like to convert the data into a string.
What would be the correct way to convert data that is stored in uint8_t* buf into a char array?
I have tried the using strcat function as following:
I have declared my char array as following:
char string_var[10];
But when receiving any message, the device restarts (meditation error, exception was unhandled)
https://github.com/yoursunny/WifiEspNow/blob/master/src/WifiEspNow.cpp
In order to send data:
Code:
char msg[60];
int len = snprintf(msg, sizeof(msg), "2");
WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg),len);
and receiving data:
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf));
}
digitalWrite(ONBOARD_LED,HIGH);
Serial.println();
}
I have a few questions:
1. Is it possible to simplify the sending data function ? I dont really understand the way he is suggesting to cast the message data.
2. Lets say I send a string to one ESP32 device:
Code:
char msg[60];
int len = snprintf(msg, sizeof(msg), "hello");
WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg),len);
Code:
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf[i]));
}
digitalWrite(ONBOARD_LED,HIGH);
Serial.println();
}
What would be the correct way to convert data that is stored in uint8_t* buf into a char array?
I have tried the using strcat function as following:
I have declared my char array as following:
char string_var[10];
Code:
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf[i]));
strcat(string_var, (char*)buf[i]);
}
Serial.print("string_var=");
Serial.println(string_var);
digitalWrite(ONBOARD_LED,HIGH);
Serial.println();
}