I'm working a a school project where part of the project needs to use ESP NOW to let the master tell the slave when to turn on and off a relay. I've gotten the ESP NOW to work and send data but I'm stuck on how I would control the relay (what data to send from the master to the slave?, What to do with the data once the slave receives it?). I've had a go at what I thought would work (It didn't) and attached it below. Thanks
#include <esp_now.h>
#include <WiFi.h>
int relay = 20;
typedef struct struct_message {
int poop;
} 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 *peerInfo, const uint8_t *data, int data_len) {
memcpy(&myData, data, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(data_len);
Serial.print("Relay Value: ");
Serial.println(myData.poop);
if(myData.poop = 1){
digitalWrite(relay, HIGH);
}
if(myData.poop = 0){
digitalWrite(relay,LOW);
}
}
void setup() {
pinMode(relay, OUTPUT);
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
esp_now_init();
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}