简体   繁体   中英

Is void setup() code wiped out when deep sleep is used in esp32

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));
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM