Using ESP32 C6 Dev. I've copied two codes off the internet for one way communication. The code for the master works fine but both different codes for the slave come up with the error ' Compilation error: invalid conversion from void (*)(const uint8_t* , const uint8_t*, int)' {aka 'void (*)(const unsigned char* , const unsigned char*, int)'} to 'esp_now_recv_cb_t' {aka 'void (*)(const esp_now_recv_info* , const unsigned char*, int)'} [-fpermissive] and have the line esp_now_register_recv_cb(OnDataRecv); highlighted. What could be the issue?
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
uint8_t newData;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP("RX_1", "RX_1_Password", CHANNEL, 0);
esp_now_init();
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
Serial.print("I did this to data -> ");
Serial.println(newData * 5);
delay(3000);
}
/** callback when data is recv from Master **/
void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
Serial.print("I just received -> ");
Serial.println(*data);
memcpy(&newData, data, sizeof(newData));
}
Rather than messing about with someone else unknown code! Take a look at Getting Started with ESP-NOW (ESP32 with Arduino IDE) I used this when I first started out with ESP-NOW & have been using this code for a couple of years now, so can vouch that it works OK
I've changed the code and I'm now getting this errorCompilation error: redefinition of 'struct esp_now_recv_info'. I'm pretty new to this, is there a site to learn how to use the version 5 esp now that isnt as vague as the espressif website.
#include <esp_now.h>
#include <WiFi.h>
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
struct esp_now_recv_info {
uint8_t *{0x40, 0x4C, 0xCA, 0x50, 0xD9, 0xB5};
uint8_t *{0x40, 0x4C, 0xCA, 0x50, 0xD9, 0xB5};
wifi_pkt_rx_ctrl_t *Rx_1;
};
// callback function that will be executed when data is received
ypedef void (*esp_now_recv_cb_t)(const uint8_t *mac_addr, esp_now_revc_status_t status) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Char: ");
Serial.println(myData.a);
Serial.print("Int: ");
Serial.println(myData.b);
Serial.print("Float: ");
Serial.println(myData.c);
Serial.print("Bool: ");
Serial.println(myData.d);
Serial.println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}
That struct has already defined in a header that has been included, as well as that typedef that declares that struct as a esp_now_recv_info_t.
If you compare the two signatures for v4 and v5, aside from the variable names, which are can actually be anything (but chosen to make sense of course), the only difference between them is as I said before: for the first argument, what was a uint8_t pointer is now a esp_now_recv_info_t pointer.
Undo any other changes. You only need to change the signature of your OnDataRecv callback to match, because you don't use that variable anyway.
I have run into this problem a couple times and have traced it to the version3.x in the Arduino board manager. As long as your running the older 2.0.17 or earlier everything works fine but if you update to 3. anything it breaks something, I'm not sure what but it means you can't use a lot of sketches for the newer boards including the esp32 c6 boards