nagylzs
nagylzs

Reputation: 4180

MicroPython on ESP8266 - RTC does not have an init method?

The documentation says that the RTC clock can be initialized with the RTC.init call.

https://docs.micropython.org/en/latest/esp8266/library/machine.RTC.html

But it does not work that way:

>>>import machine
>>>rtc = machine.RTC()
>>>rtc.init((2018,4,10,17,30))
Traceback (most recent call last):
  File "<stdin>", line 1 in <module>:
AttributeError: 'RTC' object has no attribute 'init'

So the documentation contradicts reality. The firmware version is v1.9.3 - downloaded the latest just a few days ago.

Most interestingly, dir(rtc) gives ['datetime','memory','alarm','alarm_left','irq','ALARM0']. It is missing several other methods: now, deinit, cancel

So where is the RTC init method, and how could it disappear?

UPDATE: I have figured out that the documentation is wrong, I need to use RTC.datetime instead of RTC.init. But it is still wrong:

>>>from machine import RTC
>>>rtc=RTC()
>>>rtc.datetime((2000,1,1,23,59,59,0,0))
>>>rtc.datetime()
(2000, 1, 3, 0, 11, 59, 3, 705)

In other words: 2000-01-01T23:59:59 suddenly became 2000-01-03T00:11:59. How?

I also could not find anything useful on the tzinfo parameter of the RTC.datetime method. It should be a number, that is clear. But what does it mean?

I have also tried midnight:

>>>rtc.datetime((2000,1,1,0,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,155)

So at tzinfo=0, midnight becomes 05:00:00. I first thought that it means UTC+5 but it does not:

>>>rtc.datetime((2000,1,1,10,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,155)

And finally:

>>>rtc.datetime((2000,1,1,5,0,0,0,0))
>>>rtc.datetime()
(2000,1,1,5,0,0,1,545)

This is insane! It looks like the hour part is totally ignored.

Upvotes: 1

Views: 2372

Answers (1)

nagylzs
nagylzs

Reputation: 4180

Wow this is unbelievable! After looking in the source code, it turns out that the fourth parameter is "day of the week", beginning with Monday. So the documentation is totally messed up!

tzinfo parameter does not exist.

The actual parameters in the tuple are:

(year,month,day,day of the week(0-6;Mon-Sun),hour(24 hour format),minute,second,microsecond)

It also seems that when you set the date and time, you can always set day of the week to zero, it will be automatically corrected from the year+month+day.

So for April 10th,2018 at 6:31 P.M. with 15 seconds and 0 microseconds:

>>>rtc.datetime((2018,4,10,0,18,31,15,0))
>>>rtc.datetime()
(2018, 4, 10, 1, 18, 31, 16, 808)

Where the fourth number=1 means it is the SECOND day of the week, Tuesday.

Upvotes: 6

Related Questions