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); Serial.printf("Starting HAP Server on port %d supporting %d simultaneous HomeKit Controller Connections...\n",tcpPortNum,maxConnections);
hapServer->begin(); hapServer->begin();
@ -571,7 +580,7 @@ void Span::checkConnect(){
} else { } else {
statusLED.on(); statusLED.on();
} }
if(wifiCallback) if(wifiCallback)
wifiCallback(); wifiCallback();

View File

@ -113,7 +113,10 @@ struct Span{
nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data
nvs_handle wifiNVS=0; // handle for non-volatile-storage of WiFi 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() 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 boolean connected=false; // WiFi connection status
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
unsigned long alarmConnect=0; // time after which WiFi connection attempt should be tried again unsigned long alarmConnect=0; // time after which WiFi connection attempt should be tried again
@ -191,16 +194,16 @@ struct Span{
const char *getSketchVersion(){return sketchVersion;} // get sketch version number const char *getSketchVersion(){return sketchVersion;} // get sketch version number
void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established 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 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 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 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 void setWifiCredentials(const char *ssid, const char *pwd); // sets WiFi Credentials
void setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead void setPairingCode(const char *s){sprintf(pairingCodeCommand,"S %9s",s);} // sets the Pairing Code - use is NOT recommended. Use 'S' from CLI instead
void deleteStoredValues(){processSerialCommand("V");} // deletes stored Characteristic values from NVS 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 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.")]] [[deprecated("Please use reserveSocketConnections(n) method instead.")]]
void setMaxConnections(uint8_t n){requestedMaxCon=n;} // sets maximum number of simultaneous HAP connections void setMaxConnections(uint8_t n){requestedMaxCon=n;} // sets maximum number of simultaneous HAP connections