bugfix, optimization

This commit is contained in:
elmo128 2023-09-21 17:41:21 +02:00
parent 2057ba8082
commit 89287f43c9
7 changed files with 28 additions and 28 deletions

View File

@ -26,13 +26,12 @@ SDCard::~SDCard()
unmount(); unmount();
} }
void SDCard::SD2nvMemory(nvMemory* nvMem) void SDCard::SD2nvMemory(nvMemory* nvMem, TSettings* Settings)
{ {
TSettings Settings; if (loadConfigFile(Settings))
if (loadConfigFile(&Settings))
{ {
nvMem->saveConfig(&Settings); nvMem->saveConfig(Settings);
WiFi.begin(Settings.WifiSSID, Settings.WifiPW); WiFi.begin(Settings->WifiSSID, Settings->WifiPW);
Serial.println("SDCard: Settings transfered to internal memory. Restarting now."); Serial.println("SDCard: Settings transfered to internal memory. Restarting now.");
ESP.restart(); ESP.restart();
} }
@ -60,9 +59,9 @@ bool SDCard::loadConfigFile(TSettings* Settings)
unmount(); unmount();
if (!error) if (!error)
{ {
strcpy(Settings->WifiSSID, json[JSON_KEY_SSID] | Settings->WifiSSID); Settings->WifiSSID = json[JSON_KEY_SSID] | Settings->WifiSSID;
strcpy(Settings->WifiPW, json[JSON_KEY_PASW] | Settings->WifiPW); Settings->WifiPW = json[JSON_KEY_PASW] | Settings->WifiPW;
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); strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet);
if (json.containsKey(JSON_KEY_POOLPORT)) if (json.containsKey(JSON_KEY_POOLPORT))
Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>(); Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>();
@ -149,7 +148,7 @@ bool SDCard::initSDcard()
SDCard::SDCard() {} SDCard::SDCard() {}
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::loadConfigFile(TSettings* Settings) { return false; }
bool SDCard::initSDcard() { return false; } bool SDCard::initSDcard() { return false; }
void unmount() {} void unmount() {}

View File

@ -21,7 +21,7 @@ class SDCard
public: public:
SDCard(); SDCard();
~SDCard(); ~SDCard();
void SD2nvMemory(nvMemory* nvMem); void SD2nvMemory(nvMemory* nvMem, TSettings* Settings);
bool loadConfigFile(TSettings* Settings); bool loadConfigFile(TSettings* Settings);
private: private:
bool initSDcard(); bool initSDcard();

View File

@ -84,7 +84,7 @@ bool nvMemory::loadConfig(TSettings* Settings)
Serial.print('\n'); Serial.print('\n');
if (!error) 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); strcpy(Settings->BtcWallet, json[JSON_KEY_WALLETID] | Settings->BtcWallet);
if (json.containsKey(JSON_KEY_POOLPORT)) if (json.containsKey(JSON_KEY_POOLPORT))
Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>(); Settings->PoolPort = json[JSON_KEY_POOLPORT].as<int>();

View File

@ -21,9 +21,7 @@ private:
bool Initialized_; bool Initialized_;
}; };
#ifdef NVMEM_SPIFFS #ifndef NVMEM_SPIFFS
#define ESP_DRD_USE_SPIFFS true
#else
#error We need some kind of permanent storage implementation! #error We need some kind of permanent storage implementation!
#endif //NVMEM_TYPE #endif //NVMEM_TYPE

View File

@ -1,7 +1,7 @@
#ifndef _STORAGE_H_ #ifndef _STORAGE_H_
#define _STORAGE_H_ #define _STORAGE_H_
#include <inttypes.h> #include <Arduino.h>
#define DEFAULT_SSID "NerdMinerAP" #define DEFAULT_SSID "NerdMinerAP"
#define DEFAULT_WIFIPW "MineYourCoins" #define DEFAULT_WIFIPW "MineYourCoins"
@ -23,12 +23,12 @@
struct TSettings struct TSettings
{ {
char WifiSSID[80]{ DEFAULT_SSID }; String WifiSSID{ DEFAULT_SSID };
char WifiPW[80]{ DEFAULT_WIFIPW }; String WifiPW{ DEFAULT_WIFIPW };
char PoolAddress[80]{ DEFAULT_POOLURL }; String PoolAddress{ DEFAULT_POOLURL };
char BtcWallet[80]{ DEFAULT_WALLETID }; char BtcWallet[80]{ DEFAULT_WALLETID };
uint32_t PoolPort{ DEFAULT_POOLPORT }; int PoolPort{ DEFAULT_POOLPORT };
uint32_t Timezone{ DEFAULT_TIMEZONE }; int Timezone{ DEFAULT_TIMEZONE };
bool saveStats{ DEFAULT_SAVESTATS }; bool saveStats{ DEFAULT_SAVESTATS };
}; };

View File

@ -59,14 +59,14 @@ bool checkPoolConnection(void) {
//Resolve first time pool DNS and save IP //Resolve first time pool DNS and save IP
if(serverIP == IPAddress(1,1,1,1)) { 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()); Serial.printf("Resolved DNS and save ip (first time) got: %s\n", serverIP.toString());
} }
//Try connecting pool IP //Try connecting pool IP
if (!client.connect(serverIP, Settings.PoolPort)) { if (!client.connect(serverIP, Settings.PoolPort)) {
Serial.println("Imposible to connect to : " + String(Settings.PoolAddress)); Serial.println("Imposible to connect to : " + Settings.PoolAddress);
WiFi.hostByName(Settings.PoolAddress, serverIP); WiFi.hostByName(Settings.PoolAddress.c_str(), serverIP);
Serial.printf("Resolved DNS got: %s\n", serverIP.toString()); Serial.printf("Resolved DNS got: %s\n", serverIP.toString());
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
return false; return false;

View File

@ -1,6 +1,8 @@
#define ESP_DRD_USE_SPIFFS true
// Include Libraries // Include Libraries
//#include ".h" //#include ".h"
#include <WiFi.h> #include <WiFi.h>
#include <WiFiManager.h> #include <WiFiManager.h>
@ -89,7 +91,7 @@ void init_WifiManager()
if (SDCrd.loadConfigFile(&Settings)) if (SDCrd.loadConfigFile(&Settings))
{ {
//Config file on SD card. //Config file on SD card.
SDCrd.SD2nvMemory(&nvMem); // reboot on success. SDCrd.SD2nvMemory(&nvMem, &Settings); // reboot on success.
} }
else else
{ {
@ -122,7 +124,7 @@ void init_WifiManager()
// Custom elements // Custom elements
// Text box (String) - 80 characters maximum // 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. // Need to convert numerical input to string to display the default value.
char convertedValue[6]; char convertedValue[6];
@ -168,7 +170,7 @@ void init_WifiManager()
{ {
//Could be break forced after edditing, so save new config //Could be break forced after edditing, so save new config
Serial.println("failed to connect and hit timeout"); 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()); Settings.PoolPort = atoi(port_text_box_num.getValue());
strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet)); strncpy(Settings.BtcWallet, addr_text_box.getValue(), sizeof(Settings.BtcWallet));
Settings.Timezone = atoi(time_text_box_num.getValue()); 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 //Tratamos de conectar con la configuración inicial ya almacenada
mMonitor.NerdStatus = NM_Connecting; mMonitor.NerdStatus = NM_Connecting;
wm.setCaptivePortalEnable(false); // disable captive portal redirection 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"); Serial.println("Failed to connect and hit timeout");
//delay(3000); //delay(3000);
@ -210,7 +212,8 @@ void init_WifiManager()
// Lets deal with the user config values // Lets deal with the user config values
// Copy the string value // 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.print("PoolString: ");
Serial.println(Settings.PoolAddress); Serial.println(Settings.PoolAddress);