#include // initializing the required resource libraries #include uint8_t broadcastAddress[] = {0x30,0x30,0xF9,0x17,0xF8,0x94}; // Secondary's MAC Address 84:f3:eb:e1:57:fd #define BUTTON_PIN 13 // The ESP8266 pin D7 connected to button typedef struct struct_message { //create a structure called "message" and add the contents of the message int btnstate; //integer value "c" } struct_message; struct_message myData; // Create a struct_message called myData unsigned long lastTime = 0; unsigned long timerDelay = 1000; // send readings timer void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { Serial.print("Last Packet Send Status: "); if (sendStatus == 0){ Serial.println("Delivery success"); } else{ Serial.println("Delivery fail"); } } void setup() { Serial.begin(115200); // Init 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 Send CB to // get the status of Trasnmitted packet esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); esp_now_register_send_cb(OnDataSent); // Register peer esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); // Initialize the Serial to communicate with the Serial Monitor. pinMode(BUTTON_PIN, INPUT_PULLUP); // Configure the ESP8266 pin as a pull-up input: HIGH when the button is open, LOW when pressed. } void loop() { if ((millis() - lastTime) > timerDelay) { //int button_state = digitalRead(BUTTON_PIN); // read the state of the switch/button: myData.btnstate = digitalRead(BUTTON_PIN); Serial.println(myData.btnstate); // print out the button's state esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); lastTime = millis(); } }