From ab48dbf5e309df2fa011d8a255b90e90fc90b39b Mon Sep 17 00:00:00 2001 From: elmo128 <60213508+elmo128@users.noreply.github.com> Date: Thu, 14 Sep 2023 22:32:25 +0200 Subject: [PATCH] cleanup, fix --- src/drivers/storage/SDCard.h | 114 +++++++++++++++++------------- src/drivers/storage/SPIStorage.h | 117 ++++++++++++++++--------------- src/drivers/storage/UARTCfgUI.h | 100 -------------------------- src/drivers/storage/storage.h | 51 ++++++++++++-- src/mining.cpp | 4 +- src/wManager.cpp | 35 +++++---- 6 files changed, 192 insertions(+), 229 deletions(-) delete mode 100644 src/drivers/storage/UARTCfgUI.h diff --git a/src/drivers/storage/SDCard.h b/src/drivers/storage/SDCard.h index dff399a..75c812a 100644 --- a/src/drivers/storage/SDCard.h +++ b/src/drivers/storage/SDCard.h @@ -11,8 +11,6 @@ #include "storage.h" #include "SPIStorage.h" -#define JSON_CONFIG_FILE "/config.json" - class SDCard { private: @@ -20,7 +18,7 @@ private: public: SDCard() { - cardInitialized_ = initSDcard(); + cardInitialized_ = false; } ~SDCard() @@ -29,42 +27,26 @@ public: SD_MMC.end(); } - bool initSDcard() + void SD2SPIStorage(SPIStorage* spifs) { - if (cardInitialized_) - return cardInitialized_; - - bool oneBitMode = true; -#if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3) - if (SD_MMC.cardType() == CARD_NONE) - SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); - oneBitMode = false; -#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)) - if (SD_MMC.cardType() == CARD_NONE) - SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); -#else - Serial.println("SDCard: interface not available."); - return false; -#endif // dataPinsDefined - - if ((!SD_MMC.begin("/sdcard", oneBitMode)) || (SD_MMC.cardType() == CARD_NONE)) + TSettings Settings; + if (loadConfigFile(&Settings)) { - Serial.println("SDCard: No card found."); - return false; + spifs->saveConfigFile(&Settings); + WiFi.begin(Settings.WifiSSID, Settings.WifiPW); + Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); + ESP.restart(); } - return true; } - TSettings loadConfigFile() + bool loadConfigFile(TSettings* Settings) { // Load existing configuration file // Read configuration from FS json Serial.println("SDCard: Mounting File System..."); - TSettings Settings; if (initSDcard()) { - Serial.println("SDCard: Mounted"); if (SD_MMC.exists(JSON_CONFIG_FILE)) { // The file exists, reading and loading @@ -77,16 +59,20 @@ public: DeserializationError error = deserializeJson(json, configFile); configFile.close(); serializeJsonPretty(json, Serial); + Serial.print('\n'); if (!error) { Serial.println("SDCard: Parsing JSON"); - strcpy(Settings.WifiSSID, json["SSID"]); - strcpy(Settings.WifiPW, json["Password"]); - strcpy(Settings.PoolAddress, json["PoolURL"]); - strcpy(Settings.BtcWallet, json["WalletID"]); - Settings.PoolPort = json["Port"].as(); - Settings.Timezone = json["Timezone"].as(); - Settings.holdsData = true; + 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); + strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet); + if (json.containsKey(JSON_KEY_POOLPORT)) + Settings->PoolPort = json[JSON_KEY_POOLPORT].as(); + if (json.containsKey(JSON_KEY_TIMEZONE)) + Settings->Timezone = json[JSON_KEY_TIMEZONE].as(); + SD_MMC.end(); + return true; } else { @@ -94,26 +80,54 @@ public: Serial.println("SDCard: Failed to load json config"); } } - SD_MMC.end(); } + else + { + Serial.println("SDCard: No config file available!"); + } + SD_MMC.end(); + } + return false; + } + +private: + + bool initSDcard() + { + if((cardInitialized_)&&(SD_MMC.cardType() != CARD_NONE)) + { + Serial.println("SDCard: Already mounted."); + return cardInitialized_; + } + + bool oneBitMode = true; +#if defined (SDMMC_D0) && defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3) + if (SD_MMC.cardType() == CARD_NONE) + { + SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0, SDMMC_D1, SDMMC_D2, SDMMC_D3); + oneBitMode = false; + Serial.println("SDCard: 4-Bit Mode."); + } +#elif defined (SDMMC_D0) && !(defined (SDMMC_D1) && defined (SDMMC_D2) && defined (SDMMC_D3)) + if (SD_MMC.cardType() == CARD_NONE) + { + SD_MMC.setPins(SDMMC_CLK, SDMMC_CMD, SDMMC_D0); + Serial.println("SDCard: 1-Bit Mode."); + } +#else + Serial.println("SDCard: interface not available."); + return false; +#endif // dataPinsDefined + cardInitialized_ = SD_MMC.begin("/sdcard", oneBitMode); + if ((cardInitialized_) && (SD_MMC.cardType() != CARD_NONE)) + { + Serial.println("SDCard: Card mounted."); + return true; } else { - // Error mounting file system - Serial.println("SDCard: Failed to mount."); - } - return Settings; - } - - void SD2SPIStorage(SPIStorage* spifs) - { - TSettings Settings = loadConfigFile(); - if (Settings.holdsData) - { - spifs->saveConfigFile(&Settings); - WiFi.begin(Settings.WifiSSID, Settings.WifiPW); - Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); - ESP.restart(); + Serial.println("SDCard: No card found."); + return false; } } }; diff --git a/src/drivers/storage/SPIStorage.h b/src/drivers/storage/SPIStorage.h index 2661be6..c17d15f 100644 --- a/src/drivers/storage/SPIStorage.h +++ b/src/drivers/storage/SPIStorage.h @@ -12,9 +12,6 @@ #include "..\drivers.h" #include "storage.h" -// JSON configuration file -#define JSON_CONFIG_FILE "/config.json" - class SPIStorage { private: @@ -22,65 +19,60 @@ private: public: SPIStorage() { - SPIFFSInitialized_ = init(); + SPIFFSInitialized_ = false; } ~SPIStorage() { - SPIFFS.end(); + if (SPIFFSInitialized_) + SPIFFS.end(); }; void saveConfigFile(TSettings*Settings) { - // Save Config in JSON format - Serial.println(F("SPIFS: Saving configuration...")); - - // Create a JSON document - StaticJsonDocument<512> json; - json["poolString"] = Settings->PoolAddress; - json["portNumber"] = Settings->PoolPort; - json["btcString"] = Settings->BtcWallet; - json["gmtZone"] = Settings->Timezone; - - // Open config file - File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); - if (!configFile) + if (init()) { - // Error, file did not open - Serial.println("SPIFS: failed to open config file for writing"); - } + // Save Config in JSON format + Serial.println(F("SPIFS: Saving configuration...")); - // Serialize JSON data to write to file - serializeJsonPretty(json, Serial); - if (serializeJson(json, configFile) == 0) - { - // Error writing file - Serial.println(F("SPIFS: Failed to write to file")); - } - // Close file - configFile.close(); + // Create a JSON document + StaticJsonDocument<512> json; + json[JSON_KEY_POOLURL] = Settings->PoolAddress; + json[JSON_KEY_POOLPORT] = Settings->PoolPort; + json[JSON_KEY_WALLETID] = Settings->BtcWallet; + json[JSON_KEY_TIMEZONE] = Settings->Timezone; + + // Open config file + File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); + if (!configFile) + { + // Error, file did not open + Serial.println("SPIFS: failed to open config file for writing"); + } + + // Serialize JSON data to write to file + serializeJsonPretty(json, Serial); + Serial.print('\n'); + if (serializeJson(json, configFile) == 0) + { + // Error writing file + Serial.println(F("SPIFS: Failed to write to file")); + } + // Close file + configFile.close(); + }; } - bool init() + bool loadConfigFile(TSettings* Settings) { - if (SPIFFSInitialized_) - return SPIFFSInitialized_; - return SPIFFS.begin(false) || SPIFFS.begin(true); - }; - - TSettings loadConfigFile() - { - // Load existing configuration file // Uncomment if we need to format filesystem // SPIFFS.format(); + // Load existing configuration file // Read configuration from FS json - Serial.println("SPIFS: Mounting File System..."); - TSettings Settings; - // May need to make it begin(true) first time you are using SPIFFS - if ((SPIFFSInitialized_)||(init())) + + if (init()) { - Serial.println("SPIFS: Mounted"); if (SPIFFS.exists(JSON_CONFIG_FILE)) { // The file exists, reading and loading @@ -93,15 +85,17 @@ public: DeserializationError error = deserializeJson(json, configFile); configFile.close(); serializeJsonPretty(json, Serial); + Serial.print('\n'); if (!error) { Serial.println("SPIFS: Parsing JSON"); - - strcpy(Settings.PoolAddress, json["poolString"]); - strcpy(Settings.BtcWallet, json["btcString"]); - Settings.PoolPort = json["portNumber"].as(); - Settings.Timezone = json["gmtZone"].as(); - Settings.holdsData = true; + strcpy(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(); + if (json.containsKey(JSON_KEY_TIMEZONE)) + Settings->Timezone = json[JSON_KEY_TIMEZONE].as(); + return true; } else { @@ -111,12 +105,7 @@ public: } } } - else - { - // Error mounting file system - Serial.println("SPIFS: Failed to mount."); - } - return Settings; + return false; } void deleteConfigFile() @@ -124,6 +113,22 @@ public: Serial.println("SPIFS: Erasing config file.."); SPIFFS.remove(JSON_CONFIG_FILE); //Borramos fichero } +private: + bool init() + { + if (!SPIFFSInitialized_) + { + Serial.println("SPIFS: Mounting File System..."); + // May need to make it begin(true) first time you are using SPIFFS + SPIFFSInitialized_ = SPIFFS.begin(false) || SPIFFS.begin(true); + SPIFFSInitialized_ ? Serial.println("SPIFS: Mounted") : Serial.println("SPIFS: Mounting failed."); + } + else + { + Serial.println("SPIFS: Already Mounted"); + } + return SPIFFSInitialized_; + }; }; #endif // _SPISTORAGE_H_ diff --git a/src/drivers/storage/UARTCfgUI.h b/src/drivers/storage/UARTCfgUI.h deleted file mode 100644 index 82be232..0000000 --- a/src/drivers/storage/UARTCfgUI.h +++ /dev/null @@ -1,100 +0,0 @@ - -#ifndef _UARTCFGUI_H_ -#define _UARTCFGUI_H_ - -#include - -#include "storage.h" -#include "SPIStorage.h" - -class SerialCfgUI -{ -public: - SerialCfgUI() - { - #ifdef MONITOR_SPEED - Serial.begin(MONITOR_SPEED); - #else - Serial.begin(115200); - #endif //MONITOR_SPEED - } - - void UARTUI2SPIStorage(SPIStorage* spifs) - { - TSettings Settings; - - if ((spifs->loadConfigFile(&Settings)) && StartUI(&Settings)) - { - spifs->saveConfigFile(&Settings); - WiFi.begin(Settings.WifiSSID, Settings.WifiPW); - Serial.println("SerialUI: Settings transfered to internal memory. Restarting now."); - ESP.restart(); - } - Serial.println("SerialUI: not started."); - } - -private: - - bool StartUI(TSettings* Settings) - { - Serial.println("Welcome to Nerdminer v2 serial config Interface."); - Serial.println("Fill out the form to set up your Nerdminer."); - Serial.println("Press 'Enter' to save your input or the default value."); - - strcpy(Settings->WifiSSID, readEntry("Enter Wifi SSID:", Settings->WifiSSID)); - strcpy(Settings->WifiPW, readEntry("Enter Wifi Password:", Settings->WifiPW)); - strcpy(Settings->PoolAddress, readEntry("Enter Pool Address:", Settings->PoolAddress)); - Settings->PoolPort = readEntry("Enter Pool Port:", Settings->PoolPort); - strcpy(Settings->BtcWallet, readEntry("Enter your BTC Wallet ID:", Settings->BtcWallet)); - Settings->Timezone = readEntry("Enter your Timezone (UTC+-12):", Settings->Timezone); - - Serial.println("Setup complete."); - - return true; - } - - template - T readEntry(const char* message = "newEntry:", T defaultvalue = "", const char delimieter = '\n') - requires( - (std::is_same_v) - || (std::is_same_v) - || (std::is_same_v) - || (std::is_same_v) - || (std::is_same_v) - || (std::is_same_v)) - { - Serial.println(message); - - if(!String(defaultvalue).isEmpty()) - { - Serial.print("Default Value: >"); - Serial.print(defaultvalue); - Serial.println("<"); - }; - String value = Serial.readStringUntil(delimieter); - value.trim(); - while((value.length() > 0) && value.endsWith(String('\r'))) - value.remove(value.length()-1); - value.trim(); - if (value.length() > 0) - { - if constexpr (std::is_same_v) - return value; - else if constexpr ((std::is_same_v)|| (std::is_same_v)) - return value.c_str(); - else if constexpr (std::is_same_v) - return value.toInt(); - else if constexpr (std::is_same_v) - return value.toDouble(); - else if constexpr (std::is_same_v) - return value.toFloat(); - } - else - { - return defaultvalue; - } - } -}; - - -#endif // _UARTCFGUI_H_ \ No newline at end of file diff --git a/src/drivers/storage/storage.h b/src/drivers/storage/storage.h index 5fbd4b7..bddb103 100644 --- a/src/drivers/storage/storage.h +++ b/src/drivers/storage/storage.h @@ -1,16 +1,53 @@ #ifndef _STORAGE_H_ #define _STORAGE_H_ +#define DEFAULT_SSID "NMAP" +#define DEFAULT_WIFIPW "1234567890" +#define DEFAULT_POOLURL "public-pool.io" +#define DEFAULT_WALLETID "yourBtcAddress" +#define DEFAULT_POOLPORT 21496 +#define DEFAULT_TIMEZONE 2 + +// JSON config file +#define JSON_CONFIG_FILE "/config.json" +#define JSON_KEY_SSID "SSID" +#define JSON_KEY_PASW "PW" +#define JSON_KEY_POOLURL "PoolUrl" +#define JSON_KEY_WALLETID "BtcWallet" +#define JSON_KEY_POOLPORT "PoolPort" +#define JSON_KEY_TIMEZONE "Timezone" + class TSettings { public: - char WifiSSID[80]{ "sA51" }; - char WifiPW[80]{ "0000" }; - char PoolAddress[80]{ "public-pool.io" }; - char BtcWallet[80]{ "yourBtcAddress" }; - uint32_t PoolPort{ 21496 }; - uint32_t Timezone{ 2 }; - bool holdsData{ false }; + char WifiSSID[80]{ DEFAULT_SSID }; + char WifiPW[80]{ DEFAULT_WIFIPW }; + char PoolAddress[80]{ DEFAULT_POOLURL }; + char BtcWallet[80]{ DEFAULT_WALLETID }; + uint32_t PoolPort{ DEFAULT_POOLPORT }; + uint32_t Timezone{ DEFAULT_TIMEZONE }; + + void print() + { + Serial.print("WifiSSID:>"); + Serial.print(WifiSSID); + Serial.print("<, "); + Serial.print("WifiPW:>"); + Serial.print(WifiPW); + Serial.print("<, "); + Serial.print("PoolAddress:>"); + Serial.print(PoolAddress); + Serial.print("<, "); + Serial.print("BtcWallet:>"); + Serial.print(BtcWallet); + Serial.print("<, "); + Serial.print("PoolPort:>"); + Serial.print(PoolPort); + Serial.print("<, "); + Serial.print("Timezone:>"); + Serial.print(Timezone); + Serial.println("<"); + } }; #endif // _STORAGE_H_ \ No newline at end of file diff --git a/src/mining.cpp b/src/mining.cpp index ce65d99..0395b57 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -402,8 +402,8 @@ void runMonitor(void *name) if (elapsedKHs == 0) { Serial.printf(">>> [i] Miner: newJob>%s / inRun>%s) - Client: connected>%s / subscribed>%s / wificonnected>%s\n", - mMiner.newJob ? "true" : "false", mMiner.inRun ? "true" : "false", - client.connected() ? "true" : "false", isMinerSuscribed ? "true" : "false", WiFi.status() == WL_CONNECTED ? "true" : "false"); + mMiner.newJob ? "true" : "false", mMiner.inRun ? "true" : "false", + client.connected() ? "true" : "false", isMinerSuscribed ? "true" : "false", WiFi.status() == WL_CONNECTED ? "true" : "false"); } #ifdef DEBUG_MEMORY diff --git a/src/wManager.cpp b/src/wManager.cpp index 65abb86..e49164b 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -12,6 +12,7 @@ #include "drivers/storage/SPIStorage.h" #include "drivers/storage/storage.h" + // Flag for saving data bool shouldSaveConfig = false; @@ -45,7 +46,7 @@ void configModeCallback(WiFiManager* myWiFiManager) Serial.println(WiFi.softAPIP()); } -void reset_configurations() +void reset_configurations() { Serial.println("Erasing Config, restarting"); SPIFS.deleteConfigFile(); @@ -53,7 +54,6 @@ void reset_configurations() ESP.restart(); } - void init_WifiManager() { #ifdef MONITOR_SPEED @@ -76,7 +76,7 @@ void init_WifiManager() // Check if button2 is pressed to enter configMode with actual configuration if (!digitalRead(PIN_BUTTON_2)) { Serial.println(F("Button pressed to force start config mode")); - resetConfig(); + reset_configurations(); forceConfig = true; wm.setBreakAfterConfig(true); //Set to detect config edition and save } @@ -84,16 +84,20 @@ void init_WifiManager() // Explicitly set WiFi mode WiFi.mode(WIFI_STA); - Settings = SPIFS.loadConfigFile(); - if (!Settings.holdsData) + if (!SPIFS.loadConfigFile(&Settings)) { + Serial.println(F("No config file on internal flash.")); SDCard sdc; - if (sdc.loadConfigFile().holdsData) + if (!sdc.loadConfigFile(&Settings)) { - Serial.println(F("No config file on internal flash, force config mode.")); - sdc.SD2SPIStorage(&SPIFS); // reboot on success. + Serial.println(F("No config file on SD card.")); forceConfig = true; - }; + } + else + { + Serial.println(F("Config file on SD card. Copy and restart.")); + sdc.SD2SPIStorage(&SPIFS); // reboot on success. + } }; // Reset settings (only for development) @@ -136,7 +140,7 @@ void init_WifiManager() char charZone[6]; sprintf(charZone, "%d", Settings.Timezone); WiFiManagerParameter time_text_box_num("TimeZone", "TimeZone fromUTC (-12/+12)", charZone, 3); - + // Add all defined parameters wm.addParameter(&pool_text_box); wm.addParameter(&port_text_box_num); @@ -144,21 +148,23 @@ void init_WifiManager() wm.addParameter(&time_text_box_num); Serial.println("AllDone: "); - if (forceConfig) - // Run if we need a configuration + if (forceConfig) { + // Run if we need a configuration //No configuramos timeout al modulo wm.setConfigPortalBlocking(true); //Hacemos que el portal SI bloquee el firmware drawSetupScreen(); - Settings = TSettings(); - if (!wm.startConfigPortal(Settings.WifiSSID, Settings.WifiPW)) + + if (!wm.startConfigPortal(DEFAULT_SSID, DEFAULT_WIFIPW)) { Serial.println("failed to connect and hit timeout"); //Could be break forced after edditing, so save new config + strncpy(Settings.PoolAddress, pool_text_box.getValue(), sizeof(Settings.PoolAddress)); 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()); + SPIFS.saveConfigFile(&Settings); delay(3000); //reset and try again, or maybe put it to deep sleep @@ -212,6 +218,7 @@ void init_WifiManager() Settings.Timezone = atoi(time_text_box_num.getValue()); Serial.print("TimeZone fromUTC: "); Serial.println(Settings.Timezone); + } // Save the custom parameters to FS