From 03e43e0bbb379644a5986f98d6e2a7689a550204 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 31 Jan 2021 18:58:45 -0600 Subject: [PATCH] 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. --- src/HAP.cpp | 8 ++++++++ src/HomeSpan.cpp | 25 ++++++++++++++++++++++++- src/HomeSpan.h | 2 +- src/src.ino | 2 +- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 09c74b8..2fd8b05 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -67,6 +67,14 @@ void HAPClient::init(){ Serial.print(homeSpan.qrCode.get(atoi(homeSpan.defaultSetupCode),homeSpan.qrID,atoi(homeSpan.category))); 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 nvs_get_blob(hapNVS,"ACCESSORY",&accessory,&len); // retrieve data diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 7f106d2..1267e84 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -385,6 +385,8 @@ void Span::checkConnect(){ Serial.print(displayName); Serial.print("\nModel Name: "); Serial.print(modelName); + Serial.print("\nSetup ID: "); + Serial.print(qrID); Serial.print("\n"); 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); if(strlen(id)==4 && strlen(tBuf)==4){ - qrID=id; + sprintf(qrID,"%s",id); } } // setQRID @@ -513,6 +515,26 @@ void Span::processSerialCommand(const char *c){ } 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': { char buf[128]; @@ -725,6 +747,7 @@ void Span::processSerialCommand(const char *c){ Serial.print(" W - configure WiFi Credentials and restart\n"); Serial.print(" X - delete WiFi Credentials and restart\n"); Serial.print(" S - change the HomeKit Pairing Setup Code to \n"); + Serial.print(" Q - change the HomeKit Setup ID for QR Codes to \n"); Serial.print(" A - start the HomeSpan Setup Access Point\n"); Serial.print("\n"); Serial.print(" U - unpair device by deleting all Controller data\n"); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 281ee36..5960bb0 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -99,7 +99,7 @@ struct Span{ 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 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 Blinker statusLED; // indicates HomeSpan status diff --git a/src/src.ino b/src/src.ino index b60eb48..635b658 100644 --- a/src/src.ino +++ b/src/src.ino @@ -12,7 +12,7 @@ void setup() { homeSpan.setHostNameSuffix(""); homeSpan.setPortNum(1200); - homeSpan.setQRID("One1"); +// homeSpan.setQRID("One1"); homeSpan.begin(Category::Lighting,"HomeSpanTest");