Getting the uptime

StefanRvO
Posts: 10
Joined: Sun Mar 06, 2016 9:25 pm

Getting the uptime

Postby StefanRvO » Thu Mar 16, 2017 8:39 pm

Hi
Is it possible to find the time since last boot?
I know that it is possible to use gettimeofday() in some cases, but this (obviously) does not work if sntp is enabled. Is there any other way to find the uptime?

Regards
Stefan

Hans Dorn
Posts: 62
Joined: Tue Feb 21, 2017 2:21 am

Re: Getting the uptime

Postby Hans Dorn » Thu Mar 16, 2017 8:55 pm

Hi

xTaskGetTickCount will probably do the trick.

P.S: The tickcount will wrap every 49 days I guess. You'll have to check it periodically and adjust your uptime calculation.

meowsqueak
Posts: 133
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Getting the uptime

Postby meowsqueak » Wed Mar 14, 2018 5:08 am

Hans Dorn wrote:xTaskGetTickCount will probably do the trick.

P.S: The tickcount will wrap every 49 days I guess. You'll have to check it periodically and adjust your uptime calculation.
With a default tick rate of 100 Hz and TickType_t being a uint32_t, wouldn't it overflow every ~497 days?

Code: Select all

In [7]: 2**32 / 100 / 60 / 60 / 24.
Out[7]: 497.10269629629624

ESP_igrr
Posts: 1497
Joined: Tue Dec 01, 2015 8:37 am

Re: Getting the uptime

Postby ESP_igrr » Wed Mar 14, 2018 5:50 am

esp_timer_get_time returns 64-bit time since startup, in microseconds.

meowsqueak
Posts: 133
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Getting the uptime

Postby meowsqueak » Wed Mar 14, 2018 6:25 am

ESP_igrr wrote:esp_timer_get_time returns 64-bit time since startup, in microseconds.
Oh, now that IS useful! - now I wish I hadn't just spent the last hour implementing the exact same thing :)

By my calculations that should last for about 584,000 years before overflow. Should be sufficient for most IoT applications.

EDIT: oh, it's signed, so I guess that's only 292,000 years then. Pity.

shelladdicted
Posts: 3
Joined: Fri Dec 29, 2017 11:32 pm

Re: Getting the uptime

Postby shelladdicted » Fri Jun 08, 2018 4:21 pm

meowsqueak wrote:
ESP_igrr wrote:esp_timer_get_time returns 64-bit time since startup, in microseconds.
Oh, now that IS useful! - now I wish I hadn't just spent the last hour implementing the exact same thing :)

By my calculations that should last for about 584,000 years before overflow. Should be sufficient for most IoT applications.

EDIT: oh, it's signed, so I guess that's only 292,000 years then. Pity.
esp_timer_get_time() returns an int64_t so [-2³²,+2³²] is the max.
in a day there are 8.64*10^10 uS and is bigger than 2³² so esp_timer_get_time() will overflow in less than one day
how can i handle more than one day?

ESP_Sprite
Posts: 2761
Joined: Thu Nov 26, 2015 4:08 am

Re: Getting the uptime

Postby ESP_Sprite » Sat Jun 09, 2018 4:52 am

shelladdicted wrote: esp_timer_get_time() returns an int64_t so [-2³²,+2³²] is the max.
Let me correct you there: esp_timer_get_time() returns an int64_t so [-2^63,+2^63-1] is the max.

meowsqueak
Posts: 133
Joined: Thu Jun 15, 2017 4:54 am
Location: New Zealand

Re: Getting the uptime

Postby meowsqueak » Sat Jun 09, 2018 7:24 am

shelladdicted wrote: esp_timer_get_time() returns an int64_t so [-2³²,+2³²] is the max.
As ESP_Sprite said, you've got your math wrong. It will overflow after 292,000 years. Should be long enough for most IoT apps and even some enterprise applications.

User avatar
HermannSW
Posts: 89
Joined: Fri Oct 27, 2017 6:58 am
Location: Eberbach, Germany
Contact:

Re: Getting the uptime

Postby HermannSW » Wed Nov 14, 2018 9:04 am

Interesting. I looked into ESP32 (and ESP8266) overflow for MicroPython utime module ticks_ms() and ticks_us() functions:
https://forum.micropython.org/viewtopic ... 855#p31855

Both do return uint32_t, so overflow after 71 minutes / 49 days.
Interestingly ESP8266 MicroPython implements high 32bit of 64bit counter and allows for same overflows, although ESP8266 system_get_time() returns uint32_t only.

Ritesh
Posts: 1074
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Getting the uptime

Postby Ritesh » Mon Nov 19, 2018 7:35 pm

ESP_igrr wrote:
Wed Mar 14, 2018 5:50 am
esp_timer_get_time returns 64-bit time since startup, in microseconds.
From which ESP32 IDF, It has been supported?
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: No registered users and 13 guests