ESPNOW fails to work with deepsleep

I have a project with three nodemcu boards and temp sensors (DS18B20) that send the temps to a receiving box, also a nodemcu, that then uploads the three temps to Thingspeak every 30 minutes. I am using ESPNOW to send the temps from the three senders to the receiver. All are powered with 5v phone chargers. I have found in previous projects that the temp sensors will be elevated by a few degrees if they are mounted in the same box as the nodemcu. I have solved this by putting the nodemcu into deepsleep in between readings. I have attempted to do the same thing with this project but cannot communicate with ESPNOW between the sending units and the receiver after the nodemcu comes out of deepsleep. The receiver unit is always on, only the sending units are put into deepsleep.

When monitoring the sending unit there will be a line of gibberish then the temp reading then a error message "Last Packet Send Status: Delivery Failed". If I use a delay instead of the deepsleep everything works just fine. I have tried different nodemcu boards for the sending unit with the same result. I attached the code for one of the sending units. They are all the same just with a different ID. Any thoughts or suggestions would be appreciated. Thanks.

#include <ESP8266WiFi.h>
#include <espnow.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// MAC Address of receiver
uint8_t broadcastAddress[] = {0xC8, 0x2B, 0x96, 0x20, 0x2D, 0x1C};

//Setup DS18B20 sensor
// Data wire is connteced to pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature DS18B20(&oneWire);

//ID Number for board
int id = 1;

// Define variables to store DHT readings to be sent
float t;

// Variable to store if sending data was successful
String success;

//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
  int id;
  float temp;
} struct_message;

// Create a struct_message called Room1 to hold sensor readings
struct_message myData;

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0) {
    Serial.println("Delivery success");
  }
  else {
    Serial.println("Delivery fail");
  }
}

void getReadings() {
  // Measure Temperature
  DS18B20.requestTemperatures();
  t = DS18B20.getTempFByIndex(0);
  delay(1000);
  Serial.println("  ");
  Serial.print("Room 1 temperature: ");
  Serial.println(t);
  delay(100);
  //DS18B20 Error notification
  if (isnan(t)) {
    Serial.println("Failed to read from DS18B20 sensor!");
    return;
  }
}

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  Serial.setTimeout(2000);

  //Wait for serial to intialize
  while (!Serial) {}

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Set ESP-NOW Role
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);

  // Once ESPNow is successfully Init, we will register for Send CB to get the status of 
  //transmitted packet
  esp_now_register_send_cb(OnDataSent);

  Serial.println("Register CB");

  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {
  //Get DS18B20 readings
  getReadings();

  //Set values to send
  myData.id = id;
  myData.temp = t;

  // Send message via ESP-NOW
  esp_now_send(0, (uint8_t *) &myData, sizeof(myData));

  Serial.println();
  Serial.println("Entering deep sleep for 10 seconds");
  ESP.deepSleep(0.16 * 60 * 1.0e6, WAKE_RF_DEFAULT);//Will read temp successfully but will not send 
  //to receiving unit

  //delay(0.16 * 60 * 1000); //Will read temp successfully AND will successfully send to 
  //receiving unit
}

Try move ESP.deepSleep in OnDataSent

1 Like

Thank you for the reply. I think you were referring to placing deesleep within the OnDataSent function like this:

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0) {
    Serial.println("Delivery success");
  }
  else {
    Serial.println("Delivery fail");
  }
  ESP.deepSleep(0.16 * 60 * 1.0e6, WAKE_RF_DEFAULT);
}

I tried this and unfortunately it does the same thing.

I apologize. That did fix the problem! I did not have the receiver powered up at the time. The sending and receiving units are working well with deepsleep. Thanks so much for the help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.