#include #include #define LEDpin D7 //Connect LED on pin 2 // Structure example to receive data // Must match the sender structure typedef struct struct_message { //create a structure called "message" and add the contents of the messag int btnstate; //integer value "State" } struct_message; struct_message myData; // Create a struct_message called myData // Callback function that will be executed when data is received void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) { memcpy(&myData, incomingData, sizeof(myData)); Serial.print("Received state: "); Serial.println(myData.btnstate); Serial.println(); } void setup() { pinMode(LEDpin, OUTPUT); //Define LED as output Serial.begin(115200); // Initialize Serial Monitor WiFi.mode(WIFI_STA); // Set device as a Wi-Fi Station // Init ESP-NOW if (esp_now_init() != 0) { 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_set_self_role(ESP_NOW_ROLE_SLAVE); esp_now_register_recv_cb(OnDataRecv); } void loop() { if (myData.btnstate == 0) { digitalWrite(LEDpin, HIGH); } else { digitalWrite(LEDpin, LOW); } }