0

In order to do a project that includes data from PySense, I need to attach to that data a way to associate a time and date. However, it is showing me a date from 1970 and the time starts at 00:00:00.

How do I get the real time and date?

What I found online:

current_time = utime.localtime()
timestamp = "{:04}-{:02}-{:02} {:02}:{:02}:{:02}".format(current_time[0], current_time[1], 
current_time[2], current_time[3], current_time[4], current_time[5])
print("Timestamp: {}".format(timestamp)) 

This method uses the utime lib. However, the output I'm getting after running the code is like this:

Timestamp: 1970-01-01 00:04:40
Temperature: 22.96 C
Humidity: 62.90 %
Light: 42 lux

which is not the real data.

PS. This is a program that is supposed to run on a LoPy4 from Pycom board company.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    How do you expect it to *know* what the current date/time is? You'd have to add some sort of user interface for setting it - and then NEVER turn the device off, since the LoPy4 does not appear to have a backup battery to maintain the time when it's not powered. – jasonharper Mar 14 '23 at 13:27

1 Answers1

1

You will have to sync RTC with some NTP server first!

See: https://docs.pycom.io/firmwareapi/pycom/machine/rtc/ and https://docs.pycom.io/tutorials/networkprotocols/ntp/

Of course, this means you will need to connect to the internet in some way.

My answer in Pycom forum

Alex
  • 193
  • 1
  • 4
  • 11