Ciao
Non so' se anche a voi e' mai capitato.
Mi trovo con alcuni ESP32 che comunicano solo in una direzione.
Li sto' provando e sotto una tabella con le varie combinazioni
Sapete se ci sono dei parametri da adattare?
Inoltre per esempio questo
0xDA, 0x8A, 0xFC, 0xA0, 0x31, 0x30
non riceve dati altre schede ma riceve il segnale wifi da altra reti se lo uso per scannerizzare le reti wifi
Sotto lo sketch caricato su entrambe la coppia di schede con i relativi indirizzi
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp32/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <WiFi.h>
//#include <Wire.h>
// REPLACE WITH THE MAC Address of your receiver
//uint8_t broadcastAddress[] = {0xDA, 0x8A, 0xFC, 0x97, 0x71, 0x34};
// REPLACE WITH THE MAC Address of your receiver
//uint8_t broadcastAddress[] = {0xDA, 0x8A, 0xFC, 0xA0, 0x31, 0x30};
// REPLACE WITH THE MAC Address of your receiver
//uint8_t broadcastAddress[] = {0x58, 0xBF, 0x25, 0xE9, 0x70, 0xC8};
// REPLACE WITH THE MAC Address of your receiver
//uint8_t broadcastAddress[] = { 0xA0, 0xA3, 0xB3, 0x2F, 0xB4, 0xC0 };
// REPLACE WITH THE MAC Address of your receiver
//uint8_t broadcastAddress[] = {0x08, 0xD1, 0xF9, 0xE7, 0x5F, 0xD8};
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0xE4, 0x65, 0xB8, 0x20, 0x24, 0xBC};
// Define variables to store BME280 readings to be sent
float temperature;
float humidity;
float pressure;
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
float incomingPres;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp;
float hum;
float pres;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
esp_now_peer_info_t peerInfo;
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;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
getReadings();
// Set values to send
BME280Readings.temp = temperature;
BME280Readings.hum = humidity;
BME280Readings.pres = pressure;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&BME280Readings, sizeof(BME280Readings));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
updateDisplay();
delay(10000);
}
void getReadings() {
temperature = random(10, 50);
humidity = random(60, 100);
pressure = random(110, 150);
}
void updateDisplay() {
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS from");
Serial.println(broadcastAddress[0], HEX);
Serial.println(broadcastAddress[1], HEX);
Serial.println(broadcastAddress[2], HEX);
Serial.println(broadcastAddress[3], HEX);
Serial.println(broadcastAddress[4], HEX);
Serial.println(broadcastAddress[5], HEX);
Serial.print("Temperature: ");
Serial.print(incomingReadings.temp);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingReadings.hum);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(incomingReadings.pres);
Serial.println(" hPa");
Serial.println();
}
// 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 ? "Delivery Success" : "Delivery Fail");
/*
if (status == 0) {
success = "Delivery Success :)";
} else {
success = "Delivery Fail :(";
}
*/
}
// Callback when data is received
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
incomingPres = incomingReadings.pres;
}
