utime.localtime() giving 00:00 in hh:mm format

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
moinologics
Posts: 1
Joined: Wed Jun 16, 2021 8:37 pm

utime.localtime() giving 00:00 in hh:mm format

Post by moinologics » Wed Jun 16, 2021 9:38 pm

when my esp-12f board boots up, in main.py i called utime.localtime() function but it giving me 0 and 0 for h and m

when i call same function from webrepl console, it giving me time correctly.

so how i get correct localtime in main.py file.

Board - esp8266, esp-12f
micropython - v1.15

note - i need it without internet connection.

davef
Posts: 326
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: utime.localtime() giving 00:00 in hh:mm format

Post by davef » Thu Jun 17, 2021 12:08 am

Code: Select all

https://docs.micropython.org/en/latest/library/utime.html
Maintaining actual calendar date/time: This requires a Real Time Clock (RTC). On systems with underlying OS (including some RTOS), an RTC may be implicit. Setting and maintaining actual calendar time is responsibility of OS/RTOS and is done outside of MicroPython, it just uses OS API to query date/time. On baremetal ports however system time depends on machine.RTC() object. The current calendar time may be set using machine.RTC().datetime(tuple) function, and maintained by following means:

By a backup battery (which may be an additional, optional component for a particular board).

Using networked time protocol (requires setup by a port/user).

Set manually by a user on each power-up (many boards then maintain RTC time across hard resets, though some may require setting it again in such case).
As I use NTP to get the time I haven't tried setting it using machine.RTC().datetime() ... so can't provide any sample code.

Be aware that the RTC on the ESP8266 doesn't keep very good time. For the ESP8266 I use NTP to kick-off a virtual timer that uses the system clock. I will provide code if you want it.

davef
Posts: 326
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: utime.localtime() giving 00:00 in hh:mm format

Post by davef » Thu Jun 17, 2021 12:49 am

Just for setting the RTC

Code: Select all

import machine

rtc = machine.RTC()
datetime = rtc.datetime
year = 2019
month = 12
day = 31
_weekday = 0
hours = 19
minutes = 05
seconds = 0
subseconds = 0

rtc.init((year, month, day, _weekday, hours, minutes, seconds, subseconds))

print(datetime_format.format(datetime()[0],get_time_date('month', datetime()[1]),get_time_date('weekday', datetime()[3]),datetime()[2],datetime()[4],datetime()[5],datetime()[6],datetime()[7],))

Post Reply