diff --git a/src/mining.cpp b/src/mining.cpp index 3d9d74b..9bf3fce 100644 --- a/src/mining.cpp +++ b/src/mining.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "ShaTests/nerdSHA256.h" //#include "ShaTests/nerdSHA256plus.h" #include "stratum.h" @@ -10,14 +12,17 @@ #include "monitor.h" #include "drivers/display.h" -unsigned long templates = 0; -unsigned long hashes= 0; -unsigned long Mhashes = 0; -unsigned long totalKHashes = 0; -unsigned long elapsedKHs = 0; +nvs_handle_t stat_handle; -unsigned int shares; // increase if blockhash has 32 bits of zeroes -unsigned int valids; // increased if blockhash <= target +uint32_t templates = 0; +uint32_t hashes = 0; +uint32_t Mhashes = 0; +uint32_t totalKHashes = 0; +uint32_t elapsedKHs = 0; +uint64_t upTime = 0; + +uint32_t shares; // increase if blockhash has 32 bits of zeroes +uint32_t valids; // increased if blockhash <= target // Track best diff double best_diff = 0.0; @@ -27,6 +32,7 @@ extern char poolString[80]; extern int portNumber; extern char btcString[80]; IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres +extern bool saveStatsToNVS; //Track mining stats in non volatile memory //Global work data static WiFiClient client; @@ -37,6 +43,10 @@ monitor_data mMonitor; bool isMinerSuscribed = false; unsigned long mLastTXtoPool = millis(); +int saveIntervals[7] = {5 * 60, 15 * 60, 30 * 60, 1 * 360, 3 * 360, 6 * 360, 12 * 360}; +int saveIntervalsSize = sizeof(saveIntervals)/sizeof(saveIntervals[0]); +int currentIntervalIndex = 0; + bool checkPoolConnection(void) { if (client.connected()) { @@ -375,10 +385,41 @@ void runMiner(void * task_id) { #define DELAY 100 #define REDRAW_EVERY 10 +void restoreStat() { + if(!saveStatsToNVS) return; + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + Serial.printf("[MONITOR] NVS partition is full or has invalid version, erasing...\n"); + nvs_flash_init(); + } + + ret = nvs_open("state", NVS_READWRITE, &stat_handle); + + size_t required_size = sizeof(double); + nvs_get_blob(stat_handle, "best_diff", &best_diff, &required_size); + nvs_get_u32(stat_handle, "Mhashes", &Mhashes); + nvs_get_u32(stat_handle, "shares", &shares); + nvs_get_u32(stat_handle, "valids", &valids); + nvs_get_u32(stat_handle, "templates", &templates); + nvs_get_u64(stat_handle, "upTime", &upTime); +} + +void saveStat() { + if(!saveStatsToNVS) return; + Serial.printf("[MONITOR] Saving stats\n"); + nvs_set_blob(stat_handle, "best_diff", &best_diff, sizeof(double)); + nvs_set_u32(stat_handle, "Mhashes", Mhashes); + nvs_set_u32(stat_handle, "shares", shares); + nvs_set_u32(stat_handle, "valids", valids); + nvs_set_u32(stat_handle, "templates", templates); + nvs_set_u64(stat_handle, "upTime", upTime + (esp_timer_get_time()/1000000)); +} + void runMonitor(void *name) { Serial.println("[MONITOR] started"); + restoreStat(); unsigned long mLastCheck = 0; @@ -386,6 +427,10 @@ void runMonitor(void *name) unsigned long frame = 0; + uint32_t seconds_elapsed = 0; + + totalKHashes = (Mhashes * 1000) + hashes / 1000;; + while (1) { if ((frame % REDRAW_EVERY) == 0) @@ -410,11 +455,19 @@ void runMonitor(void *name) Serial.printf("### [Total Heap / Free heap]: %d / %d \n", ESP.getHeapSize(), ESP.getFreeHeap()); Serial.printf("### Max stack usage: %d\n", uxTaskGetStackHighWaterMark(NULL)); #endif + + seconds_elapsed++; + + if(seconds_elapsed % (saveIntervals[currentIntervalIndex]) == 0){ + saveStat(); + seconds_elapsed = 0; + if(currentIntervalIndex < saveIntervalsSize - 1) + currentIntervalIndex++; + } } animateCurrentScreen(frame); doLedStuff(frame); - // Pause the task for 1000ms vTaskDelay(DELAY / portTICK_PERIOD_MS); frame++; } diff --git a/src/monitor.cpp b/src/monitor.cpp index 062f3a3..7cd942b 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -9,14 +9,15 @@ #include "monitor.h" extern char poolString[80]; -extern unsigned long templates; -extern unsigned long hashes; -extern unsigned long Mhashes; -extern unsigned long totalKHashes; -extern unsigned long elapsedKHs; +extern uint32_t templates; +extern uint32_t hashes; +extern uint32_t Mhashes; +extern uint32_t totalKHashes; +extern uint32_t elapsedKHs; +extern uint64_t upTime; -extern unsigned int shares; // increase if blockhash has 32 bits of zeroes -extern unsigned int valids; // increased if blockhash <= target +extern uint32_t shares; // increase if blockhash has 32 bits of zeroes +extern uint32_t valids; // increased if blockhash <= targethalfshares extern double best_diff; // track best diff @@ -222,7 +223,7 @@ mining_data getMiningData(unsigned long mElapsed) suffix_string(best_diff, best_diff_string, 16, 0); char timeMining[15] = {0}; - unsigned long secElapsed = millis() / 1000; + uint64_t secElapsed = upTime + (esp_timer_get_time() / 1000000); int days = secElapsed / 86400; int hours = (secElapsed - (days * 86400)) / 3600; // Number of seconds in an hour int mins = (secElapsed - (days * 86400) - (hours * 3600)) / 60; // Remove the number of hours and calculate the minutes. @@ -289,6 +290,5 @@ coin_data getCoinData(unsigned long mElapsed) data.progressPercent = (HALVING_BLOCKS - remainingBlocks) * 100 / HALVING_BLOCKS; data.remainingBlocks = String(remainingBlocks) + " BLOCKS"; - return data; } \ No newline at end of file diff --git a/src/wManager.cpp b/src/wManager.cpp index 51b5d27..1e94ae4 100644 --- a/src/wManager.cpp +++ b/src/wManager.cpp @@ -23,6 +23,7 @@ char poolString[80] = "public-pool.io"; int portNumber = 21496;//3333; char btcString[80] = "yourBtcAddress"; int GMTzone = 2; //Currently selected in spain +bool saveStatsToNVS = false; //Track mining stats in non volatile memory // Define WiFiManager Object @@ -40,6 +41,7 @@ void saveConfigFile() json["portNumber"] = portNumber; json["btcString"] = btcString; json["gmtZone"] = GMTzone; + json["saveStatsToNVS"] = String(saveStatsToNVS); // Open config file File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); @@ -93,6 +95,8 @@ bool loadConfigFile() strcpy(btcString, json["btcString"]); portNumber = json["portNumber"].as(); GMTzone = json["gmtZone"].as(); + if(json.containsKey("saveStatsToNVS")) + saveStatsToNVS = json["saveStatsToNVS"].as(); return true; } else @@ -206,13 +210,24 @@ void init_WifiManager() // Text box (Number) - 2 characters maximum char charZone[6]; sprintf(charZone, "%d", GMTzone); - WiFiManagerParameter time_text_box_num("TimeZone", "TimeZone fromUTC (-12/+12)", charZone, 3); + WiFiManagerParameter time_text_box_num("TimeZone", "TimeZone fromUTC (-12/+12)", charZone, 3); + + WiFiManagerParameter features_html("

"); + + char checkboxParams[24] = "type=\"checkbox\""; + if (saveStatsToNVS) + { + strcat(checkboxParams, " checked"); + } + WiFiManagerParameter save_stats_to_nvs("SaveStatsToNVS", "Track Uptime, Best Diff, Total Hashes in device Flash memory. (Experimental)", "T", 2, checkboxParams, WFM_LABEL_AFTER); // Add all defined parameters wm.addParameter(&pool_text_box); wm.addParameter(&port_text_box_num); wm.addParameter(&addr_text_box); wm.addParameter(&time_text_box_num); + wm.addParameter(&features_html); + wm.addParameter(&save_stats_to_nvs); Serial.println("AllDone: "); if (forceConfig) @@ -229,6 +244,7 @@ void init_WifiManager() portNumber = atoi(port_text_box_num.getValue()); strncpy(btcString, addr_text_box.getValue(), sizeof(btcString)); GMTzone = atoi(time_text_box_num.getValue()); + saveStatsToNVS = (strncmp(save_stats_to_nvs.getValue(), "T", 1) == 0); saveConfigFile(); delay(3000); //reset and try again, or maybe put it to deep sleep