Factory Reset
Overview
The SMARTA L v2 device provides a factory reset mechanism to revert all configurable settings to their original default values. This is a crucial feature for troubleshooting, clearing user-specific configurations, or preparing the device for a new deployment.
A factory reset will typically erase all settings stored in Non-Volatile Storage (NVS), including:
- Wi-Fi credentials (SSID and password)
- LTE APN configuration
- MQTT broker details (if dynamically configured)
- Sensor calibration data (Min/Max levels, Raw Zero, etc.)
- Operational parameters (Pump max runtime, MQTT interval, etc.)
- BLE sensor MAC address (ADV_ID)
- Any other user-modified settings detailed in Settings.
Upon the next boot after a factory reset, the device will load the default values as defined in the firmware (e.g., in getSettingValues() in main.ino or hardcoded defaults in Preferences.get<Type>() calls).
Initiating a Factory Reset
A factory reset can be initiated through the following methods:
1. Button Interface
- Navigation: Enter the settings menu by a very long press of the physical button.
- Selection: Navigate through the menu options using short presses until "Reset" (or "FACTORY RESET") is displayed on the OLED screen.
- Confirmation: Perform a long press on the "Reset" option to select it.
- Process: The device will display "FACTORY RESET" on the OLED. The
settingsTask()inSMARTA_L_v2.inowill then callhandleFactoryReset(). - Feedback: The OLED will show "Performing factory reset...".
- Action: The
handleFactoryReset()function inmain.inois executed.- It sets a temporary flag:
preferences.begin("settings", false); preferences.putBool("factory_reset", true); preferences.end();. - It then calls
buzzAndRestart()which makes the buzzer sound and reboots the device.
- It sets a temporary flag:
- On Next Boot: The
initialize_device()function (or similar startup code) checks for thisfactory_resetflag. If true, it proceeds to clear the relevant preference namespaces:resetSettings()inmain.ino: Callspreferences.begin("settings", false); preferences.clear(); preferences.end();resetWiFiSettings()inmain.ino: Callspreferences.begin("wifi", false); preferences.clear(); preferences.end();- The
factory_resetflag itself is then cleared to prevent repeated resets.
2. MQTT Command
- A specific MQTT command can trigger a factory reset. While not explicitly detailed as a direct "factory reset" command in the provided MQTT message handling for
MQTT.ino, commands like"CE"(Clear EEPROM/Preferences) or a dedicated factory reset command could be implemented. - If such a command is received:
- The MQTT callback (
wifiCallbackormodemCallback) would parse the message. - The
handleMessage()function inMQTT.inowould identify the command. - It would then likely call
handleFactoryReset()or directly execute the preference clearing logic and reboot. - The
MQTT.inosnippet shows a"CE"command that callsresetEEPROM()(which appears to be an alias or related function for clearing preferences) and then reboots.
- The MQTT callback (
3. BLE Command
- A BLE characteristic could be exposed to trigger a factory reset. This is not explicitly detailed in the provided BLE characteristic list but is a common way to provide such functionality via a companion app.
- If implemented, writing a specific value to this characteristic would invoke
handleFactoryReset()or similar logic.
Process Details (handleFactoryReset())
Located in main.ino:
- OLED Indication:
printStatusToOLED("Performing factory reset...")displays the action on the screen. - Set Reset Flag:
cpp preferences.begin("settings", false); preferences.putBool("factory_reset", true); preferences.end();This flag signals the boot sequence to perform the actual clearing of settings. - Notify & Reboot:
buzzAndRestart()is called. This function typically includes:- Audible feedback using the buzzer.
- A short delay.
ESP.restart()to reboot the device.
Actions on Subsequent Boot
During the device initialization sequence after the reboot triggered by handleFactoryReset():
- The firmware checks the
factory_resetboolean flag in the "settings" preferences namespace. - If
true:resetSettings()is called: This function opens the "settings" namespace and callspreferences.clear()to erase all keys within it.resetWiFiSettings()is called: This function opens the "wifi" namespace and callspreferences.clear()to erase Wi-Fi credentials.- The
factory_resetflag itself is cleared from preferences to ensure the reset only occurs once.
- The device then proceeds with its normal boot sequence, loading default values for all settings as they are no longer present in NVS.
Code References
- Initiation via Button Menu:
settingsTask()inSMARTA_L_v2.ino(handles navigation to "Reset" and callshandleFactoryReset). - Core Reset Logic:
handleFactoryReset()inmain.ino(sets flag, reboots). - Clearing Preferences on Boot:
initialize_device()(or similar inmain.inoorSMARTA_L_v2.ino) should contain the logic to check thefactory_resetflag and callresetSettings()andresetWiFiSettings(). - Preference Clearing Functions:
resetSettings()andresetWiFiSettings()inmain.ino. - MQTT Command Handling (Example):
handleMessage()inMQTT.inofor commands like"CE".
By following these procedures, the SMARTA L v2 device can be reliably returned to its initial factory state.