[EN] machine.RTC

This article takes a detailed look at the Micropython machine.RTC class. The main function of this class is designed to be an RTC (Real-Time Clock) inside the microcontroller for storing date and time. It is more convenient when used with ESP8266 or ESP32 chip because NTP can be accessed to read the date and time from the internet and then store the value into the RTC to enable the accuracy and do not require frequent internet access to read the values ​​again. This saves the use of external RTC circuits as well.

machine.RTC

The esp8266 microcontroller supports only partial functionality of the machine.RTC, so this article focuses only on the part of the chip that supports it. To create an RTC object to use, do the following:

นิ่ำแะ = machine.RTC( id=0 )

Assigning a date and time to an RTC object must be supplied as a tuple of 8 members, which is the values of the year, month, date, day, hour, minute, second, and milli second. The format of the command is as follows.

object.datetime( (year, month, date, day, hour, minute, second, millisec) )

If you want to read the date and time values from the RTC, you can call the function as follows. The result is a tuple of 8 members, each with the same meaning as when sent to the RTC.

dateTime = object.datetime( )

Setting an alarm as needed is done by using the following command, where id is the generated RTC number, which at the time of creation of the object will be 0. The value time is the next number of milliseconds to be given an alarm.

object.alarm( id, time )

If you want to know the number of milliseconds before reaching the set time, you can use the following command:

millisec = object.alarm_left()

An example of using alarm and alarm_left is shown in Figure 1.

(Figure. 1 Example of alarm and alarm_left)

Therefore, the implementation of alarm and alarm_left is like a delay loop, e.g. requires a delay of 2 seconds or 2000 milliseconds, after which ‘ho’ is displayed.

import machine
rtc = machine.RTC()
rtc.alarm(0, 2000)
while (rtc.alarm_left()>0):
    pass
print("Ho")

The esp32 microcontroller supports datetime() but does not support alarm and alarm_left. If you want to delay like above, use time.sleep_ms( number of seconds ) instead of alarm/alarm_left .

An example code to set time by loading time from NTP and updating time in rtc object is as follows.

import ntptime
import machine
import socket
import network as nw

myESSID = "ชื่อAP"
myPassword = "รหัสผ่าน"
ifSTA = nw.WLAN(nw.STA_IF)
if (ifSTA.active() == False):
    rtc = machine.RTC()
    print(rtc.datetime())
    ifSTA.active(True)
    ifSTA.connect(myESSID, myPassword)
    while not ifSTA.isconnected():
        pass
    print("network configuration:\n{}".format(ifSTA.ifconfig()))
    ntptime.settime(timezone=7, erver = 'ntp.ntsc.ac.cn') # Bangkok +7
    ifSTA.active(False)
    print(rtc.datetime())
else:
    print("STA active Failed!")

Conclusion

From this article, you will find that the use of machine.RTC in esp8266 and esp32 microcontrollers is focused on collecting the date and time. For setting the time, use machine.Timer instead, especially esp32 if you want to delay the time. use time.sleep_ms( ) in the article esp8266 time library instead of a loop alarm/alarm_left Finally, have fun with programming.

If you want to talk with us, feel free to leave comments below!!

References

  1. Micropython: Quick reference for the ESP8266.
  2. Micropython: class RTC (Real-time-clock)

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-10-29