Continous time check

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 7
Joined: Tue Jan 22, 2019 12:00 pm

Continous time check

Post by hetvishah08 » Thu Jan 24, 2019 11:27 am

I am developing an application using ESP32 DevkitC where i need current time to compare it and if the time is correct it would proceed to other conditions but the time i receive is Jan 1,2000. I want the current time.I used the localtime function from the time module.

>>> import time
>>> time.localtime(time.time())
(2000, 1, 1, 0, 17, 3, 5, 1)

I read on the micropython docs sit that if RTC is not used it uses epoch of 2000-01-01 00:00:00 UTC.

I used RTC to set time and it worked but after reset the time shown is JAN 1 2000.

>>> import machine
>>> rtc=machine.RTC()
>>> rtc.datetime((2019,1,24,4,15,48,0,0))
>>> rtc.datetime()
(2019, 1, 24, 3, 15, 48, 11, 99222)
ets Jun 8 2016 00:22:57

configsip: 0, SPIWP:0xee
mode:DIO, clock div:2
entry 0x4008114c
I (399) cpu_start: Pro cpu up.
I (399) cpu_start: Single core mode
I (400) heap_init: Initializing. RAM available for dynamic allocation:
I (403) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (409) heap_init: At 3FFC0E00 len 0001F200 (124 KiB): DRAM
I (415) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (422) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (428) heap_init: At 400915E0 len 0000EA20 (58 KiB): IRAM
I (434) cpu_start: Pro cpu start user code
I (5) cpu_start: Starting scheduler on PRO CPU.
I (589) mmodsocket: Initializing
I (599) system_api: Base MAC address is not set, read default base MAC address f rom BLK0 of EFUSE
MicroPython v1.9.4-771-gb33f108cd on 2019-01-03; ESP32 module with ESP32
Type "help()" for more information.
>>> import machine
>>> import time
>>> rtc=machine.RTC()
>>> rtc.datetime()
(2000, 1, 1, 5, 0, 0, 47, 488887)

How do i get current time even after reset? I am running a script and want to continously check current time for a condition.Using RTC would work once but after reset the previous time set in RTC in a script would not be the current time.

Posts: 144
Joined: Wed Nov 01, 2017 7:46 am

Re: Continous time check

Post by HermannSW » Tue Feb 05, 2019 11:56 am

You can use ntptime to set current time:

Add the statements to or, and you will have current time even after reset.

Post Reply