- Why do we need this RTL8720_RTC library
- Changelog
- Prerequisites
- Installation
- Packages' Patches
- Usage
- Examples
- Example RTL8720_RTC_Time_WiFi
- Debug Terminal Output Samples
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License
- Copyright
Why do we need this RTL8720_RTC library
This is an Arduino library for RTL8720-based RTC
The examples will demonstrate how to get the UTC time from NTP server, then update the RTC to make sure the time is perfectly correct.
Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use an ISR-based Alarm with Interrupt to call your function.
These ISR-based Alarm, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
- RTL8720-based boards such as Realtek RTL8720DN, RTL8722DM and RTL8722CSM, etc.
-
Inside the attached function, delay() won’t work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.
-
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as volatile.
-
Don't use Serial.print(ln) function inside ISR or system will hang up.
Arduino IDE 1.8.19+
for Arduino.Arduino AmebaD core 3.1.2+
for Realtek RTL8720DN, RTL8722DM and RTL8722CSM.Timezone_Generic library v1.10.0+
to use examples using Timezone. To install, checkWiFiWebServer_RTL8720 library v1.1.1+
WiFi to access NTP server. To install. check .
The best and easiest way is to use Arduino Library Manager
. Search for RTL8720_RTC, then select / install the latest version.
You can also use this link for more detailed instructions.
Another way to install is to:
- Navigate to RTL8720_RTC page.
- Download the latest release
RTL8720_RTC-main.zip
. - Extract the zip file to
RTL8720_RTC-main
directory - Copy whole
RTL8720_RTC-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install RTL8720_RTC library by using Library Manager. Search for RTL8720_RTC in Platform.io Author's Libraries
- Please visit documentation for the other options and examples at Project Configuration File
To avoid compile error relating to PROGMEM, you have to copy the file Realtek AmebaD core pgmspace.h into Realtek AmebaD directory (~/.arduino15/packages/realtek/hardware/AmebaD/3.1.1/cores/arduino/avr/pgmspace.h).
Supposing the Realtek AmebaD core version is 3.1.1. This file must be copied into the directory:
~/.arduino15/packages/realtek/hardware/AmebaD/3.1.1/cores/arduino/avr/pgmspace.h
Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/realtek/hardware/AmebaD/x.yy.zz/cores/arduino/avr/pgmspace.h
void init();
bool free();
void write(time_t t);
time_t read();
void wait(float s);
void setAlarm(alarm_t *alrm, alarm_irq_handler alarmHandler);
void disableAlarm();
void restoreTimeInfo();
void backupTimeInfo();
class TimeSpan;
class DateTime;
/** ISO 8601 Timestamp function */
enum timestampOpt
{
TIMESTAMP_FULL, // YYYY-MM-DDTHH:MM:SS
TIMESTAMP_TIME, // HH:MM:SS
TIMESTAMP_DATE // YYYY-MM-DD
};
class DateTime
{
DateTime (uint32_t t = SECONDS_FROM_1970_TO_2000);
DateTime (const uint16_t year, const uint8_t month, const uint8_t day, const uint8_t hour = 0, const uint8_t min = 0, const uint8_t sec = 0);
DateTime (const DateTime& copy);
DateTime (const tmElements_t& tm);
// To use with RTL8720 datetime_t struct
DateTime (const datetime_t &tm);
tmElements_t get_tmElements_t();
void setFrom_tmElements_t(const tmElements_t& tm);
time_t get_time_t();
void setFrom_time_t(const time_t& timeInput);
uint16_t year() const;
uint8_t month() const;
uint8_t day() const;
uint8_t hour() const;
uint8_t minute() const;
uint8_t second() const;
uint16_t yearOffset() const;
uint8_t dayOfTheWeek() const;
/** 32-bit times as seconds since 1/1/2000 */
long secondstime() const;
/** 32-bit times as seconds since 1/1/1970 */
uint32_t unixtime() const;
/** ISO 8601 Timestamp function */
String timestamp(timestampOpt opt = TIMESTAMP_FULL)
}
Example RTL8720_RTC_Time_WiFi
1. File RTL8720_RTC_Time_WiFi.ino
RTL8720_RTC/examples/RTL8720_RTC_Time_WiFi/RTL8720_RTC_Time_WiFi.ino
Lines 15 to 260 in 92386b3
2. File defines.h
RTL8720_RTC/examples/RTL8720_RTC_Time_WiFi/defines.h
Lines 15 to 29 in 92386b3
The following is the sample terminal output when running example RTL8720_RTC_Time_WiFi on RTL8720DN_BW16
Start RTL8720_RTC_Time_WiFi on RTL8720DN_BW16
RTL8720_RTC v1.0.2
Timezone_Generic v1.10.0
Current Firmware Version = 1.0.0
Attempting to connect to SSID: HueNet_5G
Attempting to connect to SSID: HueNet_5G
TZ_NTP_Clock_RTL8720DN started @ IP address: 192.168.2.111
Packet received
Seconds since Jan 1 1900 = 3851712462
Unix time = 1642723662
The UTC time is 0:07:42
============================
00:07:43 Fri 21 Jan 2022 UTC
19:07:43 Thu 20 Jan 2022 EST
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: RTL8720_RTC issues
- Search for bug and improvement.
- Basic RTC-wrapper library for Realtek RTL8720DN, RTL8722DM and RTL8722CSM, using
Arduino AmebaD core
- Add Version String
- Add Table of Contents
Many thanks for everyone for bug reporting, new feature suggesting, testing and contributing to the development of this library.
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under MIT
Copyright 2021- Khoi Hoang