From 28bf6ff6b7589aa8648939c23aba088917ca3c41 Mon Sep 17 00:00:00 2001 From: paulvha Date: Fri, 16 Feb 2024 17:40:32 +0100 Subject: [PATCH 1/4] add getTime --- .../WiFiS3/examples/WiFiTime/WiFiTime.ino | 123 ++++++++++++++++++ .../examples/WiFiTime/arduino_secrets.h | 2 + libraries/WiFiS3/src/WiFi.cpp | 14 +- 3 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino create mode 100644 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 100644 index 00000000..ddd55d74 --- /dev/null +++ b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino @@ -0,0 +1,123 @@ +/* + 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 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: + 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() { +/* -------------------------------------------------------------------------- */ + + unsigned long EpochTime; + + EpochTime = WiFi.getTime(); + + if (EpochTime > 0) { + UpdateRTC(EpochTime); + } + else { + Serial.println("Error during reading epoch time."); + } + + Serial.println(); + delay(10000); +} + +/* -------------------------------------------------------------------------- */ +void UpdateRTC(time_t EpochTime) { +/* -------------------------------------------------------------------------- */ + + 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)); +} + +/* -------------------------------------------------------------------------- */ +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 100644 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 6cd63526..c6acbc15 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -542,18 +542,22 @@ int CWifi::hostByName(const char* aHostname, IPAddress& aResult) { return 0; } - - uint8_t CWifi::reasonCode() { return 0; } +/* ----------------------------------------------*/ unsigned long CWifi::getTime() { - return 0; +/* ----------------------------------------------*/ + 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; } - - void CWifi::setTimeout(unsigned long timeout) { _timeout = timeout; } From a2cceca9877e22a8c3739956aa2c0c6e51fa86da Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 21 Nov 2024 16:24:36 +0100 Subject: [PATCH 2/4] WiFiTime: add info about minimum required firmware version --- libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino index ddd55d74..415aca0c 100644 --- a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino +++ b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino @@ -3,7 +3,7 @@ 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. + It requires at least version 0.5.0 of USB Wifi bridge firmware and WiFiS3 library. This example is written for a network using WPA encryption. For WEP or WPA, change the WiFi.begin() call accordingly. @@ -80,6 +80,7 @@ void loop() { } else { Serial.println("Error during reading epoch time."); + Serial.println("Make sure your WiFi firmware version is greater than 0.5.0"); } Serial.println(); From 64ffce964cf4f57f4011d0368538659fc7491004 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 21 Nov 2024 16:30:18 +0100 Subject: [PATCH 3/4] getTime: remove not necessary variable --- libraries/WiFiS3/src/WiFi.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/WiFiS3/src/WiFi.cpp b/libraries/WiFiS3/src/WiFi.cpp index c6acbc15..0717f673 100644 --- a/libraries/WiFiS3/src/WiFi.cpp +++ b/libraries/WiFiS3/src/WiFi.cpp @@ -551,11 +551,10 @@ unsigned long CWifi::getTime() { /* ----------------------------------------------*/ 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 strtol(res.c_str(), NULL, 10); } - return tt; + return 0; } void CWifi::setTimeout(unsigned long timeout) { From 0afd739ca4d71e2e49bf3fcded3a9229f2de417e Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 26 Nov 2024 15:28:44 +0100 Subject: [PATCH 4/4] WiFiTime: remove WIFI_FIRMWARE_LATEST_VERSION check --- libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino index 415aca0c..22ab7495 100644 --- a/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino +++ b/libraries/WiFiS3/examples/WiFiTime/WiFiTime.ino @@ -41,14 +41,7 @@ void setup() { // 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 !"); + Serial.println("Communication with WiFi module failed."); // don't continue while (true); }