mirror of
				https://github.com/KevinMidboe/TinyGSM.git
				synced 2025-10-29 18:00:18 +00:00 
			
		
		
		
	Add parsed network time
Signed-off-by: Sara Damiano <sdamiano@stroudcenter.org>
This commit is contained in:
		| @@ -77,12 +77,12 @@ | |||||||
| // #define CALL_TARGET "+380xxxxxxxxx" | // #define CALL_TARGET "+380xxxxxxxxx" | ||||||
|  |  | ||||||
| // Your GPRS credentials, if any | // Your GPRS credentials, if any | ||||||
| const char apn[]  = "YourAPN"; | const char apn[]      = "YourAPN"; | ||||||
| const char gprsUser[] = ""; | const char gprsUser[] = ""; | ||||||
| const char gprsPass[] = ""; | const char gprsPass[] = ""; | ||||||
|  |  | ||||||
| // Your WiFi connection credentials, if applicable | // Your WiFi connection credentials, if applicable | ||||||
| const char wifiSSID[]  = "YourSSID"; | const char wifiSSID[] = "YourSSID"; | ||||||
| const char wifiPass[] = "YourWiFiPass"; | const char wifiPass[] = "YourWiFiPass"; | ||||||
|  |  | ||||||
| // Server details to test TCP/SSL | // Server details to test TCP/SSL | ||||||
| @@ -119,6 +119,8 @@ void setup() { | |||||||
|  |  | ||||||
|   // !!!!!!!!!!! |   // !!!!!!!!!!! | ||||||
|   // Set your reset, enable, power pins here |   // Set your reset, enable, power pins here | ||||||
|  |   pinMode(23, OUTPUT); | ||||||
|  |   digitalWrite(23, HIGH); | ||||||
|   // !!!!!!!!!!! |   // !!!!!!!!!!! | ||||||
|  |  | ||||||
|   DBG("Wait..."); |   DBG("Wait..."); | ||||||
| @@ -398,6 +400,30 @@ void loop() { | |||||||
| #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME | #if TINY_GSM_TEST_TIME && defined TINY_GSM_MODEM_HAS_TIME | ||||||
|   String time = modem.getGSMDateTime(DATE_FULL); |   String time = modem.getGSMDateTime(DATE_FULL); | ||||||
|   DBG("Current Network Time:", time); |   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 | #endif | ||||||
|  |  | ||||||
| #if TINY_GSM_TEST_GPRS | #if TINY_GSM_TEST_GPRS | ||||||
|   | |||||||
| @@ -24,6 +24,10 @@ class TinyGsmTime { | |||||||
|   String getGSMDateTime(TinyGSMDateTimeFormat format) { |   String getGSMDateTime(TinyGSMDateTimeFormat format) { | ||||||
|     return thisModem().getGSMDateTimeImpl(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 |    * CRTP Helper | ||||||
| @@ -56,6 +60,49 @@ class TinyGsmTime { | |||||||
|     } |     } | ||||||
|     return res; |     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<float>(itimezone)/ 4.0; | ||||||
|  |  | ||||||
|  |     // Final OK | ||||||
|  |     thisModem().waitResponse(); | ||||||
|  |     return true; | ||||||
|  |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #endif  // SRC_TINYGSMTIME_H_ | #endif  // SRC_TINYGSMTIME_H_ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user