SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
pcf8563_time_sync.ino
Go to the documentation of this file.
1
30#include <RtcDrv.hpp>
31#if defined(ARDUINO_ARCH_ESP32)
32#include <time.h>
33#include <WiFi.h>
34#include <esp_sntp.h>
35
36
37#ifndef SENSOR_SDA
38#define SENSOR_SDA 42
39#endif
40
41#ifndef SENSOR_SCL
42#define SENSOR_SCL 41
43#endif
44
45#ifndef SENSOR_IRQ
46#define SENSOR_IRQ 14
47#endif
48
49const char *ssid = "YOUR_SSID";
50const char *password = "YOUR_PASS";
51
52const char *ntpServer1 = "pool.ntp.org";
53const char *ntpServer2 = "time.nist.gov";
54const long gmtOffset_sec = 3600;
55const int daylightOffset_sec = 3600;
56
57const char *time_zone = "CST-8"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
58
60uint32_t intervalue;
61
62
63// Callback function (get's called when time adjusts via NTP)
64void timeavailable(struct timeval *t)
65{
66 Serial.println("Got time adjustment from NTP, Write the hardware clock");
67
68 // Write synchronization time to hardware
70}
71
72void setup()
73{
74 Serial.begin(115200);
75 while (!Serial);
76
77 pinMode(SENSOR_IRQ, INPUT_PULLUP);
78
79 if (!rtc.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
80 Serial.println("Failed to find PCF8563 - check your wiring!");
81 while (1) {
82 delay(1000);
83 }
84 }
85
86 // set notification call-back function
87 sntp_set_time_sync_notification_cb( timeavailable );
88
97 sntp_servermode_dhcp(1); // (optional)
98
104 // configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
105
111 configTzTime(time_zone, ntpServer1, ntpServer2);
112
113 //connect to WiFi
114 Serial.print("Connecting to ");
115 Serial.println(ssid);
116 WiFi.begin(ssid, password);
117 while (WiFi.status() != WL_CONNECTED) {
118 delay(500);
119 Serial.print(".");
120 }
121 Serial.println(" CONNECTED");
122
123}
124
125
126
127
128void loop()
129{
130 if (millis() - intervalue > 1000) {
131
132 intervalue = millis();
133
134 // hardware clock
135 struct tm hwTimeinfo;
136 rtc.getDateTime(&hwTimeinfo);
137 Serial.print("Hardware clock :");
138 Serial.println(&hwTimeinfo, "%A, %B %d %Y %H:%M:%S");
139
140 // system clock
141 struct tm timeinfo;
142 if (!getLocalTime(&timeinfo)) {
143 Serial.println("No time available (yet)");
144 return;
145 }
146
147 Serial.print("System clock :");
148 Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
149 Serial.println();
150
151 }
152}
153#else
154void setup()
155{
156 Serial.begin(115200);
157}
158
159void loop()
160{
161 Serial.println("Examples only ESP32"); delay(1000);
162}
163#endif
164
165
@license MIT License
#define SENSOR_SCL
#define SENSOR_SDA
bool begin(SensorCommCustom::CustomCallback callback)
Initialize device using a custom transport callback.
RTC_DateTime getDateTime() override
Read the RTC date and time.
Driver for the PCF8563 real-time clock (RTC) over I2C.
int hwClockWrite()
Write the current system time to the RTC clock chip.
Definition SensorRTC.h:456
SensorPCF85063 rtc
uint32_t intervalue
void setup()
void loop()
#define SENSOR_IRQ