Added optional homeSpan.setTimeServer() method

If specified, HomeSpan will try to get internet time after establishing WiFi connection.  Consumes one socket connection.
This commit is contained in:
Gregg 2022-02-27 18:29:58 -06:00
parent ae4b6e8df1
commit 571bc55852
2 changed files with 18 additions and 6 deletions

View File

@ -559,6 +559,15 @@ void Span::checkConnect(){
}
}
if(timeServer){
Serial.printf("Acquiring Time from %s... ",timeServer,timeZone);
configTzTime(timeZone,timeServer);
getLocalTime(&timeinfo);
char c[65];
strftime(c,64,"%a %b %e %Y %I:%M:%S %p",&timeinfo);
Serial.printf("%s (%s)\n\n",c,timeZone);
}
Serial.printf("Starting HAP Server on port %d supporting %d simultaneous HomeKit Controller Connections...\n",tcpPortNum,maxConnections);
hapServer->begin();

View File

@ -113,6 +113,9 @@ struct Span{
nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data
nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi data
char pairingCodeCommand[12]=""; // user-specified Pairing Code - only needed if Pairing Setup Code is specified in sketch using setPairingCode()
const char *timeZone=NULL; // optional time-zone specification
const char *timeServer=NULL; // optional time server to use for acquiring clock time
struct tm timeinfo; // optional time info structure
boolean connected=false; // WiFi connection status
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
@ -192,7 +195,6 @@ struct Span{
void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established
void setPairCallback(void (*f)(boolean isPaired)){pairCallback=f;} // sets an optional user-defined function to call when Pairing is established (true) or lost (false)
void setApFunction(void (*f)()){apFunction=f;} // sets an optional user-defined function to call when activating the WiFi Access Point
void enableAutoStartAP(){autoStartAPEnabled=true;} // enables auto start-up of Access Point when WiFi Credentials not found
void setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials
@ -200,6 +202,7 @@ struct Span{
void deleteStoredValues(){processSerialCommand("V");} // deletes stored Characteristic values from NVS
void enableOTA(boolean auth=true){otaEnabled=true;otaAuth=auth;reserveSocketConnections(1);} // enables Over-the-Air updates, with (auth=true) or without (auth=false) authorization password
void setTimeServer(const char *serv, const char *tz){timeServer=serv;timeZone=tz;reserveSocketConnections(1);} // sets optional time server and time zone
[[deprecated("Please use reserveSocketConnections(n) method instead.")]]
void setMaxConnections(uint8_t n){requestedMaxCon=n;} // sets maximum number of simultaneous HAP connections