Added 'Q' command to change default Setup ID and store in NVS

Setup ID can now be stored in NVS and set dynamically with the 'Q' command.  However, homeSpan.setQRID(char *id) will override if present, but will not change the default stored in the NVS.  This default wil be used once again if homeSpan.setQRID() is removed from the sektch.

If the NVS is empty, the default is set to "HSPN" as in version 1.1.4.
This commit is contained in:
Gregg 2021-01-31 18:58:45 -06:00
parent 02cfe7bbcd
commit 03e43e0bbb
4 changed files with 34 additions and 3 deletions

View File

@ -67,6 +67,14 @@ void HAPClient::init(){
Serial.print(homeSpan.qrCode.get(atoi(homeSpan.defaultSetupCode),homeSpan.qrID,atoi(homeSpan.category))); Serial.print(homeSpan.qrCode.get(atoi(homeSpan.defaultSetupCode),homeSpan.qrID,atoi(homeSpan.category)));
Serial.print("\n\n"); Serial.print("\n\n");
} }
if(!strlen(homeSpan.qrID)){ // Setup ID has not been specified in sketch
if(!nvs_get_str(hapNVS,"SETUPID",NULL,&len)){ // check for saved value
nvs_get_str(hapNVS,"SETUPID",homeSpan.qrID,&len); // retrieve data
} else {
sprintf(homeSpan.qrID,"%s",DEFAULT_QR_ID); // use default
}
}
if(!nvs_get_blob(hapNVS,"ACCESSORY",NULL,&len)){ // if found long-term Accessory data in NVS if(!nvs_get_blob(hapNVS,"ACCESSORY",NULL,&len)){ // if found long-term Accessory data in NVS
nvs_get_blob(hapNVS,"ACCESSORY",&accessory,&len); // retrieve data nvs_get_blob(hapNVS,"ACCESSORY",&accessory,&len); // retrieve data

View File

@ -385,6 +385,8 @@ void Span::checkConnect(){
Serial.print(displayName); Serial.print(displayName);
Serial.print("\nModel Name: "); Serial.print("\nModel Name: ");
Serial.print(modelName); Serial.print(modelName);
Serial.print("\nSetup ID: ");
Serial.print(qrID);
Serial.print("\n"); Serial.print("\n");
MDNS.begin(hostName); // set server host name (.local implied) MDNS.begin(hostName); // set server host name (.local implied)
@ -443,7 +445,7 @@ void Span::setQRID(const char *id){
sscanf(id,"%4[0-9A-Za-z]",tBuf); sscanf(id,"%4[0-9A-Za-z]",tBuf);
if(strlen(id)==4 && strlen(tBuf)==4){ if(strlen(id)==4 && strlen(tBuf)==4){
qrID=id; sprintf(qrID,"%s",id);
} }
} // setQRID } // setQRID
@ -513,6 +515,26 @@ void Span::processSerialCommand(const char *c){
} }
break; break;
case 'Q': {
char tBuf[5];
const char *s=c+1+strspn(c+1," ");
sscanf(s," %4[0-9A-Za-z]",tBuf);
if(strlen(s)==4 && strlen(tBuf)==4){
sprintf(qrID,"%s",tBuf);
Serial.print("\n\nChanging default Setup ID for QR Code to : '");
Serial.print(qrID);
Serial.print("'. Will take effect after next restart.\n\n");
nvs_set_str(HAPClient::hapNVS,"SETUPID",qrID); // update data
nvs_commit(HAPClient::hapNVS);
} else {
Serial.print("\n*** Invalid request to change Setup ID for QR Code to: '");
Serial.print(s);
Serial.print("'. Setup ID must be exactly 4 alphanumeric characters (0-9, A-Z, and a-z).\n\n");
}
}
break;
case 'S': { case 'S': {
char buf[128]; char buf[128];
@ -725,6 +747,7 @@ void Span::processSerialCommand(const char *c){
Serial.print(" W - configure WiFi Credentials and restart\n"); Serial.print(" W - configure WiFi Credentials and restart\n");
Serial.print(" X - delete WiFi Credentials and restart\n"); Serial.print(" X - delete WiFi Credentials and restart\n");
Serial.print(" S <code> - change the HomeKit Pairing Setup Code to <code>\n"); Serial.print(" S <code> - change the HomeKit Pairing Setup Code to <code>\n");
Serial.print(" Q <id> - change the HomeKit Setup ID for QR Codes to <id>\n");
Serial.print(" A - start the HomeSpan Setup Access Point\n"); Serial.print(" A - start the HomeSpan Setup Access Point\n");
Serial.print("\n"); Serial.print("\n");
Serial.print(" U - unpair device by deleting all Controller data\n"); Serial.print(" U - unpair device by deleting all Controller data\n");

View File

@ -99,7 +99,7 @@ struct Span{
uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections uint8_t maxConnections=DEFAULT_MAX_CONNECTIONS; // number of simultaneous HAP connections
unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations
uint16_t tcpPortNum=DEFAULT_TCP_PORT; // port for TCP communications between HomeKit and HomeSpan uint16_t tcpPortNum=DEFAULT_TCP_PORT; // port for TCP communications between HomeKit and HomeSpan
const char *qrID=DEFAULT_QR_ID; // optional Setup ID used to pair with QR Code char qrID[5]=""; // Setup ID used for pairing with QR Code
WiFiServer *hapServer; // pointer to the HAP Server connection WiFiServer *hapServer; // pointer to the HAP Server connection
Blinker statusLED; // indicates HomeSpan status Blinker statusLED; // indicates HomeSpan status

View File

@ -12,7 +12,7 @@ void setup() {
homeSpan.setHostNameSuffix(""); homeSpan.setHostNameSuffix("");
homeSpan.setPortNum(1200); homeSpan.setPortNum(1200);
homeSpan.setQRID("One1"); // homeSpan.setQRID("One1");
homeSpan.begin(Category::Lighting,"HomeSpanTest"); homeSpan.begin(Category::Lighting,"HomeSpanTest");