diff --git a/include/bmHTTP.cpp b/include/bmHTTP.cpp index 85f00ff..ed47f24 100644 --- a/include/bmHTTP.cpp +++ b/include/bmHTTP.cpp @@ -1,5 +1,7 @@ #include "bmHTTP.hpp" #include "esp_http_client.h" +#include "nvs_flash.h" +#include "defines.h" #define httpSrv "http://192.168.1.190:3000/" std::string webToken; @@ -62,4 +64,28 @@ bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse) { esp_http_client_cleanup(client); return success; +} + +void deleteWiFiAndTokenDetails() { + nvs_handle_t wifiHandle; + if (nvs_open(nvsWiFi, NVS_READWRITE, &wifiHandle) == ESP_OK) { + if (nvs_erase_all(wifiHandle) == ESP_OK) { + printf("Successfully erased WiFi details\n"); + nvs_commit(wifiHandle); + } + else printf("ERROR: Erase wifi failed\n"); + nvs_close(wifiHandle); + } + else printf("ERROR: Failed to open WiFi section for deletion\n"); + + nvs_handle_t authHandle; + if (nvs_open(nvsAuth, NVS_READWRITE, &authHandle) == ESP_OK) { + if (nvs_erase_all(authHandle) == ESP_OK) { + printf("Successfully erased Auth details\n"); + nvs_commit(authHandle); + } + else printf("ERROR: Erase auth failed\n"); + nvs_close(authHandle); + } + else printf("ERROR: Failed to open Auth section for deletion\n"); } \ No newline at end of file diff --git a/include/bmHTTP.hpp b/include/bmHTTP.hpp index db226bb..332344c 100644 --- a/include/bmHTTP.hpp +++ b/include/bmHTTP.hpp @@ -7,4 +7,6 @@ extern std::string webToken; bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse); +void deleteWiFiAndTokenDetails(); + #endif \ No newline at end of file diff --git a/include/calibration.cpp b/include/calibration.cpp index cc0703e..dd77548 100644 --- a/include/calibration.cpp +++ b/include/calibration.cpp @@ -40,6 +40,7 @@ bool Calibration::clearCalibrated() { printf("Error saving calibration status as false.\n"); return false; } + nvs_commit(calibHandle); nvs_close(calibHandle); } else { @@ -56,6 +57,7 @@ bool Calibration::beginDownwardCalib(Encoder& topEnc) { if (nvs_set_i32(calibHandle, UpTicksTag, tempUpTicks) == ESP_OK) { printf("Saved UpTicks to NVS\n"); UpTicks = tempUpTicks; + nvs_commit(calibHandle); } else { printf("Error saving UpTicks.\n"); @@ -88,6 +90,7 @@ bool Calibration::completeCalib(Encoder& topEnc) { DownTicks = tempDownTicks; calibrated = true; printf("Range: %d - %d\n", UpTicks.load(), tempDownTicks); + nvs_commit(calibHandle); nvs_close(calibHandle); } else { diff --git a/include/encoder.cpp b/include/encoder.cpp index c4fec06..1687c4c 100644 --- a/include/encoder.cpp +++ b/include/encoder.cpp @@ -109,7 +109,7 @@ void Encoder::setupWatchdog() { void Encoder::pauseWatchdog() { feedWDog = false; - esp_timer_stop(watchdog_handle); + if (watchdog_handle != NULL) esp_timer_stop(watchdog_handle); } Encoder::~Encoder() { diff --git a/include/servo.cpp b/include/servo.cpp index 9daa049..edbd87b 100644 --- a/include/servo.cpp +++ b/include/servo.cpp @@ -66,6 +66,12 @@ void servoMainSwitch(uint8_t onOff) { } bool servoInitCalib() { + topEnc->pauseWatchdog(); + + // get ready for calibration by clearing all these listeners + bottomEnc->wandListen = false; + topEnc->wandListen = false; + topEnc->serverListen = false; if (!calib.clearCalibrated()) return false; if (topEnc == nullptr || bottomEnc == nullptr) { printf("ERROR: CALIBRATION STARTED BEFORE SERVO INITIALIZATION\n"); @@ -138,6 +144,7 @@ void servoSavePos() { if (nvs_set_i32(servoHandle, posTag, topCount) != ESP_OK) printf("Error saving current position\n"); else printf("Success - Current position saved as: %d\n", topCount); + nvs_commit(servoHandle); nvs_close(servoHandle); } else { diff --git a/include/socketIO.cpp b/include/socketIO.cpp index 569bdf4..496a234 100644 --- a/include/socketIO.cpp +++ b/include/socketIO.cpp @@ -104,6 +104,8 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base, statusResolved = true; } else { printf("Device authentication failed\n"); + calib.clearCalibrated(); + deleteWiFiAndTokenDetails(); connected = false; statusResolved = true; } @@ -119,6 +121,8 @@ static void socketio_event_handler(void *handler_args, esp_event_base_t base, printf("Server message: %s\n", message->valuestring); } } + calib.clearCalibrated(); + deleteWiFiAndTokenDetails(); connected = false; statusResolved = true; }