#include "Network.h" #include #include void Network::begin() { byte TryCounter = 0; // Initiating wifi, like in BasicHttpClient example WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while ((WiFi.status() != WL_CONNECTED) && (TryCounter < 10)) { delay(1000); TryCounter++; } // Find internet time setTime(); } // Gets time from ntp server void Network::getTime(int *timeDigits) { // Get seconds since 1.1.1970. time_t nowSecs = time(nullptr); // Used to store time struct tm timeinfo; gmtime_r(&nowSecs, &timeinfo); // Setting time string timezone (Better defined modulo, in case timezone makes hours to go below 0) timeinfo.tm_hour = ((timeinfo.tm_hour + timeZone) % 24 + 24) %24; // fill in the digits timeDigits[0] = timeinfo.tm_min % 10; //minutes (units) timeDigits[1] = timeinfo.tm_min / 10; //minutes (tens) timeDigits[2] = timeinfo.tm_hour % 10; //hours (units) timeDigits[3] = timeinfo.tm_hour / 10; //hours (tens) } void Network::off() { WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } void Network::setTime() { byte TryCounter = 0; // Used for setting correct time configTime(0, 0, "de.pool.ntp.org", "time.nist.gov"); delay(2000); // wait 2s to (re)sync time // if time is already set in RTC, // the loop below doesn't wait for sync // because the time value is valid time_t nowSecs = time(nullptr); while ((nowSecs < 8 * 3600 * 2)&& (TryCounter < 120)) { delay(500); // yield(); nowSecs = time(nullptr); TryCounter++; } }