//----------------------------------------Load libraries #include #include //----------------------------------------Defines PIN Button and PIN LED. #define LED_Pin D4 #define BTN_Pin D2 //---------------------------------------------------------------------------------------------------------------------------- //uint8_t broadcastAddress[] = {0xE0, 0x98, 0x06, 0x9C, 0x16, 0x7F}; uint8_t broadcastAddress[] = {0xE0, 0x98, 0x06, 0x0B, 0x1E, 0xEF}; //--------------------------------------------------------------------------------------------------------------------------- int BTN_State; //--> Variable to hold the button state. int LED_State_Send = 1; //--> Variable to hold the data to be transmitted to control the LEDs. int LED_State_Receive; //--> Variable to receive data to control the LEDs on the ESP32 running this code. //----------------------------------------Structure example to send data must match the receiver structure typedef struct struct_message { int led; } struct_message_send; struct_message send_Data; // Create a struct_message to send data. struct_message receive_Data; // Create a struct_message to receive data. //--------------------------------------------------------------------------------------------------------------------------- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Callback when data is sent void OnDataSent(uint8_t *mac_addr, uint8_t status) { Serial.print("\r\nLast Packet Send Status:\t"); if (status ==0){ Serial.println("Delivery success"); } else{ Serial.println("Delivery fail"); } Serial.println(">>>>>"); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Callback when data is received void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) { memcpy(&receive_Data, incomingData, sizeof(receive_Data)); Serial.println(); // Serial.println("<<<<< Receive Data:"); // Serial.print("Bytes received: "); // Serial.println(len); LED_State_Receive = receive_Data.led; Serial.print("Receive Data: "); Serial.println(LED_State_Receive); Serial.println("<<<<<"); digitalWrite(LED_Pin, LED_State_Receive); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ VOID SETUP void setup() { Serial.begin(115200); pinMode(LED_Pin, OUTPUT); pinMode(BTN_Pin, INPUT_PULLUP); WiFi.mode(WIFI_STA); //--> Set device as a Wi-Fi Station WiFi.disconnect(); //----------------------------------------Init ESP-NOW if (esp_now_init() != 0) { 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); esp_now_register_recv_cb(OnDataRecv); //--> Register for a callback function that will be called when data is received // Set ESP-NOW Role esp_now_set_self_role(ESP_NOW_ROLE_COMBO); //---------------------------------------- //----------------------------------------Register peer if (esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0) == 0) { Serial.println("Peer added successfully !!"); } else { Serial.println("Failed to add peer !!"); } Serial.println("Initializing push button ..."); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void loop() { BTN_State = digitalRead(BTN_Pin); //--> Reads and holds button states. //----------------------------------------When the button is pressed it will send data to control the LED on the ESP32 Target. if(BTN_State == 0) { LED_State_Send = !LED_State_Send; // toggle state send_Data.led = LED_State_Send; // send state to the other board Serial.println(); Serial.print(">>>>> "); Serial.println("Send data"); //----------------------------------------Send message via ESP-NOW if (esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data))==0) { Serial.println("Message sent successfully"); } else { Serial.println("Error sending the data"); } //----------------------------------------Wait for the button to be released. Release the button first to send the next data. while(BTN_State == 0) { BTN_State = digitalRead(BTN_Pin); delay(10); } } }