使用 ESP32 精确睡眠时间

问题描述 投票:0回答:1

我想编写一段代码,每10分钟唤醒一次ESP32。然后代码执行一些任务,计算距离下一轮 10 分钟的时间,然后让 ESP32 进入睡眠状态。最后一部分代码如下:

#include "time.h"

time_t currenttime;
long int sleeptime;

currenttime = time(nullptr);
sleeptime = (600 - static_cast<int> (currenttime) % 600);  // time to the next round 10 minutes
if (sleeptime < 120) sleeptime = sleeptime + 600;          // skip if next round 10 minutes is less than 2 minutes away
esp_sleep_enable_timer_wakeup(1000000 * sleeptime);
esp_deep_sleep_start();

代码在 99% 的情况下都能正常工作,但偶尔 ESP32 只会进入睡眠模式最多几秒钟,尽管根据代码,最短睡眠时间应至少为 120 秒。这段代码一定有问题,但我不知道是什么问题。原因之一是设备没有连接USB,无法读取串口通讯。

你能告诉我出了什么问题吗?

esp32
1个回答
0
投票

也许你想做:

sleeptime = (600 - (static_cast<int> (currenttime) % 600));  // time to the next round 10 minutes
© www.soinside.com 2019 - 2024. All rights reserved.