Hi everyone,
I hope someone can help me here with my project.
First of all i am not really a programer or computer tech but i know electronics so please consider, in the other hand im doing my very best to learn ESP32 coding. That is why i bougth this courses to learn more one step at a time.
I am trying to create an animatronic show.
— First show is 5 animatronics will move for 1 mins once recieved a comand from the ESP32 sender. the command will send every 5 mins.
–Second show the other 5 animatronic will move for 1 mins once recieved a comand from the Same ESP sender. the command will be send every 10 mins.
— Third Show all the 10 animatronics will move for 1 mins once recieved a command from the Same ESP sender the command will be send every 15 mins.
First i need to learn how to send a command and here is my first try.
Practice makes perfect + experiment.
here’s an idited code copied from the couse and i studed it. but its not sending and recieving.
It complied but not working i dont know what is wrong.
hope someone can tell me what is my mistake.
thanks in advance guys.
regards,
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
— Sender code,
#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;
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
esp_now_peer_info_t peerInfo;
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() {
message_struct Start_show1;
message_struct Start_show2;
message_struct Start_show3;
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(message_struct));
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);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Reciever.
#include <esp_now.h>
#include <WiFi.h>
// Structure example to receive data
// Must match the sender structure
typedef struct message_struct {
bool Start_show = true;
bool Stop_show = false;
} message_struct;
// Create a struct_message called myData
message_struct myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print(“Byte received: “);
Serial.println(len);
Serial.print(“ture: “);
Serial.println(myData.Start_show);
Serial.print(“False: “);
Serial.println(myData.Stop_show);
Serial.println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}
Hi.
When you say that ESP-NOW is not working, what happens exactly?
What messages do you get on the serial monitor?
Can you provide more details about the errors?
Regards,
Sara
Hi Sara,
In the Serial monitor of the sender its showing, error sending the data.
In the Serial monitor of the receiver nothing show its not recieving data.
What i realy want to achieve in this experiment is that i want to Send a data structure to command the reciever to move. command like okey its time now you can move for one minute. But as for now I want to learn first how to send and to create a data structure.
Im not sure if that message structure is correct.
hope you can give me a suggestion or other way to achive my goal.
Regards,
keven
Hi.
I took a quick look at your sender code.
You just need to make a few changes.
Declare the peer object before the setup:
esp_now_peer_info_t peerInfo;
Declare the structures before the setup() and not in the loop():
message_struct Start_show1;
message_struct Start_show2;
message_struct Start_show3;
In the following link you can find the complete sender code with the changes:
https://gist.github.com/sarasantos/4beb4aeb321682f87508de2321d7b3fb
I hope this helps.
Regards,
Sara
Hi Sara,
Thank you very much it works now. I can proceed now to the next activity.
Regards,
keven.