From 8004da624d9f606e3372a65032c55fab771441af Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 7 Nov 2023 19:16:30 +0700 Subject: [PATCH] factory reset function --- src/espmega_iot_core.cpp | 33 +++++++++++++++++++++++++++++++++ src/espmega_iot_core.hpp | 5 ++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index ccd75d2..47e09f7 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -123,6 +123,7 @@ void setup() lcd_send_stop_bit(); lcd_init(); lcd_begin(); + check_boot_reset(); #ifdef ENABLE_EXTERNAL_LCD Serial2.print("rest"); Serial2.write(0xFF); @@ -1191,4 +1192,36 @@ void set_mqtt_useauth(bool use_auth) { MQTT_USE_AUTH = use_auth; ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH); +} + +void factory_reset() { + for(int i=5;i>0;i--) { + if(digitalRead(2)==HIGH) { + lcd_send_command("boot_state.txt=\"Factory Reset Canceled, Restarting\""); + delay(5000); + ESP.restart(); + } + Serial.printf("boot_state.txt=\"Factory Reset in %d\"",i); + lcd_send_stop_bit(); + delay(1000); + } + lcd_send_command("boot_state.txt=\"Factory Reseting . . .\""); + // Format FRAM + for(int i=0;i<32768;i++) { + ESPMega_FRAM.write8(i,0); + } + // Load Default Values + set_ip("192.168.0.10"); + set_gw("192.168.0.1"); + // Reboot + lcd_send_stop_bit(); + lcd_send_command("boot_state.txt=\"Factory Reset OK. Release Button.\""); + delay(3000); + ESP.restart(); +} +void check_boot_reset() { + pinMode(2, INPUT_PULLUP); + if(digitalRead(2)==LOW) { + factory_reset(); + } } \ No newline at end of file diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index b19ace1..23c32eb 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -104,4 +104,7 @@ void eeprom_mqtt_password_retrieve(); void set_mqtt_username(String username); void set_mqtt_password(String password); void eeprom_mqtt_useauth_retrieve(); -void set_mqtt_useauth(bool use_auth); \ No newline at end of file +void set_mqtt_useauth(bool use_auth); + +void factory_reset(); +void check_boot_reset(); \ No newline at end of file