ESP32 Deep Sleep

Ich spiele ein wenig mit deep sleep und mein ESP32 schläft immer so 60 Sekunden egal
was ich al Wert eintrage.
esp_sleep_enable_timer_wakeup(10);

void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  print_wakeup_reason();

  /*
  First we configure the wake up source
  We set our ESP32 to wake up every 5 seconds
  */
  esp_sleep_enable_timer_wakeup(10);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  " Seconds");

  /*
  Next we decide what all peripherals to shut down/keep on
  By default, ESP32 will automatically power down the peripherals
  not needed by the wakeup source, but if you want to be a poweruser
  this is for you. Read in detail at the API docs
  http://esp-idf.readthedocs.io/en/latest/api-reference/system/deep_sleep.html
  Left the line commented as an example of how to configure peripherals.
  The line below turns off all RTC peripherals in deep sleep.
  */
  //esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
  //Serial.println("Configured all RTC Peripherals to be powered down in sleep");

  /*
  Now that we have setup a wake cause and if needed setup the
  peripherals state in deep sleep, we can now start going to
  deep sleep.
  In the case that no wake up sources were provided but deep
  sleep was started, it will sleep forever unless hardware
  reset occurs.
  */
  Serial.println("Going to sleep now");
  delay(1000);
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){
  //This is not going to be called
}

Irreführung:
10µs sind keine 5 Minuten sondern 500 tausend mal weniger.

Dein Code ist nicht testbar.
Wirft Errors.

Ich weiß was Du meinst ich programmiere mit PlatformIO
esp_sleep_enable_timer_wakeup(10);
ist der gleiche Interval wie
esp_sleep_enable_timer_wakeup(10e6);
Was könnte das Problem sein ?

Das wage ich zu bezweifeln
10e6 bedeutet eine 10 mit 6 nullen hinten dran
Wenn du es nicht glaubst dann lass es einfach mal auf dem seriellen Monitor ausgben
und dann kannst du ja spaßeshalber doch mal 10e6 eintragen

Ansonsten hatte ich vor längerer Zeit mal einen thread wo dann verschiedene Code-Versionen gepostet wurden

/*
  https://forum.arduino.cc/t/how-can-i-force-an-esp32-that-awakes-from-deepsleep-to-re-sync-with-a-ntp-server/1116313/5

  call NTP even after a deepsleep periode
  2023-04-17 by noiasca
*/

#include <WiFi.h>

//#include <credentials.h>                           // that's just if you have already one file with your credentials

#ifndef STASSID
#define STASSID "your SSID"                            // set your SSID
#define STAPSK  "your password"                        // set your wifi password
#endif


#define MY_NTP_SERVER "at.pool.ntp.org"                // choose the best fitting NTP server pool for your country
//#define MY_NTP_SERVER "fritz.box"               
#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03"         // Berlin, Vienna, Rom, ... https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv

RTC_DATA_ATTR int bootCounter = 0;                     // test variable in RTC memory
const uint32_t sleeptime = 30 * 1000UL * 1000UL ;      // in microseconds


// callback to check if NTP was called
#include "esp_sntp.h"
void cbSyncTime(struct timeval *tv) { // callback function to show when NTP was synchronized
  Serial.println("callback NTP time synched");
}


void firstStart() {
  Serial.println("firstStart");
  WiFi.mode(WIFI_STA);
  WiFi.begin(STASSID, STAPSK);
  Serial.println("trying to connect to SSID #");
  Serial.print(STASSID);
  Serial.println("#");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("connected IP: ");
  Serial.println(WiFi.localIP());
  struct tm local;
  configTzTime(MY_TZ, MY_NTP_SERVER);
  Serial.println("before getLocalTime(&local, 10000)");
  getLocalTime(&local, 10000);
  Serial.println("after getLocalTime(&local, 10000)");
}


void setup() {
  Serial.begin(115200);
  Serial.println("\nsetup");
  sntp_set_time_sync_notification_cb(cbSyncTime);  // optional/just for test: set a Callback function for time synchronization notification
  esp_sleep_wakeup_cause_t wakeup_cause;
  setenv("TZ", MY_TZ, 1);                          // Set environment variable with your time zone - causes NTP call
  tzset();
  bootCounter++;
  Serial.println("Startup: " + String(bootCounter));
  wakeup_cause = esp_sleep_get_wakeup_cause();     // get wakeup
  Serial.print("wakeup_cause="); 
  Serial.println(wakeup_cause);
  if (wakeup_cause != 3) 
    firstStart();             // when wakeup by reset
}


void loop() {
  Serial.println("top of loop delay(11000);");
  delay(11000);               // consume time ... make it possible that NTP has a chance to get called in the background!!!
  tm local;
  getLocalTime(&local);
  Serial.println(&local, "%Y-%m-%d %H:%M:%S");
  Serial.print("goto Deepsleep for ");
  Serial.print(sleeptime / 1000000);
  Serial.print(" seconds");
  
  esp_sleep_enable_timer_wakeup(sleeptime);
  esp_deep_sleep_start();
}

Diese Aussage verstehe ich nicht.
In der Doku steht:

esp_err_t esp_sleep_enable_timer_wakeup(uint64_t time_in_us)
Enable wakeup by timer.

Parameters
time_in_us -- time before wakeup, in microseconds

Returns
ESP_OK on success
ESP_ERR_INVALID_ARG if value is out of range (TBD)

Wie kann da 10 das gleiche seine wie 10^6?

PlatformIO macht das sicher nicht gleich….

Anhand der Kommentare im Text vermute ich als Quelle mal randomnerdtutorials ... :sunglasses:

Den Code allerdings verstümmelt und so nicht lauffähig.
In Deinem Beispiel gibt es weder print_wakeup_reason() noch TIME_TO_SLEEP noch bootCount.

Der Originalcode funktioniert einwandfrei, hier mit

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  20        /* Time ESP32 will go to sleep (in seconds) */

11:20:12.449 -> Wakeup caused by timer
11:20:12.449 -> Setup ESP32 to sleep for every 20 Seconds
11:20:12.449 -> Going to sleep now
11:20:33.311 -> ets Jul 29 2019 12:21:46
11:20:33.311 ->
11:20:33.311 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
11:20:33.311 -> configsip: 0, SPIWP:0xee
11:20:33.311 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
11:20:33.311 -> mode:DIO, clock div:1
11:20:33.358 -> load:0x3fff0030,len:1344
11:20:33.358 -> load:0x40078000,len:13964
11:20:33.358 -> load:0x40080400,len:3600
11:20:33.358 -> entry 0x400805f0
11:20:34.395 -> Boot number: 3
11:20:34.395 -> Wakeup caused by timer

cu

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