//Project_V6 ESP-NOW: Two-Way communication controlling LED
//Upload this code to every board, but remember to change MAC Address! (line15)
#include <esp_now.h>
#include <WiFi.h>
#define LED_Pin 4
#define BTN_Pin 35
int BTN_State; //store button state
int LED_State_Send = 0; //store data that's going to be transmitted from this ESP to control led at reciver ESP
int LED_State_Receive; //store data recived from other ESP to control led at this ESP
String success; //store to see if sending data was a success
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //MAC Address of your receiver (the board that is reciving command from this ESP32)
//Ex Board 1 -> C4:DD:57:C9:84:24 = {0xC4, 0xDD, 0x57, 0xC9, 0x89, 0x24}
//Ex Board 2 -> C4:DD:57:CB:73:10 = {0xC4, 0xDD, 0x57, 0xCB, 0x73, 0x10}
//structure transmitting data
typedef struct struct_message {
int led;
} struct_message_send;
struct_message send_Data; //structure message to send
struct_message receive_Data; //structure message to receive
//callback function to see if data was sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { //function executed when message sent (mac address, send status)
Serial.print("\r\nSend Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
if (status == 0){
success = "Success :)";
}
else{
success = "Fail :(";
}
Serial.println(">>>>>");
}
//callback function to see if data was received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { //function executed when message received (mac address, data, length of data)
memcpy(&receive_Data, incomingData, sizeof(receive_Data)); //copy data incoming data and ad it to recive_Data varable (memcpy -> copies from memory)
Serial.println();
Serial.println("<<<<< Receive Data:");
Serial.print("Bytes received: ");
Serial.println(len); //length of packet (how many bytes message)
LED_State_Receive = receive_Data.led;
Serial.print("Receive Data: ");
Serial.println(LED_State_Receive);
Serial.println("<<<<<");
digitalWrite(LED_Pin, LED_State_Receive); //turn led on or off
}
void setup() {
Serial.begin(115200);
//set pinmode for led and button on ths ESP
pinMode(LED_Pin, OUTPUT);
pinMode(BTN_Pin, INPUT);
WiFi.mode(WIFI_STA); //ESP is in Wi-Fi Station mode
//INITIALIZE ESP-NOW
//if not ok, print error message
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
//if successfull goes to/registers callback function OnDataSent to transmitt data
esp_now_register_send_cb(OnDataSent);
//register peer (other ESP) that is going to recive the data
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
//add the peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
//goes to/register callback function OnDataRecv to receive data from other ESP
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
BTN_State = digitalRead(BTN_Pin); //read button state on this ESP
//if button pressed, send data to control led on other ESP via ESP-NOW
if(BTN_State == 1) {
LED_State_Send = !LED_State_Send;
send_Data.led = LED_State_Send;
Serial.println();
Serial.print(">>>>> ");
Serial.println("Send data");
//send message to other ESP via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data)); //add broadcast address to send_Data varable, and get message length
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
while(BTN_State == 1) { //send data when button is released
BTN_State = digitalRead(BTN_Pin);
delay(10);
}
}
}