2323#define NC_UPDATE_NETWORK_OPTIONS_TIMER_ms 120000
2424
2525constexpr char *STORAGE_KEY{ " NETWORK_CONFIGS" };
26+ constexpr char *START_BLE_AT_STARTUP_KEY{ " START_BLE" };
2627
2728NetworkConfiguratorClass::NetworkConfiguratorClass (ConnectionHandler &connectionHandler)
2829 :
@@ -32,6 +33,7 @@ NetworkConfiguratorClass::NetworkConfiguratorClass(ConnectionHandler &connection
3233 _optionUpdateTimer{ NC_UPDATE_NETWORK_OPTIONS_TIMER_ms, NC_UPDATE_NETWORK_OPTIONS_TIMER_ms } {
3334 _optionUpdateTimer.begin (NC_UPDATE_NETWORK_OPTIONS_TIMER_ms); // initialize the timer before calling begin
3435 _agentsManager = &AgentsManagerClass::getInstance ();
36+ _resetInput = &ResetInput::getInstance ();
3537}
3638
3739bool NetworkConfiguratorClass::begin () {
@@ -71,6 +73,7 @@ bool NetworkConfiguratorClass::begin() {
7173
7274 _connectionTimeout.begin (NC_CONNECTION_TIMEOUT_ms);
7375 _connectionRetryTimer.begin (NC_CONNECTION_RETRY_TIMER_ms);
76+ _resetInput->begin ();
7477
7578#ifdef BOARD_HAS_ETHERNET
7679 _networkSetting.type = NetworkAdapter::ETHERNET;
@@ -110,6 +113,18 @@ NetworkConfiguratorStates NetworkConfiguratorClass::poll() {
110113 _state = nextState;
111114 }
112115
116+ /* Reconfiguration procedure:
117+ * - Arduino Opta: press and hold the user button (BTN_USER) until the led (LED_USER) turns off
118+ * - Arduino Nano 33 IOT: short the pin 2 to GND until the led turns off
119+ * - Arduino Uno R4 WiFi: short the pin 2 to GND until the led turns off
120+ * - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
121+ * - Other boards: short the pin 7 to GND until the led turns off
122+ */
123+
124+ if (_resetInput->isEventFired ()) {
125+ startReconfigureProcedure ();
126+ }
127+
113128 return _state;
114129}
115130
@@ -153,15 +168,24 @@ bool NetworkConfiguratorClass::end() {
153168 return _agentsManager->end (SERVICE_ID_FOR_AGENTMANAGER);
154169}
155170
171+ void NetworkConfiguratorClass::setReconfigurePin (uint32_t pin) {
172+ _resetInput->setPin (pin);
173+ }
174+
175+ void NetworkConfiguratorClass::addReconfigurePinCallback (void (*callback)()) {
176+ _resetInput->setPinChangedCallback (callback);
177+ }
178+
156179bool NetworkConfiguratorClass::isBLEenabled () {
157180 return _agentsManager->isBLEAgentEnabled ();
158181}
159182
160183void NetworkConfiguratorClass::enableBLE (bool enable) {
184+ _bleEnabled = enable;
161185 _agentsManager->enableBLEAgent (enable);
162186}
163187
164- void NetworkConfiguratorClass::configurationCompleted () {
188+ void NetworkConfiguratorClass::disconnectAgent () {
165189 _agentsManager->disconnect ();
166190}
167191
@@ -404,6 +428,19 @@ void NetworkConfiguratorClass::handleGetWiFiFWVersion() {
404428 _agentsManager->sendMsg (fwVersionMsg);
405429}
406430
431+ void NetworkConfiguratorClass::startReconfigureProcedure () {
432+ resetStoredConfiguration ();
433+ if (_kvstore != nullptr ){
434+ if (_kvstore->begin ()) {
435+ if (!_kvstore->putBool (START_BLE_AT_STARTUP_KEY, true )){
436+ DEBUG_ERROR (" NetworkConfiguratorClass::%s Error saving BLE enabled at startup" , __FUNCTION__);
437+ }
438+ _kvstore->end ();
439+ }
440+ }
441+ NVIC_SystemReset ();
442+ }
443+
407444#ifdef BOARD_HAS_ETHERNET
408445NetworkConfiguratorStates NetworkConfiguratorClass::handleCheckEth () {
409446 NetworkConfiguratorStates nextState = _state;
@@ -445,6 +482,12 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() {
445482 }
446483
447484 } else {
485+ if (_kvstore->exists (START_BLE_AT_STARTUP_KEY)) {
486+ if (_kvstore->getBool (START_BLE_AT_STARTUP_KEY)) {
487+ _agentsManager->enableBLEAgent (true );
488+ }
489+ _kvstore->remove (START_BLE_AT_STARTUP_KEY);
490+ }
448491 nextState = NetworkConfiguratorStates::WAITING_FOR_CONFIG;
449492 }
450493 _kvstore->end ();
@@ -492,6 +535,9 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnecting() {
492535 ConnectionResult res = connectToNetwork (&err);
493536
494537 if (res == ConnectionResult::SUCCESS) {
538+ if (_agentsManager->isBLEAgentEnabled () && !_bleEnabled) {
539+ _agentsManager->enableBLEAgent (false );
540+ }
495541 nextState = NetworkConfiguratorStates::CONFIGURED;
496542 } else if (res == ConnectionResult::FAILED) {
497543 sendStatus (err);
0 commit comments