ESP8266: Strange behavior of ESP-NOW and deepSleep
Posted: Sat Sep 17, 2022 10:06 am
I would like to combine ESP-NOW and deepSleep to send data from a sensor to base controller. My code is:
The fragment of output on serial monitor is:
17:04:53.651 -> Packet to:18:fe:34:d3:b0:4a Send status: 1 Delivery fail
But, when instruction ESP.deepSleep(60e6); is commented the output is:
15:52:42.622 -> Packet to:18:fe:34:d3:b0:4a send status: 0 Delivery success
It is interesting that in both cases the receiver gets data properly.
Any suggestions?
Kind regards.
Pop
Code: Select all
/********************************************************************
* ESP-NOW Sender using ESP8266
* ESP8266 Deep sleep mode, GPIO 16 connected to the RESET (RST) pin
********************************************************************/
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Wire.h>
// RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x18, 0xFE, 0x34, 0xD3, 0xB0, 0x4A}; // ESP8266 No.2: 18:FE:34:D3:B0:4A
// Structure example to send data. Must match the receiver structure
typedef struct struct_message {
float Temp;
float Pres;
float Hum;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus)
{
char macStr[18];
Serial.print("Packet to:");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.print(macStr);
Serial.print(" Send status: ");
Serial.print(sendStatus);
if (sendStatus == 0)
{
Serial.println(" Delivery success");
}
else
{
Serial.println(" 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() != 0)
{
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_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
//esp_now_add_peer(broadcastAddress2, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
// Set values to send
myData.Temp = 25.1;
myData.Pres = 1012.5;
myData.Hum = 48.7;
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
// Deep sleep mode for 60 seconds, the ESP8266 wakes up by itself
Serial.println("Going into deep sleep mode for 60 seconds");
ESP.deepSleep(60e6);
}
void loop() {}
The fragment of output on serial monitor is:
17:04:53.651 -> Packet to:18:fe:34:d3:b0:4a Send status: 1 Delivery fail
But, when instruction ESP.deepSleep(60e6); is commented the output is:
15:52:42.622 -> Packet to:18:fe:34:d3:b0:4a send status: 0 Delivery success
It is interesting that in both cases the receiver gets data properly.
Any suggestions?
Kind regards.
Pop