/* ESPNOWBroadcastESP32v1 04/02/2024 delivery from other board fails because the MAC address has changed sending is OK because it's sending to all addresses. */ #include "esp_now.h" #include "WiFi.h" //no longer needed as wifi address not changed //#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 // 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 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 interrupt 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("ESPNOWBroadcastESP32v1"); // 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; } //on data receive function esp_now_register_recv_cb(OnDataRecv); // Register peer memcpy(peerInfo.peer_addr, broadcastAddress, 6); //peerInfo.channel = 0; peerInfo.channel = 1; 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 unsigned long currentMillis; unsigned long transmitMillis; int transmitTimer = 2000; void loop() { int w; //just used as a counter currentMillis = millis(); if (currentMillis - transmitMillis >= transmitTimer) { transmitMillis = currentMillis; 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)); //Serial.println(result); } }