59 lines
1.4 KiB
C++
59 lines
1.4 KiB
C++
#include <esp_now.h>
|
|
#include <WiFi.h>
|
|
|
|
// Structure example to receive data
|
|
// Must match the sender structure
|
|
typedef struct struct_message {
|
|
int x;
|
|
int y;
|
|
bool b;
|
|
} struct_message;
|
|
|
|
// Create a struct_message called myData
|
|
struct_message myData;
|
|
|
|
// Callback function that will be executed when data is received
|
|
void OnDataRecv(const esp_now_recv_info_t *esp_now_info, const uint8_t *incomingData, int len) {
|
|
// Copy incoming data into the myData structure
|
|
memcpy(&myData, incomingData, sizeof(myData));
|
|
|
|
// Print the MAC address of the sender
|
|
// Serial.print("Received from MAC: ");
|
|
// for (int i = 0; i < 6; i++) {
|
|
// Serial.printf("%02X", esp_now_info->src_addr[i]);
|
|
// if (i < 5) Serial.print(":");
|
|
// }
|
|
// Serial.println();
|
|
|
|
// Print received data
|
|
// Serial.print("x: ");
|
|
Serial.print(myData.x);
|
|
Serial.print(" ");
|
|
Serial.print(myData.y);
|
|
Serial.print(" ");
|
|
Serial.println(myData.b);
|
|
}
|
|
|
|
void setup() {
|
|
// Initialize Serial Monitor
|
|
Serial.begin(115200);
|
|
|
|
// Set device as a Wi-Fi Station
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
// Initialize ESP-NOW
|
|
if (esp_now_init() != ESP_OK) {
|
|
Serial.println("Error initializing ESP-NOW");
|
|
return;
|
|
}
|
|
|
|
// Register callback function to receive data
|
|
esp_now_register_recv_cb(OnDataRecv);
|
|
|
|
Serial.println("Receiver ready. Waiting for data...");
|
|
}
|
|
|
|
void loop() {
|
|
// Nothing to do here
|
|
}
|