(solved) ESP Now : Hello World not working
Posted: Sat Dec 28, 2024 2:17 pm
Hi,
I am learning how to use Esp Now. I try to send a simple message from an Esp32 to another but it seems the message is not received.
The two ESP32s are S3-wroom1.
Here is the code of the sender :
Here is the code of the receiver :
Here is the Serial Monitor of the sender :
Here is the Serial Monitor of the receiver :
I don't find what's wrong. Can anyone help please ?
I am learning how to use Esp Now. I try to send a simple message from an Esp32 to another but it seems the message is not received.
The two ESP32s are S3-wroom1.
Here is the code of the sender :
Code: Select all
// Librairies pour ESP Now et le wifi
#include <esp_now.h>
#include <WiFi.h>
// L'adresse Mac du récepteur
uint8_t MacAdress[] = {0xa0, 0x85, 0xe3, 0xe7, 0x44, 0x58};
// Définition de la structure (identique au récepteur)
typedef struct Message {
int Compteur; // La valeur du potentiomètre
char* Hello; // La valeur du bouton poussoir
};
char* Hell = "Hello world ";
int Compteu = 0;
Message Mon_message; //crée une variable de type Message
esp_now_peer_info_t peerInfo; // Crée une variable pour stocker les informations du partage
void OnDataSent(const uint8_t *MacAdress, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
} // Cette fonction permet d'afficher si le message a été correctement livré
void setup() {
Serial.begin(115200); // Initialise le moniteur serial
WiFi.mode(WIFI_STA); // Active le wifi
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return; // affiche les éventuels problèmes d'initialisation d'ESP now
}
esp_now_register_send_cb(OnDataSent); // permet d'envoyer l'accusé de réception
memcpy(peerInfo.peer_addr, MacAdress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false; // Ajoute un pair
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
} // Affiche s'il y a eu un problème lors de l'ajout du pair
}
void loop() {
Compteu++;
Mon_message.Hello = Hell;
Mon_message.Compteur = Compteu;
esp_err_t result = esp_now_send(MacAdress, (uint8_t *) &Mon_message, sizeof(Mon_message)); // Envoie les données de Mon_message
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
} // Vérifie si l'envoi a fonctionné
Serial.print(Mon_message.Hello);
Serial.println(Mon_message.Compteur);
delay(1000);
}
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
// Définition de la structure (identique à l'émetteur)
typedef struct Message {
int Compteur; // La valeur du potentiomètre
char* Hello; // La valeur du bouton poussoir
};
Message Mon_message; //crée une variable de type Message
// Fonction de réception des données
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
char macStr[18];
// Copier les données reçues dans Message
memcpy(&Mon_message, incomingData, sizeof(Mon_message));
Serial.print(Mon_message.Hello);
Serial.println(Mon_message.Compteur);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Mode station pour l'ESP32
// Initialisation de ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Erreur d'initialisation d'ESP-NOW");
return;
}
// Enregistrer la fonction de réception
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
Serial.print(Mon_message.Hello);
Serial.println(Mon_message.Compteur);
}
void loop() {
// On affiche les données reçues toutes les 500 ms
delay(1000); // Attente de 500 ms
}
Code: Select all
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce2820,len:0x1188
load:0x403c8700,len:0x4
load:0x403c8704,len:0xbf0
load:0x403cb700,len:0x30e4
entry 0x403c88ac
E (193) esp_core_dump_flash: Core dump data check failed:
Calculated Sent with success
Hello world 1
Last Packet Send Status: Delivery Fail
Sent with success
Hello world 2
Last Packet Send Status: Delivery Fail
Sent with success
Hello world 3
Last Packet Send Status: Delivery Fail
Sent with success
Hello world 4
Code: Select all
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x0 (DOWNLOAD(USB/UART0))
waiting for download