Skip to content

Add flags to handle localtime/utc selection and NTPSync #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/ArduinoCellular.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool ArduinoCellular::connect(String apn, String username, String password, bool
return true;
}

if(connectToGPRS(apn.c_str(), username.c_str(), password.c_str())){
if(connectToGPRS(apn.c_str(), username.c_str(), password.c_str(), waitForever)){
auto response = this->sendATCommand("+QIDNSCFG=1,\"8.8.8.8\",\"8.8.4.4\"");

if(response.indexOf("OK") != -1){
Expand Down Expand Up @@ -126,17 +126,26 @@ Time ArduinoCellular::getGPSTime(){
return Time(year, month, day, hour, minute, second);
}

Time ArduinoCellular::getCellularTime(){
Time ArduinoCellular::getCellularTime(bool localTime, bool forceNTPSync) {
int year = 1970;
int month = 1;
int day = 1;
int hour = 0;
int minute = 0;
int second = 0;
float tz;
if (modem.NTPServerSync() == 0) {

if ((needNTPSync || forceNTPSync) && (modem.NTPServerSync() != 0)) {
return Time(year, month, day, hour, minute, second);
}
needNTPSync = false;

if (localTime) {
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
} else {
modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz);
}

return Time(year, month, day, hour, minute, second);
}

Expand Down Expand Up @@ -187,11 +196,16 @@ bool ArduinoCellular::isConnectedToOperator(){
return modem.isNetworkConnected();
}

bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass){
bool ArduinoCellular::connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass, bool waitForever){
if(this->debugStream != nullptr){
this->debugStream->println("Connecting to 4G network...");
}
while(!modem.gprsConnect(apn, gprsUser, gprsPass)) {

if(!waitForever) {
return false;
}

if(this->debugStream != nullptr){
this->debugStream->print(".");
}
Expand Down Expand Up @@ -243,7 +257,7 @@ bool ArduinoCellular::awaitNetworkRegistration(bool waitForever){
this->debugStream->println("Waiting for network registration...");
}
while (!modem.waitForNetwork(waitForNetworkTimeout)) {

if(!waitForever) {
return false;
}
Expand Down
7 changes: 4 additions & 3 deletions src/ArduinoCellular.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ArduinoCellular {
* @brief Gets the current time from the network.
* @return The current time.
*/
Time getCellularTime();
Time getCellularTime(bool localTime = true, bool forceNTPSync = false);

/**
* @brief Gets the current time from the GPS module.
Expand Down Expand Up @@ -276,8 +276,7 @@ class ArduinoCellular {
SimStatus getSimStatus();

private:
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);

bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass, bool waitForever = true);

/**
* @brief Waits for network registration. (Blocking call)
Expand All @@ -304,6 +303,8 @@ class ArduinoCellular {
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */

static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */

bool needNTPSync = true; /**< Flag to indicate if NTP synchronization is needed. */
};


Expand Down
28 changes: 22 additions & 6 deletions src/TimeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,35 @@ class Time {
* @return The time in UNIX timestamp format.
*/
String getUNIXTimestampString() {
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
return String(timestamp);
return String(getUNIXTimestamp());
}

/**
* Returns the time in UNIX timestamp format.
* @return The time in UNIX timestamp format.
*/
unsigned long getUNIXTimestamp() {
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
return timestamp;
struct tm t =
{
0 /* tm_sec */,
0 /* tm_min */,
0 /* tm_hour */,
0 /* tm_mday */,
0 /* tm_mon */,
0 /* tm_year */,
0 /* tm_wday */,
0 /* tm_yday */,
0 /* tm_isdst */
};
t.tm_mon = month - 1;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_hour = hour;
t.tm_min = minute;
t.tm_sec = second;
t.tm_isdst = -1;

return mktime(&t);
}

/**
Expand Down
Loading