#include <WiFi.h>
#include <esp_now.h>
//MAC receiver (who is sending)
uint8_t brcAdr[]={0x8C,0xAA,0xB5,0x84,0xFB,0x90};
esp_now_peer_info_t peerInfo; //Object to know when a message arrives and what to do
//Sending callback
void OnDataSent(const uint8_t*mac_addr, esp_now_send_status_t status){
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Ok" : "Error");
}
//Receiving Callback
void OnDataRecv(const uint8_t*mac, const uint8_t*data,int len){ //Function for data received
Serial.println("Msg received: ");
char rcvstring[len];
memcpy(rcvstring,data,len); //copy of rcvstring on data, with lenght len
Serial.println(String(rcvstring));
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); //enable WIFI module
esp_now_init(); //initialize espnow protocol
//send callback
esp_now_register_send_cb(OnDataSent); //register the status
//receive callback
esp_now_register_recv_cb(OnDataRecv); //what to do when something is received
//Peer Registration
memcpy(peerInfo.peer_addr,brcAdr,6); //who to transmit this data
//copy of peerInfo.peer_addr to comunicate with brcAdr with lenght 6
peerInfo.channel=0; //set channel of wifi
peerInfo.encrypt=false; //not encrypt
esp_now_add_peer(&peerInfo); //add the peer peerInfo
}
void loop() {
//Read from the serial
while (!Serial.available()); //Wait for input
String message = Serial.readStringUntil('\n');
//send the message of the brcadr with a message length, +1 to add the end of /0.
esp_now_send(brcAdr,(uint8_t*)message.c_str(),message.length()+1);
}