DS_CAP_SYS_UPTIME - denis-stepanov/esp-ds-system GitHub Wiki

DS_CAP_SYS_UPTIME — Uptime Calculation

Description

This capability adds support for uptime calculation. The following methods are available:

time_t System::getUptime();               // Return uptime in seconds
String System::getUptimeStr();            // Return uptime as a string
time_t System::getBootTime();             // Return boot time in seconds from epoch
String System::getBootTimeStr();          // Return boot time string

Example of an uptime string: 9 days 02:29:43. Example of a boot string: 2020/10/11 20:54:19. Boot time functions are only available if time is enabled. Quite counter-intuitively, to count uptime per se a notion of time is not required.

Requires

Cooperates With

  • DS_CAP_SYS_TIME — if time is enabled, the calls getBootTime() and getBootTimeStr() become available;
  • DS_CAP_WEBSERVER — if web server is enabled, its "System Information" page will include "System Uptime" line with uptime information.

Conflicts With

None.

Usage

MySystem.h:

#define DS_CAP_SYS_UPTIME   // Enable system uptime counter

#include "System.h"         // System global definitions

sketch.ino:

#include "MySystem.h"

using namespace ds;

void setup() {
}

void loop() {
  auto uptime = System::getUptime();
  auto uptime_str = System::getUptimeStr();
  delay(1000);
}

Mandatory Calls

System::begin() Not required
System::update() Not required

Examples

Bugs

  • As per the Uptime-Library implementation, uptime calculation getUptime() (or getUptimeStr()) has to be called at least once every 49 days in order to compute uptime correctly. Since uptime is shown on "System Information" web page, it is probably good enough. If unsure, call the function yourself from time to time from your sketch. This might be changed in future versions of the library (issue #9);
  • Returned boot time seems to slightly drift with time. The reason for this has yet to be understood, but it looks like regular time correction is not being properly accounted for (issue #10).

Availability

Version 1.0 or later. Functions getUptime() and getBootTime() are available from version 1.2.

See Also

None.

⚠️ **GitHub.com Fallback** ⚠️