Bonsoir;
Je ne suis pas un spécialiste code, donc j'ai utilisé ce que d'autre savent mieux faire que moi.
J'ai sur ce coup "NON" utilisé le Chat qui ne me disait rien de bien, mais j'ai utilisé ce que Ruis et Sara Santos écrivent dans leurs pdf (en vente en ligne) "Learn ESP32 3rd édition".
J'ai opté pour un modéle rapide selon moi,
"ESP-NOW One-way communication"
(One ESP32 board sending the same or different commands to different ESP32
boards.).
J'ai adapté le code pour envoyer de l'ESP2 à l'ESP1 la valeur analogique du potentiométre cablé en broche 34)
Ca donne cela
Emetteur ESP2
Dans "broadcastAddress, remplacer les XX par l'adresse de votre ESP1
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX};
esp_now_peer_info_t peerInfo;
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
//char a[32];
int b;
//float c;
//bool d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Send Success" : "Send Fail");
}
void setup() {
// Init 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 Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
pinMode(34, INPUT);
}
void loop() {
// Set values to send
//strcpy(myData.a, "THIS IS A CHAR");
//myData.b = random(1,20);
myData.b = analogRead(34);
//myData.c = 1.2;
//myData.d = false;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData,
sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(200);
}
Recepteur ESP1
#include <esp_now.h>
#include <WiFi.h>
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
//char a[32];
int b;
//float c;
//bool d;
} struct_message;
// Create a struct_message called myData
struct_message 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("Bytes received: ");
Serial.println(len);
//Serial.print("Char: ");
//Serial.println(myData.a);
Serial.print("Int: ");
Serial.println(myData.b);
//Serial.print("Float: ");
//Serial.println(myData.c);
//Serial.print("Bool: ");
//Serial.println(myData.d);
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(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
}
Chez moi, ca marche, c'est une base de départ il me semble
PS, pour récupérer vos adresses MAC:
#include <WiFi.h>
#include <esp_wifi.h>
void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.STA.begin();
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}
void loop(){
}
C'était ma contribution du jour.
PS: au passage, je recommande pour les "non codeurs" Ruis et Sara Santos "Learn ESP32 3rd édition"