/* ESPNOWESP32v1 04/02/2024 */ #include "esp_now.h" #include "WiFi.h" #include "esp_wifi.h" //extra library to change mac address // REPLACE WITH YOUR RECEIVER MAC Address //uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};//All stations broadcast address uint8_t broadcastAddress[] = {0xE0, 0xE2, 0xE6, 0xD1, 0x0D, 0x01}; //broadcast address uint8_t CustomMACaddress[] = {0xE0, 0xE2, 0xE6, 0xD1, 0x0D, 0x02}; //Custom MAC address //uint8_t CustomMACaddress[] = {0xE0, 0xE2, 0xE6, 0xD1, 0x0D, 0x01}; //Custom MAC address //uint8_t broadcastAddress[] = {0xE0, 0xE2, 0xE6, 0xD1, 0x0D, 0x02}; //Broadcast address // Structure example to send data // Must match the receiver structure typedef struct struct_message { byte myByte[50]; } struct_message; // Create a struct_message called myData struct_message sendData;//data to be sent struct_message recData;//data received esp_now_peer_info_t peerInfo; // 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"); //This line demonstrates the data returned //Ideally it fails it should really resend the data. Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } // callback function that will be executed when data is received void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&recData, incomingData, sizeof(recData)); //WARNING: The code below is badly written and to demonstrate the onDataRevv function only //Any data should be copied out of this function as quickly as possible and processed in the main loop //This will allow for the next set of data to arrive //Use similar to an interrupot routine Serial.print("Bytes received: "); Serial.println(len); for(int w=0;w < 50;w++){ Serial.print("byte: "); Serial.print(w); Serial.print(" = "); Serial.print(recData.myByte[w]); } Serial.println(" "); } void setup() { // Init Serial Monitor Serial.begin(115200); Serial.println("ESPNOWESP32v1"); // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); Serial.print("Original MAC address: "); Serial.println(WiFi.macAddress());//The address the board came with esp_wifi_set_mac(WIFI_IF_STA, &CustomMACaddress[0]); Serial.print("Custom MAC Address for ESP32: "); Serial.println(WiFi.macAddress()); /*Prints Custom MAC address*/ // 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); //on data receive function esp_now_register_recv_cb(OnDataRecv); // 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; } } byte q;//used to store an incrementing byte to demo transmission void loop() { int w; //just used as a counter q++; // Set values to send for(w=0;w < 50;w++){ //different send data patterns to distinguish between boards //sendData.myByte[w] = q++;//q will overun at 255 and go back to Zero sendData.myByte[w] = w + q;//q will overun at 255 and go back to Zero Serial.print(sendData.myByte[w]); Serial.print(","); } Serial.println(" "); // Send message via ESP-NOW esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &sendData, sizeof(sendData)); delay(2000); }