ESP_NOW POC
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
|
||||
#include <esp_now.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#define ONBOARD_LED 2 //GPIO02 D2
|
||||
|
||||
// Structure example to receive data
|
||||
// Must match the sender structure
|
||||
typedef struct struct_message {
|
||||
char a[32];
|
||||
int b;
|
||||
float c;
|
||||
bool led;
|
||||
} struct_message;
|
||||
|
||||
// Create a struct_message called myData
|
||||
struct_message myData;
|
||||
|
||||
// callback function that will be executed when data is received
|
||||
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
|
||||
memcpy(&myData, incomingData, sizeof(myData));
|
||||
Serial.print("Bytes received: ");
|
||||
Serial.println(len);
|
||||
Serial.print("Char: ");
|
||||
Serial.println(myData.a);
|
||||
Serial.print("Int: ");
|
||||
Serial.println(myData.b);
|
||||
Serial.print("Float: ");
|
||||
Serial.println(myData.c);
|
||||
Serial.print("Bool: ");
|
||||
Serial.println(myData.led);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Initialize Serial Monitor
|
||||
Serial.begin(115200);
|
||||
|
||||
// Set device as a Wi-Fi Station
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
// Init ESP-NOW
|
||||
if (esp_now_init() != ESP_OK) {
|
||||
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_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
|
||||
pinMode(ONBOARD_LED, OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (myData.led == true){
|
||||
digitalWrite(ONBOARD_LED, HIGH);
|
||||
} else {
|
||||
digitalWrite(ONBOARD_LED, LOW);
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
Reference in New Issue
Block a user