From f5dcf22d0714a5dc5fd3cf2015e20f933793c5ef Mon Sep 17 00:00:00 2001 From: paulvha Date: Fri, 16 Feb 2024 17:40:32 +0100 Subject: [PATCH 1/5] add gettime --- .../WiFiS3/examples/WiFiTime/WiFiTime.ino | 162 ++++++++++++++++++ .../examples/WiFiTime/arduino_secrets.h | 2 + libraries/WiFiS3/src/WiFi.cpp | 24 ++- libraries/WiFiS3/src/WiFi.h | 12 +- 4 files changed, 197 insertions(+), 3 deletions(-) create mode 100755 libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino create mode 100755 libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h diff --git a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino new file mode 100755 index 00000000..02dab485 --- /dev/null +++ b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino @@ -0,0 +1,162 @@ +/* + Get local time from NTP server + + This sketch will obtain the correct local time from the NTP-servers. + + In order to get the correct time, taking in account the daylightsavings, a POSIX + formatted timezone string MUST be provided before doing getTime(). + + There are a large number of good articles on the internet + https://github.com/G6EJD/ESP32-Time-Services-and-SETENV-variable/blob/master/README.md + https://developer.ibm.com/articles/au-aix-posix/ + A pre-formatted timezone list : https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv + + The output can be formatted according the strftime() function. + The default format, if nothing provided is "%d-%b-%y, %H:%M:%S" + More information on: https://cplusplus.com/reference/ctime/strftime/ + + There are 2 calls: + void setTZ(const char * tz); + const char * getTime(const char * format = "%d-%b-%y, %H:%M:%S"); + + Usage + Set the Timezone information one time in setup(): void WiFi.setTZ("TZ_INFO"); + and then just call WiFi.getTime() or call WiFi.getTime("FORMAT"). + + It requires the latest USB Wifi bridge firmware level and WiFiS3 library. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the WiFi.begin() call accordingly. + + created 15 February 2024 + by paulvha + + */ + +#include "WiFiS3.h" +#include "arduino_secrets.h" + +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) + +int status = WL_IDLE_STATUS; + +/* -------------------------------------------------------------------------- */ +void setup() { +/* -------------------------------------------------------------------------- */ + //Initialize serial and wait for port to open: + Serial.begin(115200); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed. freeze !"); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { + Serial.println("Please upgrade to the WiFi USB bridge firmware. freeze !"); + // don't continue + while (true); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + printWifiStatus(); +} + +/* -------------------------------------------------------------------------- */ +void loop() { +/* -------------------------------------------------------------------------- */ + const char *tt; + + //=========================================================// + + // this could be in setup() - this is Europe / London + WiFi.setTZ("GMT0BST,M3.5.0/01,M10.5.0/02"); + + // using the default format. + // Output looks like: 15-Feb-24, 17:02:48 + tt = WiFi.getTime(); + + if (strlen(tt) > 0) { + Serial.print("Current time in London "); + Serial.println(tt); + } + else { + Serial.println("Error during reading London."); + } + + //=========================================================// + + // this could be in setup() - this is Europe / Amsterdam + WiFi.setTZ("CET-1CEST,M3.5.0/2,M10.5.0/3"); + + // user defined format definition, + // Output looks like 06:02PM. + // see https://cplusplus.com/reference/ctime/strftime/ + tt = WiFi.getTime("%I:%M%p."); + + if (strlen(tt) > 0) { + Serial.print("Current time in Amsterdam "); + Serial.println(tt); + } + else { + Serial.println("Error during reading Amsterdam."); + } + + //=========================================================// + + // this could be in setup() - this is Eastern Standard Time + WiFi.setTZ("EST5EDT,M3.2.0/2,M11.1.0"); + + // user defined format definition, + // Output looks like 12:02:37. + // see https://cplusplus.com/reference/ctime/strftime/ + tt = WiFi.getTime("%H:%M:%S"); + + if (strlen(tt) > 0) { + Serial.print("Current time US (EST) Time "); + Serial.println(tt); + } + else { + Serial.println("Error during reading US (EST) Time."); + } + + Serial.println(); + delay(5000); +} + +/* -------------------------------------------------------------------------- */ +void printWifiStatus() { +/* -------------------------------------------------------------------------- */ + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} diff --git a/libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h b/libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h new file mode 100755 index 00000000..0c9fdd55 --- /dev/null +++ b/libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS "" diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index 746ed337..090f8016 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -548,8 +548,28 @@ uint8_t CWifi::reasonCode() { return 0; } -unsigned long CWifi::getTime() { - return 0; +static char TimeZone[35] = {0}; +/* ----------------------------------------------*/ +void CWifi::setTZ(const char * tz) { +/* ----------------------------------------------*/ + memset(TimeZone,0x00,35); + strncpy(TimeZone,tz,sizeof(TimeZone)-1); +} + +/* ----------------------------------------------*/ +const char * CWifi::getTime(const char * format) { +/* ----------------------------------------------*/ + modem.begin(); + string res = ""; + + // if Timezone or format is missing + if (strlen(TimeZone) == 0 || strlen(format) == 0) return NULL; + + if(modem.write(string(PROMPT(_GETTIME)),res,"%s,\"%s\",\"%s\"\r\n", CMD_WRITE(_GETTIME), TimeZone, format )) { + return res.c_str(); + } + + return NULL; } diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index 9958b7c1..c6f49814 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -274,7 +274,17 @@ class CWifi { */ int hostByName(const char* aHostname, IPAddress& aResult); - unsigned long getTime(); + /* + * Provide Timezone information upfront setTZ(). + * Use POSIX timezone format https://developer.ibm.com/articles/au-aix-posix/ + * You can choose your time zone also from this list + * https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv + * + * The output can be formatted according the strftime() function + * see : https://cplusplus.com/reference/ctime/strftime/ + */ + void setTZ(const char * tz); + const char * getTime(const char * format = "%d-%b-%y, %H:%M:%S"); void setTimeout(unsigned long timeout); From a47771eb69761096c6bcfb01c900d89d6e897d84 Mon Sep 17 00:00:00 2001 From: paulvha Date: Wed, 21 Feb 2024 13:14:18 +0100 Subject: [PATCH 2/5] getTime --- .../WiFiS3/examples/WiFiTime/WiFiTime.ino | 113 ++++++------------ .../examples/WiFiTime/arduino_secrets.h | 0 libraries/WiFiS3/src/WiFi.cpp | 27 +++-- libraries/WiFiS3/src/WiFi.h | 12 +- 4 files changed, 58 insertions(+), 94 deletions(-) mode change 100755 => 100644 libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino mode change 100755 => 100644 libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h diff --git a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino old mode 100755 new mode 100644 index 02dab485..0a103d8c --- a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino +++ b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino @@ -1,58 +1,45 @@ /* - Get local time from NTP server - - This sketch will obtain the correct local time from the NTP-servers. - - In order to get the correct time, taking in account the daylightsavings, a POSIX - formatted timezone string MUST be provided before doing getTime(). - - There are a large number of good articles on the internet - https://github.com/G6EJD/ESP32-Time-Services-and-SETENV-variable/blob/master/README.md - https://developer.ibm.com/articles/au-aix-posix/ - A pre-formatted timezone list : https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv - - The output can be formatted according the strftime() function. - The default format, if nothing provided is "%d-%b-%y, %H:%M:%S" - More information on: https://cplusplus.com/reference/ctime/strftime/ - - There are 2 calls: - void setTZ(const char * tz); - const char * getTime(const char * format = "%d-%b-%y, %H:%M:%S"); - - Usage - Set the Timezone information one time in setup(): void WiFi.setTZ("TZ_INFO"); - and then just call WiFi.getTime() or call WiFi.getTime("FORMAT"). + Get the time in seconds since January 1st, 1970. + + The time is retrieved with the WiFi module by fetching the NTP time from an NTP server. It requires the latest USB Wifi bridge firmware level and WiFiS3 library. This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly. - created 15 February 2024 - by paulvha + created 21 february 2024 - */ +*/ #include "WiFiS3.h" #include "arduino_secrets.h" +#include "RTC.h" ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int keyIndex = 0; // your network key index number (needed only for WEP) +/// set offsets to GMT from local +#define GMTOffset_hour 1 // # hours difference to GMT +#define DayLightSaving 0 // 1 = daylight saving is active + int status = WL_IDLE_STATUS; /* -------------------------------------------------------------------------- */ void setup() { /* -------------------------------------------------------------------------- */ + //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } + + RTC.begin(); - // check for the WiFi module: + // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed. freeze !"); // don't continue @@ -83,63 +70,37 @@ void setup() { /* -------------------------------------------------------------------------- */ void loop() { /* -------------------------------------------------------------------------- */ - const char *tt; - - //=========================================================// - // this could be in setup() - this is Europe / London - WiFi.setTZ("GMT0BST,M3.5.0/01,M10.5.0/02"); + unsigned long EpochTime; + + EpochTime = WiFi.getTime(); - // using the default format. - // Output looks like: 15-Feb-24, 17:02:48 - tt = WiFi.getTime(); - - if (strlen(tt) > 0) { - Serial.print("Current time in London "); - Serial.println(tt); + if (EpochTime > 0) { + UpdateRTC(EpochTime); } else { - Serial.println("Error during reading London."); + Serial.println("Error during reading epoch time."); } - //=========================================================// - - // this could be in setup() - this is Europe / Amsterdam - WiFi.setTZ("CET-1CEST,M3.5.0/2,M10.5.0/3"); - - // user defined format definition, - // Output looks like 06:02PM. - // see https://cplusplus.com/reference/ctime/strftime/ - tt = WiFi.getTime("%I:%M%p."); - - if (strlen(tt) > 0) { - Serial.print("Current time in Amsterdam "); - Serial.println(tt); - } - else { - Serial.println("Error during reading Amsterdam."); - } - - //=========================================================// - - // this could be in setup() - this is Eastern Standard Time - WiFi.setTZ("EST5EDT,M3.2.0/2,M11.1.0"); - - // user defined format definition, - // Output looks like 12:02:37. - // see https://cplusplus.com/reference/ctime/strftime/ - tt = WiFi.getTime("%H:%M:%S"); + Serial.println(); + delay(10000); +} - if (strlen(tt) > 0) { - Serial.print("Current time US (EST) Time "); - Serial.println(tt); - } - else { - Serial.println("Error during reading US (EST) Time."); - } +/* -------------------------------------------------------------------------- */ +void UpdateRTC(time_t EpochTime) { +/* -------------------------------------------------------------------------- */ - Serial.println(); - delay(5000); + auto timeZoneOffsetHours = GMTOffset_hour + DayLightSaving; + auto unixTime = EpochTime + (timeZoneOffsetHours * 3600); + Serial.print("Unix time = "); + Serial.println(unixTime); + RTCTime timeToSet = RTCTime(unixTime); + RTC.setTime(timeToSet); + + // Retrieve the date and time from the RTC and print them + RTCTime currentTime; + RTC.getTime(currentTime); + Serial.println("The RTC was just set to: " + String(currentTime)); } /* -------------------------------------------------------------------------- */ diff --git a/libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h b/libraries/WiFiS3/examples/WiFiTime/arduino_secrets.h old mode 100755 new mode 100644 diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index 090f8016..ed441fd3 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -547,18 +547,23 @@ int CWifi::hostByName(const char* aHostname, IPAddress& aResult) { uint8_t CWifi::reasonCode() { return 0; } - -static char TimeZone[35] = {0}; /* ----------------------------------------------*/ -void CWifi::setTZ(const char * tz) { +unsigned long CWifi::getTime() { /* ----------------------------------------------*/ - memset(TimeZone,0x00,35); - strncpy(TimeZone,tz,sizeof(TimeZone)-1); + modem.begin(); + string res = ""; + unsigned long tt = 0; + + if(modem.write(string(PROMPT(_GETTIME)),res,"%s\r\n", CMD_WRITE(_GETTIME))) { + tt = strtol(res.c_str(), NULL, 10); + } + + return tt; } -/* ----------------------------------------------*/ +/* ----------------------------------------------* / const char * CWifi::getTime(const char * format) { -/* ----------------------------------------------*/ +/* ----------------------------------------------* / modem.begin(); string res = ""; @@ -572,7 +577,15 @@ const char * CWifi::getTime(const char * format) { return NULL; } +static char TimeZone[35] = {0}; +/* ----------------------------------------------* / +void CWifi::setTZ(const char * tz) { +/* ----------------------------------------------* / + memset(TimeZone,0x00,35); + strncpy(TimeZone,tz,sizeof(TimeZone)-1); +} +*/ void CWifi::setTimeout(unsigned long timeout) { (void)(timeout); diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index c6f49814..9958b7c1 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -274,17 +274,7 @@ class CWifi { */ int hostByName(const char* aHostname, IPAddress& aResult); - /* - * Provide Timezone information upfront setTZ(). - * Use POSIX timezone format https://developer.ibm.com/articles/au-aix-posix/ - * You can choose your time zone also from this list - * https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv - * - * The output can be formatted according the strftime() function - * see : https://cplusplus.com/reference/ctime/strftime/ - */ - void setTZ(const char * tz); - const char * getTime(const char * format = "%d-%b-%y, %H:%M:%S"); + unsigned long getTime(); void setTimeout(unsigned long timeout); From c3fd5b122e6766a34035288beea945ea21d492b6 Mon Sep 17 00:00:00 2001 From: paulvha Date: Wed, 21 Feb 2024 13:19:08 +0100 Subject: [PATCH 3/5] getTime --- libraries/WiFiS3/src/WiFi.cpp | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index ed441fd3..2da9881f 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -561,31 +561,6 @@ unsigned long CWifi::getTime() { return tt; } -/* ----------------------------------------------* / -const char * CWifi::getTime(const char * format) { -/* ----------------------------------------------* / - modem.begin(); - string res = ""; - - // if Timezone or format is missing - if (strlen(TimeZone) == 0 || strlen(format) == 0) return NULL; - - if(modem.write(string(PROMPT(_GETTIME)),res,"%s,\"%s\",\"%s\"\r\n", CMD_WRITE(_GETTIME), TimeZone, format )) { - return res.c_str(); - } - - return NULL; -} - -static char TimeZone[35] = {0}; -/* ----------------------------------------------* / -void CWifi::setTZ(const char * tz) { -/* ----------------------------------------------* / - memset(TimeZone,0x00,35); - strncpy(TimeZone,tz,sizeof(TimeZone)-1); -} - -*/ void CWifi::setTimeout(unsigned long timeout) { (void)(timeout); From 9b446790d6075c7589dade3490865aa23505e0aa Mon Sep 17 00:00:00 2001 From: paulvha Date: Wed, 21 Feb 2024 13:21:34 +0100 Subject: [PATCH 4/5] getTime --- libraries/WiFiS3/src/WiFi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index 9958b7c1..a6e9c4a8 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -270,7 +270,7 @@ class CWifi { * param aHostname: Name to be resolved * param aResult: IPAddress structure to store the returned IP address * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code + * else error code */ int hostByName(const char* aHostname, IPAddress& aResult); From 7d9db32472ebdf191195d57eb9eb02f9c8dcfba1 Mon Sep 17 00:00:00 2001 From: paulvha Date: Wed, 21 Feb 2024 13:22:32 +0100 Subject: [PATCH 5/5] getTime --- libraries/WiFiS3/src/WiFi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/WiFiS3/src/WiFi.h b/libraries/WiFiS3/src/WiFi.h index a6e9c4a8..9958b7c1 100644 --- a/libraries/WiFiS3/src/WiFi.h +++ b/libraries/WiFiS3/src/WiFi.h @@ -270,7 +270,7 @@ class CWifi { * param aHostname: Name to be resolved * param aResult: IPAddress structure to store the returned IP address * result: 1 if aIPAddrString was successfully converted to an IP address, - * else error code + * else error code */ int hostByName(const char* aHostname, IPAddress& aResult);