Skip to content

Commit

Permalink
Implemented auto NTP feature
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmicro committed May 31, 2021
1 parent 2f6e8a9 commit 73b1246
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/osw_config_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OswConfigKeyRGB;
namespace OswConfigAllKeys {
extern OswConfigKeyString hostname;
extern OswConfigKeyBool wifiBootEnabled;
extern OswConfigKeyBool wifiAlwaysNTPEnabled;
extern OswConfigKeyString wifiSsid;
extern OswConfigKeyPassword wifiPass;
extern OswConfigKeyRGB themeBackgroundColor;
Expand Down
4 changes: 3 additions & 1 deletion include/services/OswServiceTaskWiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OswServiceTaskWiFi : public OswServiceTask {
WiFiClass* getNativeHandler();
bool isConnected();
IPAddress getIP(); /// Either get ip of this ap client it connected and enabled or station if enabled
//TODO general ssid&pass method (if pass is from client it will return a masked string instead)
void queueTimeUpdateViaNTP();

//WiFi (client)
bool isWiFiEnabled();
Expand All @@ -47,6 +47,8 @@ class OswServiceTaskWiFi : public OswServiceTask {
bool m_enableClient = false;
bool m_enableStation = false;
bool m_enabledStationByAutoAP = false;
bool m_queuedNTPUpdate = false; //Will be set to true it this feature is active
bool m_waitingForNTPUpdate = false;
time_t m_autoAPTimeout = 0;
String m_hostname;
String m_clientSSID;
Expand Down
6 changes: 4 additions & 2 deletions src/osw_config_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace OswConfigAllKeys {
// TODO Translate all this?
OswConfigKeyString hostname("i", "WiFi", "Hostname", "Used e.g. for the wifi station", DEVICE_NAME);
OswConfigKeyBool wifiBootEnabled("j", "WiFi", "Enable on boot", "This will drain your battery faster!", false);
OswConfigKeyBool wifiAlwaysNTPEnabled("k", "WiFi", "Always fetch time (when connected)", nullptr, true);
OswConfigKeyString wifiSsid("a", "WiFi", "SSID", "Your wifi name", CONFIG_WIFI_SSID);
OswConfigKeyPassword wifiPass("b", "WiFi", "Password", nullptr, CONFIG_WIFI_PASS);

Expand Down Expand Up @@ -48,10 +49,11 @@ OswConfigKeyShort timeZone("h", "Date & Time", "Timezone", "Number of offset hou
} // namespace OswConfigAllKeys

// ...and also here, if you want to load them during boot and make them available in the configuration ui
const unsigned char oswConfigKeysCount = 25; // <------------- DON'T FORGET THIS ONE IF YOU EDIT BELOW ;)
const unsigned char oswConfigKeysCount = 26; // <------------- DON'T FORGET THIS ONE IF YOU EDIT BELOW ;)
OswConfigKey* oswConfigKeys[] = {
// wifi (2)
&OswConfigAllKeys::hostname, &OswConfigAllKeys::wifiSsid, &OswConfigAllKeys::wifiPass, &OswConfigAllKeys::wifiBootEnabled,
&OswConfigAllKeys::hostname, &OswConfigAllKeys::wifiSsid, &OswConfigAllKeys::wifiPass,
&OswConfigAllKeys::wifiBootEnabled, &OswConfigAllKeys::wifiAlwaysNTPEnabled,
// display (8)
&OswConfigAllKeys::settingDisplayTimeout, &OswConfigAllKeys::settingDisplayBrightness,
&OswConfigAllKeys::settingDisplayOverlays, &OswConfigAllKeys::settingDisplayOverlaysOnWatchScreen,
Expand Down
27 changes: 25 additions & 2 deletions src/services/OswServiceTaskWiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ void OswServiceTaskWiFi::loop(OswHal* hal) {
Serial.println(String(__FILE__) + ": [AutoAP] Active (password is " + this->m_stationPass + ").");
#endif
}

if(this->m_queuedNTPUpdate and WiFi.status() == WL_CONNECTED) {
configTime(OswConfigAllKeys::timeZone.get() * 3600 + 3600, OswConfigAllKeys::daylightOffset.get() * 3600, "pool.ntp.org", "time.nist.gov");
this->m_queuedNTPUpdate = false;
this->m_waitingForNTPUpdate = true;
#ifdef DEBUG
Serial.println(String(__FILE__) + ": [TimeViaNTP] Started update...");
#endif
}

// sometimes time(nullptr) returns seconds since boot
// so check the request was resolved
if (this->m_waitingForNTPUpdate and time(nullptr) > 1600000000) {
this->m_waitingForNTPUpdate = false;
#ifdef DEBUG
Serial.println(String(__FILE__) + ": [TimeViaNTP] Update finished (time of " + time(nullptr) + ")!");
#endif
hal->setUTCTime(time(nullptr));
}
}

if(this->m_enabledStationByAutoAP and (WiFi.status() == WL_CONNECTED or !this->m_enableClient)) {
Expand All @@ -54,8 +73,6 @@ void OswServiceTaskWiFi::loop(OswHal* hal) {
Serial.println(String(__FILE__) + ": [AutoAP] Inactive.");
#endif
}

//TODO autoreconnect
}

void OswServiceTaskWiFi::stop(OswHal* hal) {
Expand Down Expand Up @@ -101,6 +118,10 @@ IPAddress OswServiceTaskWiFi::getIP() {
return IPAddress();
}

void OswServiceTaskWiFi::queueTimeUpdateViaNTP() {
this->m_queuedNTPUpdate = true;
}

bool OswServiceTaskWiFi::isWiFiEnabled() {
return this->m_enableClient;
}
Expand All @@ -115,6 +136,8 @@ void OswServiceTaskWiFi::connectWiFi() {
this->updateWiFiConfig();
WiFi.begin(this->m_clientSSID.c_str(), this->m_clientPass.c_str());
this->m_autoAPTimeout = 0;
if(!this->m_queuedNTPUpdate)
this->m_queuedNTPUpdate = OswConfigAllKeys::wifiAlwaysNTPEnabled.get();
#ifdef DEBUG
Serial.println(String(__FILE__) + ": Connecting to SSID " + OswConfigAllKeys::wifiSsid.get() + "...");
#endif
Expand Down

0 comments on commit 73b1246

Please sign in to comment.