problem with setting ESP-NOW between two boards
Posted: Fri Jul 07, 2023 11:02 pm
hey. I am new to ESP-32 programming, so little bit stuck with a small (I hope) issue.
I am trying to make a simple peer-to-peer communication between two ESP-32 controllers. the first one is getting data from BME-280 and sending it to the second one.
very easy. a lot of examples in the internet, but I have a problem. the first board is sending the data only once. after that the callback function esp_now_register_send_cb(OnDataSent) is not called and data is not actually sending.
the code:
what I have in serial:
actually it sends only once. and after that just messages "sent with success" without sending the data (receiver doesn't get it) and "error sending data"...
anybody knows what could be the problem?
thanks!
I am trying to make a simple peer-to-peer communication between two ESP-32 controllers. the first one is getting data from BME-280 and sending it to the second one.
very easy. a lot of examples in the internet, but I have a problem. the first board is sending the data only once. after that the callback function esp_now_register_send_cb(OnDataSent) is not called and data is not actually sending.
the code:
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define I2C_SDA 33
#define I2C_SCL 32
TwoWire I2CSensors = TwoWire(0);
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxxx";
uint8_t broadcastAddress[] = {0x78, 0xE3, 0x6D, 0x16, 0xF6, 0x84};
Adafruit_BME280 bme;
// Define variables to store BME280 readings to be sent
float temperature;
float humidity;
float pressure;
//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;
esp_now_peer_info_t peerInfo;
// 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");
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
Serial.print("Request bytes received: ");
Serial.println(len);
}
void setup(){
// Init Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
uint32_t notConnectedCounter = 0;
Serial.print("Wifi connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
notConnectedCounter++;
if(notConnectedCounter > 50) {
Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();
}
}
Serial.println();
Serial.print("ESP32-CAM IP Address: ");
Serial.println(WiFi.localIP());
I2CSensors.begin(I2C_SDA, I2C_SCL, 100000);
if (!bme.begin(0x76, &I2CSensors))
{
Serial.println("Couldn't Find BME280 Sensor");
while(1);
}
else
{
Serial.println("BME280 Sensor Found");
}
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// 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;
}
if (esp_now_register_recv_cb(OnDataRecv) != ESP_OK) {
Serial.println("Failed to add OnDataRecv callback");
return;
}
if (esp_now_register_send_cb(OnDataSent) != ESP_OK) {
Serial.println("Failed to add OnDataSent callback");
return;
};
}
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");
}
delay(3000);
}
void getReadings(){
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = (bme.readPressure() / 100.0F);
}
Code: Select all
Wifi connecting....
ESP32-CAM IP Address: 192.168.0.89
BME280 Sensor Found
Sent with success
Last Packet Send Status: Delivery Fail
Sent with success
Sent with success
Sent with success
Sent with success
Sent with success
Error sending the data
Error sending the data
Error sending the data
Error sending the data
Error sending the data
Error sending the data
Error sending the data
anybody knows what could be the problem?
thanks!