init Files
This commit is contained in:
378
libraries/ESP32Time/ESP32Time.cpp
Normal file
378
libraries/ESP32Time/ESP32Time.cpp
Normal file
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "ESP32Time.h"
|
||||
#include "time.h"
|
||||
#include <sys/time.h>
|
||||
|
||||
#ifdef RTC_DATA_ATTR
|
||||
RTC_DATA_ATTR static bool overflow;
|
||||
#else
|
||||
static bool overflow;
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
@brief Constructor for ESP32Time
|
||||
*/
|
||||
ESP32Time::ESP32Time(){
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief Constructor for ESP32Time
|
||||
@param offest
|
||||
gmt offset in seconds
|
||||
*/
|
||||
ESP32Time::ESP32Time(long offset){
|
||||
this->offset = offset;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief set the internal RTC time
|
||||
@param sc
|
||||
second (0-59)
|
||||
@param mn
|
||||
minute (0-59)
|
||||
@param hr
|
||||
hour of day (0-23)
|
||||
@param dy
|
||||
day of month (1-31)
|
||||
@param mt
|
||||
month (1-12)
|
||||
@param yr
|
||||
year ie 2021
|
||||
@param ms
|
||||
microseconds (optional)
|
||||
*/
|
||||
void ESP32Time::setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms) {
|
||||
// seconds, minute, hour, day, month, year $ microseconds(optional)
|
||||
// ie setTime(20, 34, 8, 1, 4, 2021) = 8:34:20 1/4/2021
|
||||
struct tm t = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initalize to all 0's
|
||||
t.tm_year = yr - 1900; // This is year-1900, so 121 = 2021
|
||||
t.tm_mon = mt - 1;
|
||||
t.tm_mday = dy;
|
||||
t.tm_hour = hr;
|
||||
t.tm_min = mn;
|
||||
t.tm_sec = sc;
|
||||
time_t timeSinceEpoch = mktime(&t);
|
||||
setTime(timeSinceEpoch, ms);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief set time from struct
|
||||
@param tm
|
||||
time struct
|
||||
*/
|
||||
void ESP32Time::setTimeStruct(tm t) {
|
||||
time_t timeSinceEpoch = mktime(&t);
|
||||
setTime(timeSinceEpoch, 0);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief set the internal RTC time
|
||||
@param epoch
|
||||
epoch time in seconds
|
||||
@param ms
|
||||
microseconds (optional)
|
||||
*/
|
||||
void ESP32Time::setTime(unsigned long epoch, int ms) {
|
||||
struct timeval tv;
|
||||
if (epoch > 2082758399){
|
||||
overflow = true;
|
||||
tv.tv_sec = epoch - 2082758399; // epoch time (seconds)
|
||||
} else {
|
||||
overflow = false;
|
||||
tv.tv_sec = epoch; // epoch time (seconds)
|
||||
}
|
||||
tv.tv_usec = ms; // microseconds
|
||||
settimeofday(&tv, NULL);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the internal RTC time as a tm struct
|
||||
*/
|
||||
tm ESP32Time::getTimeStruct(){
|
||||
struct tm timeinfo;
|
||||
time_t now;
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
time_t tt = mktime (&timeinfo);
|
||||
|
||||
if (overflow){
|
||||
tt += 63071999;
|
||||
}
|
||||
if (offset > 0){
|
||||
tt += (unsigned long) offset;
|
||||
} else {
|
||||
tt -= (unsigned long) (offset * -1);
|
||||
}
|
||||
struct tm * tn = localtime(&tt);
|
||||
if (overflow){
|
||||
tn->tm_year += 64;
|
||||
}
|
||||
return *tn;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the time and date as an Arduino String object
|
||||
@param mode
|
||||
true = Long date format
|
||||
false = Short date format
|
||||
*/
|
||||
String ESP32Time::getDateTime(bool mode){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
char s[51];
|
||||
if (mode)
|
||||
{
|
||||
strftime(s, 50, "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
strftime(s, 50, "%a, %b %d %Y %H:%M:%S", &timeinfo);
|
||||
}
|
||||
return String(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the time and date as an Arduino String object
|
||||
@param mode
|
||||
true = Long date format
|
||||
false = Short date format
|
||||
*/
|
||||
String ESP32Time::getTimeDate(bool mode){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
char s[51];
|
||||
if (mode)
|
||||
{
|
||||
strftime(s, 50, "%H:%M:%S %A, %B %d %Y", &timeinfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
strftime(s, 50, "%H:%M:%S %a, %b %d %Y", &timeinfo);
|
||||
}
|
||||
return String(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the time as an Arduino String object
|
||||
*/
|
||||
String ESP32Time::getTime(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
char s[51];
|
||||
strftime(s, 50, "%H:%M:%S", &timeinfo);
|
||||
return String(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the time as an Arduino String object with the specified format
|
||||
@param format
|
||||
time format
|
||||
http://www.cplusplus.com/reference/ctime/strftime/
|
||||
*/
|
||||
String ESP32Time::getTime(String format){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
char s[128];
|
||||
char c[128];
|
||||
format.toCharArray(c, 127);
|
||||
strftime(s, 127, c, &timeinfo);
|
||||
return String(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the date as an Arduino String object
|
||||
@param mode
|
||||
true = Long date format
|
||||
false = Short date format
|
||||
*/
|
||||
String ESP32Time::getDate(bool mode){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
char s[51];
|
||||
if (mode)
|
||||
{
|
||||
strftime(s, 50, "%A, %B %d %Y", &timeinfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
strftime(s, 50, "%a, %b %d %Y", &timeinfo);
|
||||
}
|
||||
return String(s);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current milliseconds as unsigned long
|
||||
*/
|
||||
unsigned long ESP32Time::getMillis(){
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return tv.tv_usec/1000;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current microseconds as unsigned long
|
||||
*/
|
||||
unsigned long ESP32Time::getMicros(){
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return tv.tv_usec;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current epoch seconds as unsigned long
|
||||
*/
|
||||
unsigned long ESP32Time::getEpoch(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return mktime(&timeinfo);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current epoch seconds as unsigned long from the rtc without offset
|
||||
*/
|
||||
unsigned long ESP32Time::getLocalEpoch(){
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
unsigned long epoch = tv.tv_sec;
|
||||
if (overflow){
|
||||
epoch += 63071999 + 2019686400;
|
||||
}
|
||||
return epoch;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current seconds as int
|
||||
*/
|
||||
int ESP32Time::getSecond(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_sec;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current minutes as int
|
||||
*/
|
||||
int ESP32Time::getMinute(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_min;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current hour as int
|
||||
@param mode
|
||||
true = 24 hour mode (0-23)
|
||||
false = 12 hour mode (1-12)
|
||||
*/
|
||||
int ESP32Time::getHour(bool mode){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
if (mode)
|
||||
{
|
||||
return timeinfo.tm_hour;
|
||||
}
|
||||
else
|
||||
{
|
||||
int hour = timeinfo.tm_hour;
|
||||
if (hour > 12)
|
||||
{
|
||||
return timeinfo.tm_hour-12;
|
||||
}
|
||||
else if (hour == 0)
|
||||
{
|
||||
return 12; // 12 am
|
||||
}
|
||||
else
|
||||
{
|
||||
return timeinfo.tm_hour;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief return current hour am or pm
|
||||
@param lowercase
|
||||
true = lowercase
|
||||
false = uppercase
|
||||
*/
|
||||
String ESP32Time::getAmPm(bool lowercase){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
if (timeinfo.tm_hour >= 12)
|
||||
{
|
||||
if (lowercase)
|
||||
{
|
||||
return "pm";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "PM";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (lowercase)
|
||||
{
|
||||
return "am";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "AM";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current day as int (1-31)
|
||||
*/
|
||||
int ESP32Time::getDay(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_mday;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current day of week as int (0-6)
|
||||
*/
|
||||
int ESP32Time::getDayofWeek(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_wday;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current day of year as int (0-365)
|
||||
*/
|
||||
int ESP32Time::getDayofYear(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_yday;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current month as int (0-11)
|
||||
*/
|
||||
int ESP32Time::getMonth(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_mon;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get the current year as int
|
||||
*/
|
||||
int ESP32Time::getYear(){
|
||||
struct tm timeinfo = getTimeStruct();
|
||||
return timeinfo.tm_year+1900;
|
||||
}
|
||||
67
libraries/ESP32Time/ESP32Time.h
Normal file
67
libraries/ESP32Time/ESP32Time.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ESP32TIME_H
|
||||
#define ESP32TIME_H
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
class ESP32Time {
|
||||
|
||||
public:
|
||||
ESP32Time();
|
||||
ESP32Time(long offset);
|
||||
void setTime(unsigned long epoch = 1609459200, int ms = 0); // default (1609459200) = 1st Jan 2021
|
||||
void setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms = 0);
|
||||
void setTimeStruct(tm t);
|
||||
tm getTimeStruct();
|
||||
String getTime(String format);
|
||||
|
||||
String getTime();
|
||||
String getDateTime(bool mode = false);
|
||||
String getTimeDate(bool mode = false);
|
||||
String getDate(bool mode = false);
|
||||
String getAmPm(bool lowercase = false);
|
||||
|
||||
unsigned long getEpoch();
|
||||
unsigned long getMillis();
|
||||
unsigned long getMicros();
|
||||
int getSecond();
|
||||
int getMinute();
|
||||
int getHour(bool mode = false);
|
||||
int getDay();
|
||||
int getDayofWeek();
|
||||
int getDayofYear();
|
||||
int getMonth();
|
||||
int getYear();
|
||||
|
||||
long offset = 0;
|
||||
unsigned long getLocalEpoch();
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
21
libraries/ESP32Time/LICENSE
Normal file
21
libraries/ESP32Time/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
42
libraries/ESP32Time/README.md
Normal file
42
libraries/ESP32Time/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# ESP32Time
|
||||
An Arduino library for setting and retrieving internal RTC time on ESP32 boards
|
||||
|
||||
[](https://www.arduinolibraries.info/libraries/esp32-time)
|
||||
[](https://registry.platformio.org/libraries/fbiego/ESP32Time)
|
||||
|
||||
## Functions
|
||||
|
||||
```
|
||||
ESP32Time rtc(offset); // create an instance with a specifed offset in seconds
|
||||
rtc.offset; // get or modify the current offset
|
||||
setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
|
||||
setTime(1609459200); // 1st Jan 2021 00:00:00
|
||||
setTimeStruct(time); // set with time struct
|
||||
|
||||
getTime() // (String) 15:24:38
|
||||
getDate() // (String) Sun, Jan 17 2021
|
||||
getDate(true) // (String) Sunday, January 17 2021
|
||||
getDateTime() // (String) Sun, Jan 17 2021 15:24:38
|
||||
getDateTime(true) // (String) Sunday, January 17 2021 15:24:38
|
||||
getTimeDate() // (String) 15:24:38 Sun, Jan 17 2021
|
||||
getTimeDate(true) // (String) 15:24:38 Sunday, January 17 2021
|
||||
|
||||
getMicros() // (unsigned long) 723546
|
||||
getMillis() // (unsigned long) 723
|
||||
getEpoch() // (unsigned long) 1609459200
|
||||
getLocalEpoch() // (unsigned long) 1609459200 // local epoch without offset
|
||||
getSecond() // (int) 38 (0-59)
|
||||
getMinute() // (int) 24 (0-59)
|
||||
getHour() // (int) 3 (1-12)
|
||||
getHour(true) // (int) 15 (0-23)
|
||||
getAmPm() // (String) PM
|
||||
getAmPm(true) // (String) pm
|
||||
getDay() // (int) 17 (1-31)
|
||||
getDayofWeek() // (int) 0 (0-6)
|
||||
getDayofYear() // (int) 16 (0-365)
|
||||
getMonth() // (int) 0 (0-11)
|
||||
getYear() // (int) 2021
|
||||
|
||||
getTime("%A, %B %d %Y %H:%M:%S") // (String) returns time with specified format
|
||||
```
|
||||
[`Formatting options`](http://www.cplusplus.com/reference/ctime/strftime/)
|
||||
77
libraries/ESP32Time/examples/esp32_time/esp32_time.ino
Normal file
77
libraries/ESP32Time/examples/esp32_time/esp32_time.ino
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ESP32Time.h>
|
||||
|
||||
//ESP32Time rtc;
|
||||
ESP32Time rtc(3600); // offset in seconds GMT+1
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
|
||||
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
|
||||
//rtc.offset = 7200; // change offset value
|
||||
|
||||
/*---------set with NTP---------------*/
|
||||
// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
||||
// struct tm timeinfo;
|
||||
// if (getLocalTime(&timeinfo)){
|
||||
// rtc.setTimeStruct(timeinfo);
|
||||
// }
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Serial.println(rtc.getTime()); // (String) 15:24:38
|
||||
// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021
|
||||
// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021
|
||||
// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38
|
||||
// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38
|
||||
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
|
||||
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
|
||||
//
|
||||
// Serial.println(rtc.getMicros()); // (long) 723546
|
||||
// Serial.println(rtc.getMillis()); // (long) 723
|
||||
// Serial.println(rtc.getEpoch()); // (long) 1609459200
|
||||
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
|
||||
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
|
||||
// Serial.println(rtc.getHour()); // (int) 3 (1-12)
|
||||
// Serial.println(rtc.getHour(true)); // (int) 15 (0-23)
|
||||
// Serial.println(rtc.getAmPm()); // (String) pm
|
||||
// Serial.println(rtc.getAmPm(true)); // (String) PM
|
||||
// Serial.println(rtc.getDay()); // (int) 17 (1-31)
|
||||
// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6)
|
||||
// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365)
|
||||
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
|
||||
// Serial.println(rtc.getYear()); // (int) 2021
|
||||
|
||||
// Serial.println(rtc.getLocalEpoch()); // (long) 1609459200 epoch without offset
|
||||
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
|
||||
// formating options http://www.cplusplus.com/reference/ctime/strftime/
|
||||
|
||||
|
||||
struct tm timeinfo = rtc.getTimeStruct();
|
||||
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // (tm struct) Sunday, January 17 2021 07:24:38
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ESP32Time.h>
|
||||
|
||||
ESP32Time rtc;
|
||||
ESP32Time rtc1(-3600); // offset GMT-1
|
||||
ESP32Time rtc2(7200); // offset GMT+2
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
|
||||
//rtc1.setTime(1609459200); // 1st Jan 2021 00:00:00
|
||||
// time can be set on one instance
|
||||
// no need for rtc1.setTime() or rtc2.setTime()
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Serial.println(rtc.getTime()); // (String) 15:24:38
|
||||
// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021
|
||||
// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021
|
||||
// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38
|
||||
// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38
|
||||
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
|
||||
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
|
||||
//
|
||||
// Serial.println(rtc.getMicros()); // (unsigned long) 723546
|
||||
// Serial.println(rtc.getMillis()); // (unsigned long) 723
|
||||
// Serial.println(rtc.getEpoch()); // (unsigned long) 1609459200
|
||||
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
|
||||
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
|
||||
// Serial.println(rtc.getHour()); // (int) 3 (1-12)
|
||||
// Serial.println(rtc.getHour(true)); // (int) 15 (0-23)
|
||||
// Serial.println(rtc.getAmPm()); // (String) pm
|
||||
// Serial.println(rtc.getAmPm(true)); // (String) PM
|
||||
// Serial.println(rtc.getDay()); // (int) 17 (1-31)
|
||||
// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6)
|
||||
// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365)
|
||||
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
|
||||
// Serial.println(rtc.getYear()); // (int) 2021
|
||||
|
||||
Serial.println(rtc.getTime("RTC0: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
|
||||
Serial.println(rtc1.getTime("RTC1: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
|
||||
Serial.println(rtc2.getTime("RTC2: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
|
||||
|
||||
// formating options http://www.cplusplus.com/reference/ctime/strftime/
|
||||
|
||||
Serial.println(rtc.getEpoch()); // (unsigned long)
|
||||
Serial.println(rtc1.getEpoch()); // (unsigned long)
|
||||
Serial.println(rtc2.getEpoch()); // (unsigned long)
|
||||
|
||||
Serial.println(rtc.getLocalEpoch()); // (unsigned long) epoch without offset, same for all instances
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 Felix Biego
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ESP32Time.h>
|
||||
|
||||
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
|
||||
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
|
||||
|
||||
ESP32Time rtc;
|
||||
|
||||
|
||||
void wakeup_reason() {
|
||||
esp_sleep_wakeup_cause_t wakeup_reason;
|
||||
|
||||
wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
switch (wakeup_reason)
|
||||
{
|
||||
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
|
||||
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
|
||||
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
|
||||
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
|
||||
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
|
||||
default :
|
||||
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
|
||||
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
|
||||
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
|
||||
//rtc.offset = 7200; // change offset value
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
wakeup_reason();
|
||||
|
||||
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
|
||||
|
||||
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
|
||||
|
||||
Serial.println("Going to sleep now");
|
||||
Serial.flush();
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
22
libraries/ESP32Time/keywords.txt
Normal file
22
libraries/ESP32Time/keywords.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
ESP32Time KEYWORD1
|
||||
|
||||
setTime KEYWORD2
|
||||
getTime KEYWORD2
|
||||
setTimeStruct KEYWORD2
|
||||
getTimeStruct KEYWORD2
|
||||
getDateTime KEYWORD2
|
||||
getTimeDate KEYWORD2
|
||||
getDate KEYWORD2
|
||||
getAmPm KEYWORD2
|
||||
getMillis KEYWORD2
|
||||
getMicros KEYWORD2
|
||||
getEpoch KEYWORD2
|
||||
getLocalEpoch KEYWORD2
|
||||
getSecond KEYWORD2
|
||||
getMinute KEYWORD2
|
||||
getHour KEYWORD2
|
||||
getDay KEYWORD2
|
||||
getDayofWeek KEYWORD2
|
||||
getDayofYear KEYWORD2
|
||||
getMonth KEYWORD2
|
||||
getYear KEYWORD2
|
||||
21
libraries/ESP32Time/library.json
Normal file
21
libraries/ESP32Time/library.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "ESP32Time",
|
||||
"version": "2.0.6",
|
||||
"keywords": "Arduino, ESP32, Time, Internal RTC",
|
||||
"description": "An Arduino library for setting and retrieving internal RTC time on ESP32 boards",
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/fbiego/ESP32Time"
|
||||
},
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "fbiego",
|
||||
"email": "fbiego.fb@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif8266, espressif32"
|
||||
}
|
||||
11
libraries/ESP32Time/library.properties
Normal file
11
libraries/ESP32Time/library.properties
Normal file
@@ -0,0 +1,11 @@
|
||||
name=ESP32Time
|
||||
version=2.0.6
|
||||
author=fbiego
|
||||
maintainer=fbiego
|
||||
sentence=Set and retrieve internal RTC time on ESP32 boards.
|
||||
paragraph=No need for external RTC module or NTP time synchronization.
|
||||
category=Timing
|
||||
url=https://github.com/fbiego/ESP32Time
|
||||
architectures=*
|
||||
includes=ESP32Time.h
|
||||
|
||||
Reference in New Issue
Block a user