#include <WiFi.h>
#include <esp_now.h>
// MAC receiver
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x84, 0xFB, 0x90}; //different by other protocols to not make interference (?)
esp_now_peer_info_t peerInfo;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { //automatically passed
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) {
Serial.print("Message received: ");
char receivedString[len];
memcpy(receivedString, data, len); //from a string of byte to a string of char
Serial.println(String(receivedString));
}
void setup() {
Serial.begin(115200); //to ddedbug
WiFi.mode(WIFI_STA);
esp_now_init();
//send callback
esp_now_register_send_cb(OnDataSent); //what to do on sending, OnDataSent will be perfomed
//receive callback
esp_now_register_recv_cb(OnDataRecv); //what to do on receiveing, OnDataRecv will be perfomed
// Peer Registration
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0; //multiple (encrypted) channels
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
}
void loop() {
WiFi.setTxPower(WIFI_POWER_2dBm);
String message = "Hello!!!Hello!!!Hello!!!Hello!!!Hello!!!Hello!!!";
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1); //+1 for the terminator of strings
delay(500);
WiFi.setTxPower(WIFI_POWER_19_5dBm);
//String message = "Hello!!!Hello!!!Hello!!!Hello!!!Hello!!!Hello!!!";
esp_now_send(broadcastAddress, (uint8_t*)message.c_str(), message.length() + 1); //+1 for the terminator of strings
delay(1000);
}