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