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