RTC Basic usage
This page shows how to get time from NTP server and keep track using RTC.
ℹ️
The RTC can drift a little each day, so in order to keep time accurate it's best to resync with NTP once a day.
Example fetching time from the Internet
from inkplate2 import Inkplate
import network
import ntptime
import time
# WiFi credentials
SSID = ""
PASSWORD = ""
inkplate = Inkplate()
inkplate.begin()
# Connect to WiFi (connection process explained on previous page)
if not do_connect():
raise SystemExit("WiFi connection failed")
# Sync with NTP server and set the RTC time
try:
ntptime.settime()
except:
print("Failed to sync with NTP")
while True:
# Clear the display buffer and set cursor at upper left corner
inkplate.clearDisplay()
inkplate.setCursor(0, 0)
# Get UTC time
utc_time = time.localtime()
# Convert UTC -> local time (e.g., UTC+2
# Offset in seconds (hours * 3600)
timezone_offset = (2 * 3600)
# Apply timezone offset
local_time = time.localtime(time.mktime(utc_time) + timezone_offset)
# Extract year, month, day, hour, minute, second from the tuple, excludes weekday, yearday
year, month, mday, hour, minute, second, *_ = local_time
inkplate.print(f"{year}-{month:02d}-{mday:02d} {hour:02d}:{minute:02d}:{second:02d}")
# Update display
inkplate.display()
time.sleep(30)
ntptime.settime()
Fetch current UTC time and set Real Time Clock
time.localtime()
Convert current time in seconds into a tuple format
Returns value: Return time as a tuple: (year, month, mday, hour, minute, second, weekday, yearday)
Function parameters:
| Type | Name | Description |
|---|---|---|
int | secs | If secs is not provided or None, then return current time from the RTC |
time.mktime()
Inverse function of localtime.
Returns value: Returns an integer which is the number of seconds since the time epoch (UNIX timestamp).
Function parameters:
| Type | Name | Description |
|---|---|---|
tuple | date_time_tuple | 8-tuple which expresses a time as localtime |