Created setWifiCredentials() method

To be used with user-define Access Point, but CAN be used to programmatically hardcode SSID and PASSWORD (not a good idea to do this!)
This commit is contained in:
HomeSpan 2021-06-20 06:37:09 -05:00
parent e3d081bb35
commit fc01e37590
5 changed files with 44 additions and 16 deletions

View File

@ -38,14 +38,10 @@ void HAPClient::init(){
size_t len; // not used but required to read blobs from NVS
nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS
nvs_open("SRP",NVS_READWRITE,&srpNVS); // open SRP data namespace in NVS
nvs_open("HAP",NVS_READWRITE,&hapNVS); // open HAP data namespace in NVS
nvs_open("OTA",NVS_READWRITE,&otaNVS); // open OTA data namespace in NVS
if(!nvs_get_blob(wifiNVS,"WIFIDATA",NULL,&len)) // if found WiFi data in NVS
nvs_get_blob(wifiNVS,"WIFIDATA",&homeSpan.network.wifiData,&len); // retrieve data
if(!nvs_get_str(otaNVS,"OTADATA",NULL,&len)){ // if found OTA data in NVS
nvs_get_str(otaNVS,"OTADATA",homeSpan.otaPwd,&len); // retrieve data
} else {
@ -1650,7 +1646,6 @@ void Nonce::inc(){
TLV<kTLVType,10> HAPClient::tlv8;
nvs_handle HAPClient::hapNVS;
nvs_handle HAPClient::wifiNVS;
nvs_handle HAPClient::srpNVS;
nvs_handle HAPClient::otaNVS;
uint8_t HAPClient::httpBuf[MAX_HTTP+1];

View File

@ -79,7 +79,6 @@ struct HAPClient {
static TLV<kTLVType,10> tlv8; // TLV8 structure (HAP Section 14.1) with space for 10 TLV records of type kTLVType (HAP Table 5-6)
static nvs_handle hapNVS; // handle for non-volatile-storage of HAP data
static nvs_handle wifiNVS; // handle for non-volatile-storage of WiFi data
static nvs_handle srpNVS; // handle for non-volatile-storage of SRP data
static nvs_handle otaNVS; // handle for non-volatile-storage of OTA data
static uint8_t httpBuf[MAX_HTTP+1]; // buffer to store HTTP messages (+1 to leave room for storing an extra 'overflow' character)

View File

@ -66,7 +66,18 @@ void Span::begin(Category catID, const char *displayName, const char *hostNameBa
hapServer=new WiFiServer(tcpPortNum);
nvs_flash_init(); // initialize non-volatile-storage partition in flash
nvs_open("CHAR",NVS_READWRITE,&charNVS);
nvs_open("CHAR",NVS_READWRITE,&charNVS); // open Characteristic data namespace in NVS
nvs_open("WIFI",NVS_READWRITE,&wifiNVS); // open WIFI data namespace in NVS
size_t len;
if(strlen(network.wifiData.ssid)){ // if setWifiCredentials was already called
nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(wifiNVS); // commit to NVS
} else
if(!nvs_get_blob(wifiNVS,"WIFIDATA",NULL,&len)) // else if found WiFi data in NVS
nvs_get_blob(wifiNVS,"WIFIDATA",&homeSpan.network.wifiData,&len); // retrieve data
delay(2000);
@ -739,9 +750,16 @@ void Span::processSerialCommand(const char *c){
case 'W': {
if(strlen(network.wifiData.ssid)>0){
Serial.print("*** Stopping all current WiFi services...\n\n");
hapServer->end();
MDNS.end();
WiFi.disconnect();
}
network.serialConfigure();
nvs_set_blob(HAPClient::wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(HAPClient::wifiNVS); // commit to NVS
nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(wifiNVS); // commit to NVS
Serial.print("\n*** WiFi Credentials SAVED! Re-starting ***\n\n");
statusLED.off();
delay(1000);
@ -759,8 +777,8 @@ void Span::processSerialCommand(const char *c){
}
network.apConfigure();
nvs_set_blob(HAPClient::wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(HAPClient::wifiNVS); // commit to NVS
nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(wifiNVS); // commit to NVS
Serial.print("\n*** Credentials saved!\n\n");
if(strlen(network.setupCode)){
char s[10];
@ -780,8 +798,8 @@ void Span::processSerialCommand(const char *c){
case 'X': {
statusLED.off();
nvs_erase_all(HAPClient::wifiNVS);
nvs_commit(HAPClient::wifiNVS);
nvs_erase_all(wifiNVS);
nvs_commit(wifiNVS);
Serial.print("\n*** WiFi Credentials ERASED! Re-starting...\n\n");
delay(1000);
ESP.restart(); // re-start device
@ -821,8 +839,10 @@ void Span::processSerialCommand(const char *c){
statusLED.off();
nvs_erase_all(HAPClient::hapNVS);
nvs_commit(HAPClient::hapNVS);
nvs_erase_all(HAPClient::wifiNVS);
nvs_commit(HAPClient::wifiNVS);
nvs_erase_all(wifiNVS);
nvs_commit(wifiNVS);
nvs_erase_all(charNVS);
nvs_commit(charNVS);
Serial.print("\n*** FACTORY RESET! Restarting...\n\n");
delay(1000);
ESP.restart();
@ -948,6 +968,17 @@ void Span::processSerialCommand(const char *c){
///////////////////////////////
void Span::setWifiCredentials(char *ssid, char *pwd){
sprintf(network.wifiData.ssid,"%.*s",MAX_SSID,ssid);
sprintf(network.wifiData.pwd,"%.*s",MAX_PWD,pwd);
if(wifiNVS){ // is begin() already called and wifiNVS is open
nvs_set_blob(wifiNVS,"WIFIDATA",&network.wifiData,sizeof(network.wifiData)); // update data
nvs_commit(wifiNVS); // commit to NVS
}
}
///////////////////////////////
int Span::sprintfAttributes(char *cBuf){
int nBytes=0;

View File

@ -108,6 +108,7 @@ struct Span{
HapQR qrCode; // optional QR Code to use for pairing
const char *sketchVersion="n/a"; // version of the sketch
nvs_handle charNVS; // handle for non-volatile-storage of Characteristics data
nvs_handle wifiNVS=NULL; // handle for non-volatile-storage of WiFi data
boolean connected=false; // WiFi connection status
unsigned long waitTime=60000; // time to wait (in milliseconds) between WiFi connection attempts
@ -183,6 +184,7 @@ struct Span{
void setWifiCallback(void (*f)()){wifiCallback=f;} // sets an optional user-defined function to call once WiFi connectivity is established
void enableAutoStartAP(void (*f)()=NULL){autoStartAPEnabled=true;apFunction=f;} // enables auto start-up of Access Point when WiFi Credentials not found (will call optional f, if specified)
void setWifiCredentials(char *ssid, char *pwd); // sets WiFi Credentials
};
///////////////////////////////

View File

@ -21,7 +21,7 @@ void setup() {
new SpanUserCommand('d',"My Description",userCom1);
new SpanUserCommand('d',"My second Description",userCom2);
// homeSpan.enableAutoStartAP(myWiFiAP);
homeSpan.enableAutoStartAP(myWiFiAP);
homeSpan.begin(Category::Lighting,"HomeSpan Lamp Server","homespan");
@ -62,6 +62,7 @@ void loop(){
void myWiFiAP(){
Serial.print("Calling My WIFI AP\n\n");
homeSpan.setWifiCredentials("MY_NETWORK","MY_PASSWORD");
}
//////////////////////////////////////