Micropython and EPOCH
Asked Answered
H

2

1

I want to send data in the Graphite database with a Esp32 and for that I need the timestamp which must be in Epoch format.

It's been 2 hours that I search on the Internet and I can not find anything.

I tried machine.rtc, utime, time ...

If you have an example I am a taker.

Thank you

ntptime.settime()

rtc = machine.RTC()

print("Time:%s" %str(rtc.datetime()))

Sorry to bother you but I start under MicroPython

Hitt answered 22/7, 2019 at 23:9 Comment(0)
T
4

Many embedded ports, including MicroPython, use Epoch time of January 1, 2000. This is 946,684,800 seconds later than UNIX Epoch time of January 1, 1970. If you first set your RTC using NTP, you will be able to read MicroPython's Epoch in seconds. It sounds like your're on a network so it should be easy to do. Assuming you're already connected to WiFi, here's how to set the RTC time:

import ntptime

# General call to set RTC shown
# Note that when setting datetime, Weekday is ignored. MicroPython uses its internal calendar.
# machine.RTC().datetime((yyyy, mm, dd, Weekday, hh, mm, ss, us))

# RTC code follows.
ntptime.settime()         # This library call sets RTC to ntp time.

machine.RTC().datetime()  # Read the hardware RTC in datetime format.

After you've given your ESP32 GMT NTP time, you can read the number of seconds from MycroPython Epoch with:

import utime

utime.time()

My result was this number, 634791870. It is integer seconds. With MicroPython there is no precision beyond seconds with this call. (You can get microseconds using datetime(), but that's in datetime not timestamp format.)

Next you have to do a little arithmetic:

timestamp = 946684800 + utime.time()

This gives you a UTC timestamp from Unix Epoch in integer seconds. (MicroPython ignores timezone.) To adjust for local time you need to add 3600 for each hour going east from 0 deg longitude and subtract it going west.

If your application needs a floating point format, you can just add a 0.0.

Toh answered 12/2, 2020 at 3:35 Comment(1)
I've noticed that when using a raspberry pi Pico W, after setting ntptime.settime(), running utime.time() seems to provide the Epoch time from 1970, and on an ESP8266 I get back the timestamp from the year 2000, which is interesting.Mllly
W
0

I didn't know anything about Graphite until now but a quick web search found me the Graphite documentation:

timestamp is the number of seconds since unix epoch time. Carbon-cache will use the time of arrival if the timestamp is set to -1.

A web search for unix epoch time quickly got me to a Wikipedia page which tells me:

Unix time (also known as POSIX time or UNIX Epoch time) is ... the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds.

Using that knowledge you can calculate the timestamp you need from the value you get from RTC.now() - or you can just use a value of -1 to timestamp the data with the time it arrives at the server.

This MicroPython forum thread has some examples of how to work with ntptime (which is identical on ESP32 to the ESP8266 version)

Weal answered 25/7, 2019 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.