Merge pull request #49 from gonzabrusco/fix-NTPServerSync

Fix ntp server sync
This commit is contained in:
Sara Damiano
2024-05-20 13:38:16 -04:00
committed by GitHub
5 changed files with 57 additions and 9 deletions

View File

@@ -17,11 +17,13 @@
#include "TinyGsmClientSIM70xx.h"
#include "TinyGsmTCP.tpp"
#include "TinyGsmNTP.tpp"
class TinyGsmSim7000 : public TinyGsmSim70xx<TinyGsmSim7000>,
public TinyGsmNTP<TinyGsmSim7000>,
public TinyGsmTCP<TinyGsmSim7000, TINY_GSM_MUX_COUNT> {
friend class TinyGsmSim70xx<TinyGsmSim7000>;
friend class TinyGsmNTP<TinyGsmSim7000>;
friend class TinyGsmTCP<TinyGsmSim7000, TINY_GSM_MUX_COUNT>;
/*

View File

@@ -18,13 +18,16 @@
#include "TinyGsmClientSIM70xx.h"
#include "TinyGsmTCP.tpp"
#include "TinyGsmSSL.tpp"
#include "TinyGsmNTP.tpp"
class TinyGsmSim7000SSL
: public TinyGsmSim70xx<TinyGsmSim7000SSL>,
public TinyGsmTCP<TinyGsmSim7000SSL, TINY_GSM_MUX_COUNT>,
public TinyGsmNTP<TinyGsmSim7000SSL>,
public TinyGsmSSL<TinyGsmSim7000SSL> {
friend class TinyGsmSim70xx<TinyGsmSim7000SSL>;
friend class TinyGsmTCP<TinyGsmSim7000SSL, TINY_GSM_MUX_COUNT>;
friend class TinyGsmNTP<TinyGsmSim7000SSL>;
friend class TinyGsmSSL<TinyGsmSim7000SSL>;
/*
@@ -242,7 +245,7 @@ class TinyGsmSim7000SSL
// 2: CHAP
// 3: PAP or CHAP
if (pwd && strlen(pwd) > 0 && user && strlen(user) > 0) {
sendAT(GF("+CNCFG=1,\""), apn, "\",\"", "\",\"", user, pwd, '"');
sendAT(GF("+CNCFG=1,\""), apn, "\",\"", "\",\"", user, pwd, "\",3");
waitResponse();
} else if (user && strlen(user) > 0) {
// Set the user name only

View File

@@ -18,11 +18,14 @@
#include "TinyGsmClientSIM70xx.h"
#include "TinyGsmTCP.tpp"
#include "TinyGsmSSL.tpp"
#include "TinyGsmNTP.tpp"
class TinyGsmSim7080 : public TinyGsmSim70xx<TinyGsmSim7080>,
public TinyGsmNTP<TinyGsmSim7080>,
public TinyGsmTCP<TinyGsmSim7080, TINY_GSM_MUX_COUNT>,
public TinyGsmSSL<TinyGsmSim7080> {
friend class TinyGsmSim70xx<TinyGsmSim7080>;
friend class TinyGsmNTP<TinyGsmSim7080>;
friend class TinyGsmTCP<TinyGsmSim7080, TINY_GSM_MUX_COUNT>;
friend class TinyGsmSSL<TinyGsmSim7080>;
@@ -247,7 +250,7 @@ class TinyGsmSim7080 : public TinyGsmSim70xx<TinyGsmSim7080>,
// 2: CHAP
// 3: PAP or CHAP
if (pwd && strlen(pwd) > 0 && user && strlen(user) > 0) {
sendAT(GF("+CNCFG=0,1,\""), apn, "\",\"", user, "\",\"", pwd, '"');
sendAT(GF("+CNCFG=0,1,\""), apn, "\",\"", user, "\",\"", pwd, "\",3");
waitResponse();
} else if (user && strlen(user) > 0) {
// Set the user name only
@@ -317,7 +320,45 @@ class TinyGsmSim7080 : public TinyGsmSim70xx<TinyGsmSim7080>,
/*
* NTP server functions
*/
// Can sync with server using CNTP as per template
byte NTPServerSyncImpl(String server = "pool.ntp.org", int TimeZone = 0) {
// Set GPRS bearer profile to associate with NTP sync
// this may fail, it's not supported by all modules
sendAT(GF("+CNTPCID=0")); // CID must be 0. With 1 (like other modules) does not work!
waitResponse(10000L);
// Set NTP server and timezone
sendAT(GF("+CNTP=\""), server, "\",", String(TimeZone));
if (waitResponse(10000L) != 1) { return -1; }
// Request network synchronization
sendAT(GF("+CNTP"));
if (waitResponse(10000L, GF("+CNTP:"))) {
String result = stream.readStringUntil('\n');
// Check for ',' in case the module appends the time next to the return code. Eg: +CNTP: <code>[,<time>]
int index = result.indexOf(',');
if(index > 0) {
result.remove(index);
}
result.trim();
if (TinyGsmIsValidNumber(result)) { return result.toInt(); }
} else {
return -1;
}
return -1;
}
String ShowNTPErrorImpl(byte error) {
switch (error) {
case 1: return "Network time synchronization is successful";
case 61: return "Network error";
case 62: return "DNS resolution error";
case 63: return "Connection error";
case 64: return "Service response error";
case 65: return "Service response timeout";
default: return "Unknown error: " + String(error);
}
}
/*
* Battery functions

View File

@@ -22,7 +22,6 @@
#include "TinyGsmModem.tpp"
#include "TinyGsmSMS.tpp"
#include "TinyGsmTime.tpp"
#include "TinyGsmNTP.tpp"
#include "TinyGsmGSMLocation.tpp"
enum SIM70xxRegStatus {
@@ -41,7 +40,6 @@ class TinyGsmSim70xx : public TinyGsmModem<TinyGsmSim70xx<modemType>>,
public TinyGsmSMS<TinyGsmSim70xx<modemType>>,
public TinyGsmGPS<TinyGsmSim70xx<modemType>>,
public TinyGsmTime<TinyGsmSim70xx<modemType>>,
public TinyGsmNTP<TinyGsmSim70xx<modemType>>,
public TinyGsmBattery<TinyGsmSim70xx<modemType>>,
public TinyGsmGSMLocation<TinyGsmSim70xx<modemType>> {
friend class TinyGsmModem<TinyGsmSim70xx<modemType>>;
@@ -49,7 +47,6 @@ class TinyGsmSim70xx : public TinyGsmModem<TinyGsmSim70xx<modemType>>,
friend class TinyGsmSMS<TinyGsmSim70xx<modemType>>;
friend class TinyGsmGPS<TinyGsmSim70xx<modemType>>;
friend class TinyGsmTime<TinyGsmSim70xx<modemType>>;
friend class TinyGsmNTP<TinyGsmSim70xx<modemType>>;
friend class TinyGsmBattery<TinyGsmSim70xx<modemType>>;
friend class TinyGsmGSMLocation<TinyGsmSim70xx<modemType>>;

View File

@@ -32,7 +32,7 @@ class TinyGsmNTP {
return true;
}
byte NTPServerSync(String server = "pool.ntp.org", byte TimeZone = 3) {
byte NTPServerSync(String server = "pool.ntp.org", int TimeZone = 0) {
return thisModem().NTPServerSyncImpl(server, TimeZone);
}
String ShowNTPError(byte error) {
@@ -54,7 +54,7 @@ class TinyGsmNTP {
* NTP server functions
*/
protected:
byte NTPServerSyncImpl(String server = "pool.ntp.org", byte TimeZone = 3) {
byte NTPServerSyncImpl(String server = "pool.ntp.org", int TimeZone = 0) {
// Set GPRS bearer profile to associate with NTP sync
// this may fail, it's not supported by all modules
thisModem().sendAT(GF("+CNTPCID=1"));
@@ -68,6 +68,11 @@ class TinyGsmNTP {
thisModem().sendAT(GF("+CNTP"));
if (thisModem().waitResponse(10000L, GF("+CNTP:"))) {
String result = thisModem().stream.readStringUntil('\n');
// Check for ',' in case the module appends the time next to the return code. Eg: +CNTP: <code>[,<time>]
int index = result.indexOf(',');
if(index > 0) {
result.remove(index);
}
result.trim();
if (TinyGsmIsValidNumber(result)) { return result.toInt(); }
} else {