Skip to content

Commit

Permalink
Merge pull request #49 from gonzabrusco/fix-NTPServerSync
Browse files Browse the repository at this point in the history
Fix ntp server sync
  • Loading branch information
SRGDamia1 authored May 20, 2024
2 parents 8136a25 + 2d1fc04 commit 566aee6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/TinyGsmClientSIM7000.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

/*
Expand Down
5 changes: 4 additions & 1 deletion src/TinyGsmClientSIM7000SSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

/*
Expand Down Expand Up @@ -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
Expand Down
45 changes: 43 additions & 2 deletions src/TinyGsmClientSIM7080.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/TinyGsmClientSIM70xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "TinyGsmModem.tpp"
#include "TinyGsmSMS.tpp"
#include "TinyGsmTime.tpp"
#include "TinyGsmNTP.tpp"
#include "TinyGsmGSMLocation.tpp"

enum SIM70xxRegStatus {
Expand All @@ -41,15 +40,13 @@ 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>>;
friend class TinyGsmGPRS<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>>;

Expand Down
9 changes: 7 additions & 2 deletions src/TinyGsmNTP.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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"));
Expand All @@ -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 {
Expand Down

0 comments on commit 566aee6

Please sign in to comment.