2

I'm doing wireless sensor node using esp32 (slave) and rf24l01 module. My next step is to put my slave in sleep mode (maybe deep sleep). Can I use deep sleep for my project?

They said

everything stored in that memory is wiped out and cannot be accessed.

So is all my void setup() code wiped out? Or just my pack0.temp, humid, soil is wiped out?

My code is attached below

struct package0
{
  float temperature = 0;
  float humidity = 0;
  int soil = 0;
};
typedef struct package0 Package0;
Package0 pack0;
/**********************************/
/**************RF24****************/
  RF24 radio(25,26);
  RF24Network network(radio);
  const uint16_t this_node = 01;
  const uint16_t master00 = 00;
  const unsigned long interval = 3000;

/**********************************/
void setup() {
  dht.begin();
  radio.begin();
  network.begin(90, this_node);
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_250KBPS);
}

void loop() {
  // put your main code here, to run repeatedly:
  network.update();
  unsigned long now = millis();
  if (now - last_sent >= interval)
  {
    last_sent = now;
    send();
  }
}
void send()
{
  pack0.humidity = dht.readHumidity();
  pack0.temperature = dht.readTemperature();
  pack0.soil = map(analogRead(SOILPIN), 0, 4096, 100, 0);//convert to percentage
  if (isnan(pack0.humidity) || isnan(pack0.temperature))
  {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  RF24NetworkHeader header(master00);
  bool ok = network.write(header, &pack0, sizeof(pack0));
}
2
  • "They said..." Who, do you have a documentation link? If so, update your question.
    – Mathieu
    Jun 25, 2019 at 15:05
  • @Quang: Does John Romkey's answer below address your issue? If so, you should mark it as the accepted answer. At the very least, you could give him some feedback. Jun 29, 2019 at 17:22

1 Answer 1

10

When the ESP32 enters deep sleep, it turns off the processor that's running your code. The contents of memory and the current state of the processor are lost. It costs power to maintain the contents of its memory and the CPU state, and the point of deep sleep is to save as much power as possible, so it stops powering these things.

So when it restarts out of deep sleep it's as if it just powered up. Your setup() function will run again and will need to do any initialization again.

There are a couple of ways to preserve state across sleep cycles.

Obviously you can store data in flash memory using EEPROM or SPIFFS. Writing to flash is slow and costs a lot of power, so this isn't great if you're running off a battery.

You can also store data in the static RAM that's part of the real-time-clock (RTC). This RAM is built into the ESP32 and is maintained during deep sleep. Its contents will be lost or cleared when the ESP32 loses power or is flashed.

You can declare a variable to live in the RTC RAM using RTC_DATA_ATTR. For instance:

RTC_DATA_ATTR unsigned wakeups;

void setup() {
  wakeups++;

  Serial.begin(115200);
  Serial.printf("%u wakeups\n", wakeups);

  // do other stuff and enter deep sleep
}

There's only 8KB of static RAM so you can't store huge amounts of data there.

You also need to be careful using it. Storing complex C++ objects in it will almost certainly not work correctly across deep sleep restarts. Storing pointers to data (like a char* pointing to a C string) will not work because the data the pointer pointed to will be lost after restarting from deep sleep.

How deep sleep affects any electronics connected to the ESP32 is difficult to predict. The ESP32 will stop powering its GPIO lines during deep sleep. Whether the devices remain powered and how they react to the GPIO lines floating depends on the device. If they remain powered they may retain their state from the previous cycle. It really depends on the device and the circuitry.

This article is a good tutorial on deep sleep and has more information.

3
  • It is mentioned here that after exiting deep sleep the setup is re-ran, is there any official source that could corroborate that? In case of having multiple peripherals it would be great to be able to make sure that after exiting deeps sleep all of them are initialized properly and code that is most often include on top of the file is available.
    – Kaszanas
    Apr 14 at 15:13
  • @Kaszanas The ESP32 data sheet and technical reference documents make it clear how deep sleep operates. The position of code in the file is largely irrelevant. If you're using Arduino, setup() can appear anywhere in the code and will still be called first. Your program will run as if this were its first run (from its perspective it is), all of its code will always be available.
    – romkey
    Apr 14 at 16:06
  • Thank you for clarification. This was confusing for me at first because there are different types of sleep and they handle differently.
    – Kaszanas
    Apr 15 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.