Skip to content

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() in SMARTA_L_v2.ino will then call handleFactoryReset().
  • Feedback: The OLED will show "Performing factory reset...".
  • Action: The handleFactoryReset() function in main.ino is 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.
  • On Next Boot: The initialize_device() function (or similar startup code) checks for this factory_reset flag. If true, it proceeds to clear the relevant preference namespaces:
    • resetSettings() in main.ino: Calls preferences.begin("settings", false); preferences.clear(); preferences.end();
    • resetWiFiSettings() in main.ino: Calls preferences.begin("wifi", false); preferences.clear(); preferences.end();
    • The factory_reset flag 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 (wifiCallback or modemCallback) would parse the message.
    • The handleMessage() function in MQTT.ino would identify the command.
    • It would then likely call handleFactoryReset() or directly execute the preference clearing logic and reboot.
    • The MQTT.ino snippet shows a "CE" command that calls resetEEPROM() (which appears to be an alias or related function for clearing preferences) and then reboots.

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:

  1. OLED Indication: printStatusToOLED("Performing factory reset...") displays the action on the screen.
  2. 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.
  3. 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():

  1. The firmware checks the factory_reset boolean flag in the "settings" preferences namespace.
  2. If true:
    • resetSettings() is called: This function opens the "settings" namespace and calls preferences.clear() to erase all keys within it.
    • resetWiFiSettings() is called: This function opens the "wifi" namespace and calls preferences.clear() to erase Wi-Fi credentials.
    • The factory_reset flag itself is cleared from preferences to ensure the reset only occurs once.
  3. 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() in SMARTA_L_v2.ino (handles navigation to "Reset" and calls handleFactoryReset).
  • Core Reset Logic: handleFactoryReset() in main.ino (sets flag, reboots).
  • Clearing Preferences on Boot: initialize_device() (or similar in main.ino or SMARTA_L_v2.ino) should contain the logic to check the factory_reset flag and call resetSettings() and resetWiFiSettings().
  • Preference Clearing Functions: resetSettings() and resetWiFiSettings() in main.ino.
  • MQTT Command Handling (Example): handleMessage() in MQTT.ino for commands like "CE".

By following these procedures, the SMARTA L v2 device can be reliably returned to its initial factory state.