From 89287f43c9c5f4f1a67646633784f6018ceaaf5d Mon Sep 17 00:00:00 2001 From: elmo128 <60213508+elmo128@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:41:21 +0200 Subject: [PATCH] bugfix, optimization --- src/drivers/storage/SDCard.cpp | 17 ++++++++--------- src/drivers/storage/SDCard.h | 2 +- src/drivers/storage/nvMemory.cpp | 2 +- src/drivers/storage/nvMemory.h | 4 +--- src/drivers/storage/storage.h | 12 ++++++------ src/mining.cpp | 6 +++--- src/wManager.cpp | 13 ++++++++----- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/drivers/storage/SDCard.cpp b/src/drivers/storage/SDCard.cpp index fe06bde..d4bd273 100644 --- a/src/drivers/storage/SDCard.cpp +++ b/src/drivers/storage/SDCard.cpp @@ -26,13 +26,12 @@ SDCard::~SDCard() unmount(); } -void SDCard::SD2nvMemory(nvMemory* nvMem) +void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings) { - TSettings Settings; - if (loadConfigFile(&Settings)) + if (loadConfigFile(Settings)) { - nvMem->saveConfig(&Settings); - WiFi.begin(Settings.WifiSSID, Settings.WifiPW); + nvMem->saveConfig(Settings); + WiFi.begin(Settings->WifiSSID, Settings->WifiPW); Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); ESP.restart(); } @@ -60,9 +59,9 @@ bool SDCard::loadConfigFile(TSettings* Settings) unmount(); if (!error) { - strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID); - strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW); - strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress); + Settings->WifiSSID = json[JSON_KEY_SSID] | Settings->WifiSSID; + Settings->WifiPW = json[JSON_KEY_PASW] | Settings->WifiPW; + Settings->PoolAddress = json[JSON_KEY_POOLURL] | Settings->PoolAddress; strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet); if (json.containsKey(JSON_KEY_POOLPORT)) Settings->PoolPort = json[JSON_KEY_POOLPORT].as(); @@ -149,7 +148,7 @@ bool SDCard::initSDcard() SDCard::SDCard() {} SDCard::~SDCard() {} -void SDCard::SD2nvMemory(nvMemory* nvMem) {}; +void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings) {}; bool SDCard::loadConfigFile(TSettings* Settings) { return false; } bool SDCard::initSDcard() { return false; } void unmount() {} diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index b2e91f5..d1da16f 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -21,7 +21,7 @@ class SDCard public: SDCard(); ~SDCard(); - void SD2nvMemory(nvMemory* nvMem); + void SD2nvMemory(nvMemory* nvMem, TSettings* Settings); bool loadConfigFile(TSettings* Settings); private: bool initSDcard(); diff --git a/src/drivers/storage/nvMemory.cpp b/src/drivers/storage/nvMemory.cpp index a686589..0f737aa 100644 --- a/src/drivers/storage/nvMemory.cpp +++ b/src/drivers/storage/nvMemory.cpp @@ -84,7 +84,7 @@ bool nvMemory::loadConfig(TSettings* Settings) Serial.print('\n'); if (!error) { - strcpy(Settings->PoolAddress, json[JSON_KEY_POOLURL] | Settings->PoolAddress); + Settings->PoolAddress = json[JSON_KEY_POOLURL] | Settings->PoolAddress; strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet); if (json.containsKey(JSON_KEY_POOLPORT)) Settings->PoolPort = json[JSON_KEY_POOLPORT].as(); diff --git a/src/drivers/storage/nvMemory.h b/src/drivers/storage/nvMemory.h index 163431c..3b22c7e 100644 --- a/src/drivers/storage/nvMemory.h +++ b/src/drivers/storage/nvMemory.h @@ -21,9 +21,7 @@ private: bool Initialized_; }; -#ifdef NVMEM_SPIFFS -#define ESP_DRD_USE_SPIFFS true -#else +#ifndef NVMEM_SPIFFS #error We need some kind of permanent storage implementation! #endif //NVMEM_TYPE diff --git a/src/drivers/storage/storage.h b/src/drivers/storage/storage.h index 8aa0668..5da92e3 100644 --- a/src/drivers/storage/storage.h +++ b/src/drivers/storage/storage.h @@ -1,7 +1,7 @@ #ifndef _STORAGE_H_ #define _STORAGE_H_ -#include +#include #define DEFAULT_SSID "NerdMinerAP" #define DEFAULT_WIFIPW "MineYourCoins" @@ -23,12 +23,12 @@ struct TSettings { - char WifiSSID[80]{ DEFAULT_SSID }; - char WifiPW[80]{ DEFAULT_WIFIPW }; - char PoolAddress[80]{ DEFAULT_POOLURL }; + String WifiSSID{ DEFAULT_SSID }; + String WifiPW{ DEFAULT_WIFIPW }; + String PoolAddress{ DEFAULT_POOLURL }; char BtcWallet[80]{ DEFAULT_WALLETID }; - uint32_t PoolPort{ DEFAULT_POOLPORT }; - uint32_t Timezone{ DEFAULT_TIMEZONE }; + int PoolPort{ DEFAULT_POOLPORT }; + int Timezone{ DEFAULT_TIMEZONE }; bool saveStats{ DEFAULT_SAVESTATS }; }; diff --git a/src/mining.cpp b/src/mining.cpp index d6f5694..0b7dccb 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -59,14 +59,14 @@ bool checkPoolConnection(void) { //Resolve first time pool DNS and save IP if(serverIP == IPAddress(1,1,1,1)) { - WiFi.hostByName(Settings.PoolAddress, serverIP); + WiFi.hostByName(Settings.PoolAddress.c_str(), serverIP); Serial.printf("Resolved DNS and save ip (first time) got: %s\n", serverIP.toString()); } //Try connecting pool IP if (!client.connect(serverIP, Settings.PoolPort)) { - Serial.println("Imposible to connect to : " + String(Settings.PoolAddress)); - WiFi.hostByName(Settings.PoolAddress, serverIP); + Serial.println("Imposible to connect to : " + Settings.PoolAddress); + WiFi.hostByName(Settings.PoolAddress.c_str(), serverIP); Serial.printf("Resolved DNS got: %s\n", serverIP.toString()); vTaskDelay(1000 / portTICK_PERIOD_MS); return false; diff --git a/src/wManager.cpp b/src/wManager.cpp index 93afff4..bd89146 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -1,6 +1,8 @@ +#define ESP_DRD_USE_SPIFFS true // Include Libraries //#include ".h" + #include #include @@ -89,7 +91,7 @@ void init_WifiManager() if (SDCrd.loadConfigFile(&Settings)) { //Config file on SD card. - SDCrd.SD2nvMemory(&nvMem); // reboot on success. + SDCrd.SD2nvMemory(&nvMem, &Settings); // reboot on success. } else { @@ -122,7 +124,7 @@ void init_WifiManager() // Custom elements // Text box (String) - 80 characters maximum - WiFiManagerParameter pool_text_box("Poolurl", "Pool url", Settings.PoolAddress, 80); + WiFiManagerParameter pool_text_box("Poolurl", "Pool url", Settings.PoolAddress.c_str(), 80); // Need to convert numerical input to string to display the default value. char convertedValue[6]; @@ -168,7 +170,7 @@ void init_WifiManager() { //Could be break forced after edditing, so save new config Serial.println("failed to connect and hit timeout"); - strncpy(Settings.PoolAddress, pool_text_box.getValue(), sizeof(Settings.PoolAddress)); + Settings.PoolAddress = pool_text_box.getValue(); Settings.PoolPort = atoi(port_text_box_num.getValue()); strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet)); Settings.Timezone = atoi(time_text_box_num.getValue()); @@ -187,7 +189,7 @@ void init_WifiManager() //Tratamos de conectar con la configuraciĆ³n inicial ya almacenada mMonitor.NerdStatus = NM_Connecting; wm.setCaptivePortalEnable(false); // disable captive portal redirection - if (!wm.autoConnect(Settings.WifiSSID, Settings.WifiPW)) + if (!wm.autoConnect(Settings.WifiSSID.c_str(), Settings.WifiPW.c_str())) { Serial.println("Failed to connect and hit timeout"); //delay(3000); @@ -210,7 +212,8 @@ void init_WifiManager() // Lets deal with the user config values // Copy the string value - strncpy(Settings.PoolAddress, pool_text_box.getValue(), sizeof(Settings.PoolAddress)); + Settings.PoolAddress = pool_text_box.getValue(); + //strncpy(Settings.PoolAddress, pool_text_box.getValue(), sizeof(Settings.PoolAddress)); Serial.print("PoolString: "); Serial.println(Settings.PoolAddress);