I have a project with espcam and esp32 and I need to transfer data from the espcam to the esp32 using ESPNOW. I discovered that I need to have the same channel on both of the processors. I changed it to the same channel and the data is successfully delivered, but comes empty.
Code:
Sender (espcam):
uint8_t broadcastAddress1[] = {assume its the right one here :)}; //Enter your Receiver board MAC Address
String mess;
typedef struct test_struct {
int a;
} test_struct;
test_struct test;
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
//ESPNOW
WiFi.mode(WIFI_STA);
WiFi.printDiag(Serial); // Uncomment to verify channel number before
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(3, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
WiFi.printDiag(Serial); // Uncomment to verify channel change after
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
// register peer
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
//ESPNOW
esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
test.a = 5;
Serial.println(test.a);
delay(1);
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(2000);
Receiver (esp32):
typedef struct test_struct {
int a;
} test_struct;
//Create a struct_message called myData
test_struct myData;
//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("a: ");
Serial.println(myData.a);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(myData.a);
}
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.printDiag(Serial); // Uncomment to verify channel number before
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(3, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
WiFi.printDiag(Serial); // Uncomment to verify channel change after
Serial.println(WiFi.channel());
//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);
I tried changing the channel to the right one, and it helped to connect between them, but the data still comes in empty.
It seems you are setting the struct variable
test's attributeato5only after you send the data.Make sure to initialize the payload to what you want prior to sending.