From af3249c347f8544cfda3a87439da8906d33c418b Mon Sep 17 00:00:00 2001 From: Sara Damiano Date: Mon, 17 Feb 2020 18:22:25 -0500 Subject: [PATCH] Add parsed network time Signed-off-by: Sara Damiano --- examples/AllFunctions/AllFunctions.ino | 30 ++++++++++++++-- src/TinyGsmTime.tpp | 47 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/examples/AllFunctions/AllFunctions.ino b/examples/AllFunctions/AllFunctions.ino index 10687ab..09ed456 100644 --- a/examples/AllFunctions/AllFunctions.ino +++ b/examples/AllFunctions/AllFunctions.ino @@ -77,12 +77,12 @@ // #define CALL_TARGET "+380xxxxxxxxx" // Your GPRS credentials, if any -const char apn[] = "YourAPN"; +const char apn[] = "YourAPN"; const char gprsUser[] = ""; const char gprsPass[] = ""; // Your WiFi connection credentials, if applicable -const char wifiSSID[] = "YourSSID"; +const char wifiSSID[] = "YourSSID"; const char wifiPass[] = "YourWiFiPass"; // Server details to test TCP/SSL @@ -119,6 +119,8 @@ void setup() { // !!!!!!!!!!! // Set your reset, enable, power pins here + pinMode(23, OUTPUT); + digitalWrite(23, HIGH); // !!!!!!!!!!! DBG("Wait..."); @@ -398,6 +400,30 @@ void loop() { #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME String time = modem.getGSMDateTime(DATE_FULL); DBG("Current Network Time:", time); + int year3 = 0; + int month3 = 0; + int day3 = 0; + int hour3 = 0; + int min3 = 0; + int sec3 = 0; + float timezone = 0; + for (int8_t i = 5; i; i--) { + DBG("Requesting current network time"); + if (modem.getNetworkTime(&year3, &month3, &day3, &hour3, &min3, &sec3, + &timezone)) { + DBG("Year:", year3); + DBG("Month:", month3); + DBG("Day:", day3); + DBG("Hour:", hour3); + DBG("Minute:", min3); + DBG("Second:", sec3); + DBG("Timezone:", timezone); + break; + } else { + DBG("Couldn't get network time, retrying in 10s."); + delay(10000L); + } + } #endif #if TINY_GSM_TEST_GPRS diff --git a/src/TinyGsmTime.tpp b/src/TinyGsmTime.tpp index f6fb796..259f0da 100644 --- a/src/TinyGsmTime.tpp +++ b/src/TinyGsmTime.tpp @@ -24,6 +24,10 @@ class TinyGsmTime { String getGSMDateTime(TinyGSMDateTimeFormat format) { return thisModem().getGSMDateTimeImpl(format); } + bool getNetworkTime(int* year, int* month, int* day, int* hour, int* minute, + int* second, float* timezone) { + return thisModem().getNetworkTimeImpl(year, month, day, hour, minute, second, timezone); + } /* * CRTP Helper @@ -56,6 +60,49 @@ class TinyGsmTime { } return res; } + + bool getNetworkTimeImpl(int* year, int* month, int* day, int* hour, int* minute, + int* second, float* timezone) { + thisModem().sendAT(GF("+CCLK?")); + if (thisModem().waitResponse(2000L, GF("+CCLK: \"")) != 1) { + return false; + } + + int iyear = 0; + int imonth = 0; + int iday = 0; + int ihour = 0; + int imin = 0; + int isec = 0; + int itimezone = 0; + + // Date & Time + iyear = thisModem().streamGetIntBefore('/'); + imonth = thisModem().streamGetIntBefore('/'); + iday = thisModem().streamGetIntBefore(','); + ihour = thisModem().streamGetIntBefore(':'); + imin = thisModem().streamGetIntBefore(':'); + isec = thisModem().streamGetIntLength(2); + char tzSign = thisModem().stream.read(); + itimezone = thisModem().streamGetIntBefore('\n'); + if (strcmp(tzSign, '-') == 0) { + itimezone = itimezone * -1; + } + + // Set pointers + if (iyear < 2000) iyear += 2000; + if (year != NULL) *year = iyear; + if (month != NULL) *month = imonth; + if (day != NULL) *day = iday; + if (hour != NULL) *hour = ihour; + if (minute != NULL) *minute = imin; + if (second != NULL) *second = isec; + if (timezone != NULL) *timezone = static_cast(itimezone)/ 4.0; + + // Final OK + thisModem().waitResponse(); + return true; + } }; #endif // SRC_TINYGSMTIME_H_