-2

I'm coding for an ESP32 that receives data through MQTT to set its deep sleep time. The problem is that sometimes the ESP32 will not sleep for the requested time. It is fine for smaller times < 1hr, but when asking for > 4hrs it usually sleeps for 5 seconds.

My suspicion is that the ULL multiplication is not yielding the correct result.

Currently, the device receives a String with the number of seconds to deep sleep. I convert the string to ULL using strtoull. I'm setting the deep sleep time like this:

    #define MICRO_SEC_FACTOR (1000000ULL)
    uint64_t nap;

    nap = strtoull(args[1], NULL, 10);
    esp_sleep_enable_timer_wakeup((nap * MICRO_SEC_FACTOR);

Is there a way to ensure that the multiplication will yield the uint64_t that the function expects? I've tried casting but had the same results.

hardillb
  • 54,545
  • 11
  • 67
  • 105
DCrown
  • 57
  • 7
  • 2
    Please post the actual code which is having this problem by using copy/paste. Don't manually type it down when asking the question. – Lundin Mar 31 '23 at 06:55
  • 3
    How or why do you think it is not a uint64_t. That is not plausible. This is an X-Y problem. More likely the issue with the input. The code is not "real" in any case, so we should draw no conclusions from code that can't possibly compile. – Clifford Mar 31 '23 at 06:59
  • 1
    *"My suspicion ..."* - That is why debuggers were invented, to remove suspicion. I would re-write the code as `a = nap * MICRO_SEC_FACTOR; esp_sleep_enable_timer_wakeup(a);` and then run it through a debugger, to see what is going on. Please do that and then EDIT the question with the results (including the values you get). – virolino Mar 31 '23 at 07:09
  • 1
    Your multiplication is perfectly correct. Look at the docs for `esp_sleep_enable_timer_wakeup()` or look at the code for it. It probably has a limit, documented or not, in how long it can sleep. – TrentP Mar 31 '23 at 07:11
  • `for > 4hrs` Please test it with an exact hardcoded value `args[1] = "4something"`. I can't test "> 4hrs sleep" and `args[1]`, please post the real code. – KamilCuk Mar 31 '23 at 07:41

1 Answers1

1

Other than your code having syntax errors, nap * MICRO_SEC_FACTOR is guaranteed to be carried out using (at least) uint64_t arithmetic. And ULLONG_MAX in limits.h requires that a ULL suffixed constant to be able to hold values of at least 2^64 - 1.

Lundin
  • 195,001
  • 40
  • 254
  • 396