-
-
Save sarasantos/4beb4aeb321682f87508de2321d7b3fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <esp_now.h> | |
#include <WiFi.h> | |
// REPLACE WITH YOUR ESP RECEIVER’S MAC ADDRESS | |
uint8_t broadcastAddress1[] = {0xE8, 0x31, 0xCD, 0xD6, 0xE9, 0x20}; | |
uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x78}; | |
uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04}; | |
typedef struct message_struct { | |
bool Start_show = true; | |
bool Stop_show = false; | |
} message_struct; | |
message_struct Start_show1; | |
message_struct Start_show2; | |
message_struct Start_show3; | |
esp_now_peer_info_t peerInfo; | |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
char macStr[18]; | |
Serial.print("Packet to: "); | |
// Copies the sender mac address to a string | |
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", | |
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); | |
Serial.print(macStr); | |
Serial.print(" send status:\t"); | |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
WiFi.mode(WIFI_STA); | |
if (esp_now_init() != ESP_OK) { | |
Serial.println("Error initializing ESP-NOW"); | |
return; | |
} | |
esp_now_register_send_cb(OnDataSent); | |
// register peer | |
peerInfo.channel = 0; | |
peerInfo.encrypt = false; | |
memcpy(peerInfo.peer_addr, broadcastAddress1, 6); | |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
Serial.println("Failed to add peer"); | |
return; | |
} | |
memcpy(peerInfo.peer_addr, broadcastAddress2, 6); | |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
Serial.println("Failed to add peer"); | |
return; | |
} | |
memcpy(peerInfo.peer_addr, broadcastAddress3, 6); | |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
Serial.println("Failed to add peer"); | |
return; | |
} | |
} | |
void loop() { | |
Start_show1.Start_show = true; | |
Start_show1.Stop_show = false; | |
Start_show2.Start_show = true; | |
Start_show2.Stop_show = false; | |
Start_show3.Start_show = true; | |
Start_show3.Stop_show = false; | |
esp_err_t result1 = esp_now_send( | |
broadcastAddress1, | |
(uint8_t *) &Start_show1, | |
sizeof(Start_show1)); | |
if (result1 == ESP_OK) { | |
Serial.println("Sent with success"); | |
} | |
else { | |
Serial.println("Error sending the data"); | |
} | |
delay(500); | |
esp_err_t result2 = esp_now_send( | |
broadcastAddress2, | |
(uint8_t *) &Start_show2, | |
sizeof(message_struct)); | |
if (result2 == ESP_OK) { | |
Serial.println("Sent with success"); | |
} | |
else { | |
Serial.println("Error sending the data"); | |
} | |
delay(500); | |
esp_err_t result3 = esp_now_send( | |
broadcastAddress3, | |
(uint8_t *) &Start_show3, | |
sizeof(message_struct)); | |
if (result3 == ESP_OK) { | |
Serial.println("Sent with success"); | |
} | |
else { | |
Serial.println("Error sending the data"); | |
} | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment