In this code I need to call the function after the code of adding peer, but I have problem in the output of the function, why the output of the function is correct when call it before WiFi.mode and the output is incorrect number when call it after the Add peer ? what happen!!
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = { 0x24, 0x22, 0xAB, 0xE0, 0x34, 0x51};
//#define LENGTH 500
//unsigned char data[LENGTH] = {0};
esp_now_peer_info_t peerInfo;
int8_t data[]={88,86,86,85,85,83,83,83,83,83,83,84,84,85,85,85,85,85,85,85};
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
struct result {
size_t outSize;
unsigned char *bytes;
};
result strc;
void Send_ESP_Data(int8_t data[]) {
for( int i=0;i<20;i++){
strc.outSize +=data[i];
}
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
Send_ESP_Data(data);
Serial.printf("outSize befor Add peer =%i \n ",strc.outSize);
// 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 Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
Send_ESP_Data(data); // the calling here gave me incorrect result
Serial.printf("outSize after Add peer =%i \n ",strc.outSize);
int sendoutSize = strc.outSize;
// Here I want to use esp_now_send(broadcastAddress);
}//end setup
void loop() {} // repeat the lines of code below