Skip to content

Optimized getTime management #41

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

Merged
merged 1 commit into from
Mar 21, 2019
Merged
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
8 changes: 3 additions & 5 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ static unsigned long getTime() {
debugMessage("Bogus NTP time from API, fallback to UDP method", 0);
time = NTPUtils(getTimeConnection->getUDP()).getTime();
}
#ifdef ARDUINO_ARCH_SAMD
rtc.setEpoch(time);
#endif
return time;
}

Expand Down Expand Up @@ -411,11 +414,6 @@ void ArduinoIoTCloudClass::connectionCheck()
CloudSerial.begin(9600);
CloudSerial.println("Hello from Cloud Serial!");
}
#ifdef ARDUINO_ARCH_SAMD
unsigned long const epoch = getTime();
if (epoch!=0)
rtc.setEpoch(epoch);
#endif
break;
}
}
Expand Down
28 changes: 2 additions & 26 deletions src/GSMConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, con
login(login),
pass(pass),
lastConnectionTickTime(millis()),
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
getTimeRetries(MAX_GETTIME_RETRIES) {
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
}

/******************************************************************************
Expand Down Expand Up @@ -96,24 +95,8 @@ void GSMConnectionManager::check() {
} else {
sprintf(msgBuffer, "Connected to GPRS Network");
debugMessage(msgBuffer, 2);
changeConnectionState(CONNECTION_STATE_GETTIME);
return;
}
break;
case CONNECTION_STATE_GETTIME:
debugMessage("Acquiring Time from Network", 3);
unsigned long networkTime;
networkTime = getTime();
debugMessage(".", 3, false, false);
if(networkTime > lastValidTimestamp){
lastValidTimestamp = networkTime;
sprintf(msgBuffer, "Network Time: %u", networkTime);
debugMessage(msgBuffer, 3);
changeConnectionState(CONNECTION_STATE_CONNECTED);
}else if(gsmAccess.isAccessAlive() != 1){
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
}else if (!getTimeRetries--) {
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
return;
}
break;
case CONNECTION_STATE_CONNECTED:
Expand Down Expand Up @@ -152,20 +135,13 @@ void GSMConnectionManager::changeConnectionState(NetworkConnectionState _newStat
debugMessage(msgBuffer, 2);
newInterval = CHECK_INTERVAL_CONNECTING;
break;
case CONNECTION_STATE_GETTIME:
debugMessage("Acquiring Time from Network", 3);
newInterval = CHECK_INTERVAL_GETTIME;
getTimeRetries = MAX_GETTIME_RETRIES;
break;
case CONNECTION_STATE_CONNECTED:
newInterval = CHECK_INTERVAL_CONNECTED;
break;
case CONNECTION_STATE_DISCONNECTED:
if(netConnectionState == CONNECTION_STATE_CONNECTED){
debugMessage("Disconnected from Cellular Network", 0);
debugMessage("Attempting reconnection", 0);
}else if(netConnectionState == CONNECTION_STATE_GETTIME){
debugMessage("Connection to Cellular Network lost during Time acquisition.\nAttempting reconnection", 0);
}
newInterval = CHECK_INTERVAL_DISCONNECTED;
break;
Expand Down
6 changes: 1 addition & 5 deletions src/GSMConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,17 @@ class GSMConnectionManager : public ConnectionManager {
const int CHECK_INTERVAL_IDLE = 100;
const int CHECK_INTERVAL_INIT = 100;
const int CHECK_INTERVAL_CONNECTING = 500;
const int CHECK_INTERVAL_GETTIME = 666;
const int CHECK_INTERVAL_CONNECTED = 10000;
const int CHECK_INTERVAL_RETRYING = 5000;
const int CHECK_INTERVAL_DISCONNECTED = 1000;
const int CHECK_INTERVAL_ERROR = 500;

const int MAX_GETTIME_RETRIES = 30;

const char *pin, *apn, *login, *pass;
unsigned long lastConnectionTickTime;
unsigned long getTimeRetries;
int connectionTickTimeInterval;

};

#endif /* #ifdef BOARD_HAS_GSM */

#endif /* GSM_CONNECTION_MANAGER_H_ */
#endif /* GSM_CONNECTION_MANAGER_H_ */
26 changes: 2 additions & 24 deletions src/WiFiConnectionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ WiFiConnectionManager::WiFiConnectionManager(const char *ssid, const char *pass)
ssid(ssid),
pass(pass),
lastConnectionTickTime(millis()),
connectionTickTimeInterval(CHECK_INTERVAL_IDLE),
getTimeRetries(MAX_GETTIME_RETRIES) {
connectionTickTimeInterval(CHECK_INTERVAL_IDLE) {
}

/******************************************************************************
Expand Down Expand Up @@ -92,26 +91,10 @@ void WiFiConnectionManager::check() {
} else {
sprintf(msgBuffer, "Connected to \"%s\"", ssid);
debugMessage(msgBuffer, 2);
changeConnectionState(CONNECTION_STATE_GETTIME);
changeConnectionState(CONNECTION_STATE_CONNECTED);
return;
}
break;
case CONNECTION_STATE_GETTIME:
unsigned long networkTime;
networkTime = getTime();
debugMessage(".", 3, false, false);
if(networkTime > lastValidTimestamp){
debugMessage("", 3, false, true);
lastValidTimestamp = networkTime;
sprintf(msgBuffer, "Network Time: %u", networkTime);
debugMessage(msgBuffer, 3);
changeConnectionState(CONNECTION_STATE_CONNECTED);
} else if (WiFi.status() != WL_CONNECTED) {
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
} else if (!getTimeRetries--) {
changeConnectionState(CONNECTION_STATE_DISCONNECTED);
}
break;
case CONNECTION_STATE_CONNECTED:
// keep testing connection
networkStatus = WiFi.status();
Expand Down Expand Up @@ -149,11 +132,6 @@ void WiFiConnectionManager::changeConnectionState(NetworkConnectionState _newSta
debugMessage(msgBuffer, 2);
newInterval = CHECK_INTERVAL_CONNECTING;
break;
case CONNECTION_STATE_GETTIME:
newInterval = CHECK_INTERVAL_GETTIME;
debugMessage("Acquiring Time from Network", 3);
getTimeRetries = MAX_GETTIME_RETRIES;
break;
case CONNECTION_STATE_CONNECTED:
newInterval = CHECK_INTERVAL_CONNECTED;
break;
Expand Down
6 changes: 1 addition & 5 deletions src/WiFiConnectionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,18 @@ class WiFiConnectionManager : public ConnectionManager {
const int CHECK_INTERVAL_IDLE = 100;
const int CHECK_INTERVAL_INIT = 100;
const int CHECK_INTERVAL_CONNECTING = 500;
const int CHECK_INTERVAL_GETTIME = 666;
const int CHECK_INTERVAL_CONNECTED = 10000;
const int CHECK_INTERVAL_RETRYING = 5000;
const int CHECK_INTERVAL_DISCONNECTED = 1000;
const int CHECK_INTERVAL_ERROR = 500;

const int MAX_GETTIME_RETRIES = 30;

const char *ssid, *pass;
unsigned long lastConnectionTickTime;
unsigned long getTimeRetries;

WiFiClient wifiClient;
int connectionTickTimeInterval;
};

#endif /* #ifdef BOARD_HAS_WIFI */

#endif /* WIFI_CONNECTION_MANAGER_H_ */
#endif /* WIFI_CONNECTION_MANAGER_H_ */
1 change: 0 additions & 1 deletion src/utility/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static time_t cvt_TIME(char const *time) {
NTPUtils::NTPUtils(UDP& Udp) : Udp(Udp) {}

bool NTPUtils::isTimeValid(unsigned long time) {
Serial.println("Compile time: " + String(cvt_TIME(__DATE__)));
return (time > cvt_TIME(__DATE__));
}

Expand Down