SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
rtc_helper.ino
Go to the documentation of this file.
1
33#include <RtcDrv.hpp>
34
35#ifndef SENSOR_SDA
36#define SENSOR_SDA 17
37#endif
38
39#ifndef SENSOR_SCL
40#define SENSOR_SCL 18
41#endif
42
43#ifndef SENSOR_IRQ
44#define SENSOR_IRQ 7
45#endif
46
48
49uint32_t interval = 0;
50uint32_t loopCount = 0;
51
52void printInt(int val)
53{
54 if (val < 10) {
55 Serial.print("0");
56 }
57 Serial.print(val);
58}
59
60
61void setup()
62{
63
64 Serial.begin(115200);
65 // Wait for the serial port to be ready
66 while (!Serial);
67
68 // Try to initialize the RTC module using I2C with specified SDA and SCL pins
69 if (!rtc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
70 Serial.println("Failed to find PCF8XXX - check your wiring!");
71 // Enter an infinite loop to halt the program
72 while (1) {
73 delay(1000);
74 }
75 }
76
77 uint16_t year = 2023;
78 uint8_t month = 9;
79 uint8_t day = 7;
80 uint8_t hour = 11;
81 uint8_t minute = 24;
82 uint8_t second = 30;
83
84 // Set the defined date and time on the RTC
85 rtc.setDateTime(year, month, day, hour, minute, second);
86
87 Serial.print("RTC Model:");
88 Serial.println(rtc.getChipName());
89}
90
91void loop()
92{
93 // Check if one second has passed since the last update
94 if (millis() > interval) {
95
96 // Update the interval to the current time
97 interval = millis() + 1000;
98
99 // Retrieve the current date and time from the RTC
100 RTC_DateTime datetime = rtc.getDateTime();
101
102 Serial.print("[RTC ]:");
103 Serial.print(" Year :"); printInt(datetime.getYear());
104 Serial.print(" Month:"); printInt(datetime.getMonth());
105 Serial.print(" Day :"); printInt(datetime.getDay());
106 Serial.print(" Hour:"); printInt(datetime.getHour());
107 Serial.print(" Minute:"); printInt(datetime.getMinute());
108 Serial.print(" Sec :"); printInt(datetime.getSecond());
109 Serial.println();
110
111 // Convert the RTC date and time to Unix time
112 struct tm info = datetime.toUnixTime();
113 Serial.print("[UNIX]:");
114 Serial.print(" Year :"); printInt(info.tm_year + 1900); // tm_year starts counting from 1900
115 Serial.print(" Month:"); printInt(info.tm_mon + 1); // tm_mon range is 0 - 11, 0 means January
116 Serial.print(" Day :"); printInt(info.tm_mday);
117 Serial.print(" Hour:"); printInt(info.tm_hour);
118 Serial.print(" Minute:"); printInt(info.tm_min);
119 Serial.print(" Sec :"); printInt(info.tm_sec);
120 Serial.println();
121
122 // Set a new Unix time at the 10th loop iteration
123 if (loopCount == 10) {
124 Serial.print("Set Unix Time:");
125 Serial.println();
126 Serial.println();
127 struct tm utc_tm;
128 utc_tm.tm_year = 2025 - 1900; // tm_year starts counting from 1900
129 utc_tm.tm_mon = 0; // tm_mon range is 0 - 11, 0 means January
130 utc_tm.tm_mday = 23;
131 utc_tm.tm_hour = 7;
132 utc_tm.tm_min = 1;
133 utc_tm.tm_sec = 28;
134 rtc.setDateTime(utc_tm);
135 }
136
137 // Set a UTC time with a time zone offset of 8 hours at the 20th loop iteration
138 if (loopCount == 20) {
139 Serial.print("Set UTC time to time zone offset 8 hours:");
140 Serial.println();
141 Serial.println();
142 struct tm utc_tm;
143 utc_tm.tm_year = 2025 - 1900; // tm_year starts counting from 1900
144 utc_tm.tm_mon = 0; // tm_mon range is 0 - 11, 0 means January
145 utc_tm.tm_mday = 23;
146 utc_tm.tm_hour = 7;
147 utc_tm.tm_min = 1;
148 utc_tm.tm_sec = 28;
149 rtc.convertUtcToTimezone(utc_tm, 8 * 3600);
150 rtc.setDateTime(utc_tm);
151 }
152
153 if (loopCount > 30) {
154
155 char buf[64];
156
157 struct tm timeinfo;
158 // Get the time C library structure
159 rtc.getDateTime(&timeinfo);
160
161 // Format the output using the strftime function
162 // For more formats, please refer to :
163 // https://man7.org/linux/man-pages/man3/strftime.3.html
164
165 size_t written = strftime(buf, 64, "%A, %B %d %Y %H:%M:%S", &timeinfo);
166
167 if (written != 0) {
168 Serial.println(buf);
169 }
170
171 written = strftime(buf, 64, "%b %d %Y %H:%M:%S", &timeinfo);
172 if (written != 0) {
173 Serial.println(buf);
174 }
175
176 written = strftime(buf, 64, "%A, %d. %B %Y %I:%M%p", &timeinfo);
177 if (written != 0) {
178 Serial.println(buf);
179 }
180 }
181
182 ++loopCount;
183 }
184}
@license MIT License
uint8_t getMinute() const
Definition SensorRTC.h:149
uint8_t getMonth() const
Definition SensorRTC.h:146
uint8_t getDay() const
Definition SensorRTC.h:147
uint16_t getYear() const
Definition SensorRTC.h:145
uint8_t getHour() const
Definition SensorRTC.h:148
uint8_t getSecond() const
Definition SensorRTC.h:150
struct tm toUnixTime()
Definition SensorRTC.h:131
void convertUtcToTimezone(int &year, int &month, int &day, int &hour, int &minute, int &second, int timezoneOffsetSeconds)
Convert UTC time represented by individual values to the time in the specified timezone.
Definition SensorRTC.h:601
const char * getChipName()
Get the name of the RTC chip.
void setDateTime(RTC_DateTime datetime)
Set the date and time on the RTC device.
RTC_DateTime getDateTime()
Get the current date and time from the RTC device.
bool begin(SensorCommCustom::CustomCallback callback)
Initialize the RTC driver using a custom callback.
char buf[64]
void setup()
void printInt(int val)
#define SENSOR_SCL
#define SENSOR_SDA
uint32_t interval
uint32_t loopCount
SensorRtcHelper rtc
void loop()