Code: Select all
#include <esp_now.h>
#include <WiFi.h>
#define LED_Pin1 4
#define LED_Pin2 12
typedef struct struct_message {
int led1;
int led2;
} struct_message ;
struct_message receive_Data; // Create a struct_message to receive data.
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&receive_Data, incomingData, sizeof(receive_Data));
Serial.println();
Serial.println("<<<<< Receive Data:");
Serial.print("Bytes received: ");
Serial.println(len);
digitalWrite(LED_Pin1, receive_Data.led1);
digitalWrite(LED_Pin2, receive_Data.led2);
Serial.println("<<<<<");
}
void setup(){
Serial.begin(115200);
digitalWrite(LED_Pin1, HIGH);
digitalWrite(LED_Pin2, HIGH);
pinMode(LED_Pin1, OUTPUT);
pinMode(LED_Pin2, OUTPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop(){}