Hello the arduino community
I am new in the programming stuff and I have a project. I have 2 water tanks. Each one has an ESP32 live mini kit and a water level sensor. These 2 ESP (let call them transmitter 1 and 2) send the water level reading value to another ESP32 (the receiver) via ESP now.
The water level reading is working fine but I have an issue with the data transmission via ESP now. I reproduce my system on my test bench so you will only see the "problem" in my code.
Every minute, the receiver gives power to the transmitter 1 (GPIO output from receiver command a mosfet to give the power to the transmiter 1). The transmitter send an id and a value to the receiver via ESP now. The receiver switch off the power from transmitter 1 and gives power to transmitter 2. Transmitter 2 will do the same as transmitter one on the second water tank.
Here is the code from the receiver:
#include <esp_now.h>
#include <WiFi.h>
#include "driver/rtc_io.h"
#define uS_TO_S_FACTOR 1000000
//#define TIME_TO_SLEEP 900
#define TIME_TO_SLEEP 60
RTC_DATA_ATTR int Message= 1;
const unsigned int Remorque= 23; //mise ene marche capteur remorque
const unsigned int Cuves_haut= 5; //mise en marche capteur cuves haut
int Niveau1;
int Niveau2;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int id;
int b;
} 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));
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
pinMode(Remorque, OUTPUT);
pinMode(Cuves_haut, OUTPUT);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //sleep time
// 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() {
// Niveau Remorque
digitalWrite(Remorque, LOW);
delayMicroseconds(100);
digitalWrite(Remorque, HIGH);
delay(2500);
digitalWrite(Remorque, LOW);
Serial.println(myData.id);
Serial.println(myData.b);
if (myData.id == 1) {
Niveau1 = myData.b;
}
else {
Niveau1 = 0;
}
// Niveau Cuves haut
digitalWrite(Cuves_haut, LOW);
delayMicroseconds(100);
digitalWrite(Cuves_haut, HIGH);
delay(2500);
digitalWrite(Cuves_haut, LOW);
Serial.println(myData.id);
Serial.println(myData.b);
if (myData.id == 2) {
Niveau2 = myData.b;
}
else {
Niveau2 = 0;
}
// Envoi donnees
if (Message == 1) {
Serial.print("1|");
Serial.print(Niveau1);
Serial.print("|");
Serial.println(Niveau2);
Message= 2;
delay(3);
}
else {
Serial.print("2|");
Serial.print(Niveau1);
Serial.print("|");
Serial.println(Niveau2);
Message= 1;
delay(3);
}
esp_light_sleep_start();
}
This is the code for transmitter 1:
#include <esp_now.h>
#include <WiFi.h>
// RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xCC, 0xDB, 0xA7, 0x97, 0x8D, 0x48};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id;
int b;
} struct_message;
// Create a struct_message called myData
struct_message myData;
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");
}
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(esp_now_send_cb_t(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;
}
}
void loop() {
myData.id = 1;
myData.b = 25 ;
Serial.println(myData.b);
// 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");
}
// esp_deep_sleep_start();
}
This is the code for transmitter 2:
#include <esp_now.h>
#include <WiFi.h>
// RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xCC, 0xDB, 0xA7, 0x97, 0x8D, 0x48};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id;
int b;
} struct_message;
// Create a struct_message called myData
struct_message myData;
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");
}
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(esp_now_send_cb_t(OnDataSent));
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 1;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
myData.id = 2;
myData.b = 10 ;
Serial.println(myData.b);
// 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");
}
// esp_deep_sleep_start();
}
At receiver power up, the system works as excpected. I have on the serial monitor the following output then the receiver goes in light sleep for 1 minute:
1 ------> ID from transmitter 1
25-----> Fixed value. Will be the water level later)
2-------> ID from transmitter 2
10-----> Fixed value. Will be the water level later)
1|25|10----> desired output format
When the receiver wakes up after 1 minute, I get the following in the serial monitor
2
10
2
10
2|0|10
There is no way that the transmitter 1 send 2 as ID. Transmitter 2 has no power when transmitter 1 is on and vice versa.
I made several test. If I don't use any sleep mode or if I use deep sleep mode, everything works as excpected. I have only the issue when I use light sleep
Any idea on what cause this issue? It is like the ESP now link is not configured properly after light sleep wake up.