/*
*
* ESP32_rtc_time10_NTP.ino
*
*
* code from: ESP32_time.ino --> https://wokwi.com/projects/416631443765751809
*
// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
// Derived from Wokwi "ESP32 NTP Clock"
// https://wokwi.com/projects/321525495180034642
// Read https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/
// for more information on using timezones
*
*
*
Some parts of code are:
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.
*
*
* =====================================================================================
*
*
* The table shows the specifiers which we can use
* to access a particular configuration of the date/time.
* |-----------|-----------------------------------------|-------------|
* | Specifier | Description | Example |
* |-----------|-----------------------------------------|-------------|
* | %A | Full weekday name | Saturday |
* | %a | Abbreviated weekday name | Sat |
* | %g | Week-based year, last two digits (00-99)| 01 |
* | %G | Week-based year 2001 | |
* | %u | ISO 8601 weekday as number with | 4 |
* | | Monday as 1 (1-7) | |
* | %U | Week number with the first Sunday as | 33 |
* | | the first day of week one (00-53) | |
* | %V | ISO 8601 week number (01-53) | 34 |
* | %w | Weekday as a decimal number with | 4 |
* | | Sunday as 0 (0-6) | |
* | %W | Week number with the first Monday as | 34 |
* | | the first day of week one (00-53) | |
* |-----------|-----------------------------------------|-------------|
* | %D | Short MM/DD/YY date | 09/12/07 |
* | %F | Short YYYY-MM-DD date | 2007-09-12 | **********
* | %x | Date representation | 08/23/01 |
* | %X | Time representation | 14:55:02 |
* | %y | Last two digits of the year (00-99) | 21 |
* | %Y | Year | 2021 |
* |-----------|-----------------------------------------|-------------|
* | %B | Full month name | January |
* | %b | or %h Abbreviated month name | Jan |
* | %d | Day of the month (01-31) | 12 |
* | %j | Day of the year (001-366) | 78 |
* | %m | Month as a decimal number (01-12) | 02 |
* |-----------|-----------------------------------------|-------------|
* | %h | Abbreviated month name (same as %b) | Aug |
* | %H | The hour in 24-hour format (00-23) | 13 |
* | %I | The hour in 12-hour format (01-12) | 08 |
* | %M | Minute (00-59) | 12 |
* | %p | AM or PM | AM |
* | %r | 12-hour clock time | 05:12:32 am |
* | %R | 24-hour time HH:MM | 13:22 |
* | %S | Second (00-59) | 32 |
* | %T | Time format HH:MM:SS | 09:12:01 | **********
* |-----------|-----------------------------------------|-------------|
*
* |-----------|---------------------------------------------------------------|
* | %z | ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) |
* | | If timezone cannot be determined, no characters +100 |
* |-----------|---------------------------------------------------------------|
* | %Z | Timezone name or abbreviation |
* | | If timezone cannot be determined, no characters CDT |
* |-----------|---------------------------------------------------------------|
*
*
* =====================================================================================
*
*
* 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(1610927970); // 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) 1610927970
* getLocalEpoch() // (unsigned long) 1610927970 // 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
* // %A --> Full weekday name
* // %B --> Full month name
* // %d --> Day of the month (01-31)
* // %Y --> Year
* // %H --> Hour (00-23)
* // %M --> Minute (00-59)
* // %S --> Second (00-59)
*
* =====================================================================================
*
*
*/
//
#include <WiFi.h>
//
#include <ESP32Time.h>
//
#define NTP_SERVER "2.pool.ntp.org"
#define UTC_OFFSET 3600
#define UTC_OFFSET_DST 3600
tm tm;
ESP32Time rtc;
//
struct stct_INCOMINGcall_t {
//
// << Byte ARRAY >> used as buffer for FRAM memory operations *** Byte ARRAY -NOT CHAR- ***
uint8_t callBFFR8[25]; // [TELEPHONE NUMBER(19Byte) + DATE(3Byte-YYMMDD) + TIME(2Byte-hhmm) + LENGTH of the Telephone Number]
// callBFFR8[TELEPHONE NUMBER(19Byte) + DATE(3Byte-YYMMDD) + TIME(2Byte-hhmm) + LENGTH of the Telephone Number] *** Byte ARRAY -NOT CHAR- ***
//
// -Telephone NUMBER- (19+'\0'Byte)
char charNMBR[20] = {'\0'}; // "1234567890123456789" (19+'\0' in C-style string)
// -NAME- (19+'\0'Byte)
char charNAME[20] = {'\0'}; // "Abcdefghijklmnopqrs" (19+'\0' in C-style string)
// -Date+Time- (18+'\0\0'Byte)
char charDATE[20] = {'\0'}; // "17/01/2024 12:13:14" (19+'\0' in C-style string)
// -----------------------------------------------
// 60 Byte [stored in FRAM] for each INCOMING call
// -----------------------------------------------
//
// struct data size: 25+20+20+20=85 Byte
} Gstct_INCOMINGcall;
//
// *****************************************************************************
// *****************************************************************************
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//
delay(10);
//
Serial.printf("\n\nConnecting WiFi ...\n");
//
WiFi.begin("Wokwi-GUEST", "", 6);
//
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.printf(".");
} // while
//
Serial.println();
Serial.printf("WiFi connected.\nIP address = ");
Serial.println(WiFi.localIP());
//
Serial.printf("Online\nUpdating Date and Time...\n");
// 2024-02-17T07:19:59 1708154399 Sat, 17 Feb 2024 07:19:5
//uint32_t setDate32 = 1708154399; //2024-02-17 07:19:59 1708154399 Sat 07:19:59 GMT
//uint32_t setDate32 = 1610927970; // Sun, 17 Jan 2021 23:59:30
//
//Serial.printf("\n** setup() - setDate32 (seconds) = ");Serial.println(setDate32);
//Serial.println();
//
//rtc.setTime(setDate32);
//
// 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 (0-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("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
//
// *** esp32_time.ino ***
// Copyright (c) 2021 Felix Biego
//rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 23:59:30
//rtc.setTime(1610927970); // epoch (17th Jan 2021 23:59:30)
//
// ---------set with NTP---------------
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
//setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
//initTime("CET-1CEST,M3.5.0,M10.5.0/3"); // TZ-String for Europe/Berlin
// Note: tm --> GLOBAL
if (getLocalTime(&tm)) rtc.setTimeStruct(tm);
//
// [[ here, SECONDS are not used ]]
String s_Timestamp = rtc.getTime("%d/%m/%Y %H:%M"); //:%S");<--[SECONDS, not used yet]
// s_Timestamp --> "17/01/2024 12:13" --> length=16 [[ here, SECONDS are not used ]]
// copy current Date And Time in -Gstct_INCOMINGcall.charDATE[]-
s_Timestamp.toCharArray(Gstct_INCOMINGcall.charDATE, 17); // [[ here, SECONDS are not used ]]
//
Serial.print(F("\ns_Timestamp = "));Serial.println(s_Timestamp);
//
// s_Timestamp --> "17/11/2024 12:13" --> length=16 [[ SECONDS not used ]]
// || || || || ||
// 01 34 89 11 11
// || || || 12 45
// || || || || ||__Minute -- 14-15 ______________ Year
// || || || ||_____Hour -- 11-12 || ___________ Month
// || || ||________Year -- 8-9 || || ________ Day
// || ||_____________Month -- 3-4 || || || _____ Hour
// ||________________Day -- 0-1 || || || || __ Minute
// || || || || ||
// Gstct_INCOMINGcall.callBFFR8: 2411171213 <-- 5 values -- 5 Byte --> 24,11,17,12,13
// Gstct_INCOMINGcall.callBFFR8[] position --> 19,20,21,22,23
//
// copy current Date And Time in -Gstct_INCOMINGcall.callBFFR8[]-
String sTmp = s_Timestamp.substring(0, 2);
Gstct_INCOMINGcall.callBFFR8[21] = sTmp.toInt(); // Day
sTmp = s_Timestamp.substring(3, 5);
Gstct_INCOMINGcall.callBFFR8[20] = sTmp.toInt(); // Month
sTmp = s_Timestamp.substring(8, 10);
Gstct_INCOMINGcall.callBFFR8[19] = sTmp.toInt(); // Year
sTmp = s_Timestamp.substring(11, 13);
Gstct_INCOMINGcall.callBFFR8[22] = sTmp.toInt(); // Hour
sTmp = s_Timestamp.substring(14);
Gstct_INCOMINGcall.callBFFR8[23] = sTmp.toInt(); // Minute
//
// NOTE: use LOCAL Date And Time
//
Serial.print(F("\nGstct_INCOMINGcall.callBFFR8[]: ")); //
for (uint8_t i = 0; i < 25; i++) Serial.print(Gstct_INCOMINGcall.callBFFR8[i]);
Serial.println();
/*
s_Timestamp = 07/12/2024 17:48
Gstct_INCOMINGcall.callBFFR8[]: 00000000000000000002412717480
*/
} // setup()
void loop() {
//
delay(10); // as per https://docs.wokwi.com/faq
//
// *** esp32_time.ino ***
// Copyright (c) 2021 Felix Biego
//rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 23:59:30 --> OK!!
//rtc.setTime(1610927970); // 17th Jan 2021 23:59:30 --> OK!!
// ---------set with NTP---------------
// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// // Note: tm --> GLOBAL
// if (getLocalTime(&tm)) rtc.setTimeStruct(tm);
//
//
Serial.println(rtc.getTime("%F_T_%X")); // 2021-01-17_T_23:59:30
Serial.println(rtc.getTime("%A")); // Saturday
Serial.println(rtc.getTime("%a")); // Sat
Serial.println(rtc.getTime("%w")); // 0=Sunday ... 6=Saturday
//
// [2024-12-04_T_11:46:23] --> 202412041146
Serial.println(rtc.getTime("%Y%m%d%H%M")); // [2024-12-04_T_11:46:23] --> 202412041146
//
delay(10000);
//
} // loop()
/*
Output:
Connecting WiFi ...
....
WiFi connected.
IP address = 10.10.0.2
Online
Updating Date and Time...
s_Timestamp = 07/12/2024 17:48
Gstct_INCOMINGcall.callBFFR8[]: 00000000000000000002412717480
2024-12-07_T_17:48:25
Saturday
Sat
6
202412071748
......
*/