MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
timer.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "timer.h"
16 
17 #include "esp_sntp.h"
18 
19 const char * Timer::ntp_server_ = "pool.ntp.org";
20 
21 void timeSyncCallback(struct timeval * tv)
22 {
23  Serial.println("\n----Time Synced-----");
24 }
25 
27 
29 {
30  struct tm timeinfo;
31  if (!getLocalTime(&timeinfo)) {
32  Serial.println("Failed to obtain time");
33  return tm{};
34  }
35  return timeinfo;
36 }
37 
39 {
40  auto time = get_utc_time();
41  return mktime(&time);
42 }
43 
45 {
46  auto timeinfo = get_utc_time();
47  auto tim = mktime(&timeinfo);
48  Serial.printf("Epoch time: %ld\n", tim);
49  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
50 }
51 
52 void Timer::begin(long int sync_interval_ms)
53 {
54  while (WiFi.status() != WL_CONNECTED) {
55  delay(1000);
56  Serial.println("WiFi not connected ...");
57  }
58 
59  sntp_set_time_sync_notification_cb(timeSyncCallback);
60  sntp_set_sync_interval(sync_interval_ms);
61  configTzTime("Etc/GMT", ntp_server_);
63 }
void begin(long int sync_interval_ms=600000)
initialise the timer, set a sync interval to sync with NTP server
Definition: timer.cpp:52
Timer()
Definition: timer.cpp:26
void print_utc_time()
Print the seconds since epoch.
Definition: timer.cpp:44
tm get_utc_time()
Get the utc time now.
Definition: timer.cpp:28
time_t get_epoch_time()
Get the seconds since epoch now.
Definition: timer.cpp:38
void timeSyncCallback(struct timeval *tv)
Definition: timer.cpp:21