Save stats to NVRAM.

This commit is contained in:
Roman Mashta 2023-09-07 23:01:50 +03:00
parent 819bf9a849
commit 73fea48800
3 changed files with 87 additions and 18 deletions

View File

@ -2,6 +2,8 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <WiFi.h> #include <WiFi.h>
#include <esp_task_wdt.h> #include <esp_task_wdt.h>
#include <nvs_flash.h>
#include <nvs.h>
#include "ShaTests/nerdSHA256.h" #include "ShaTests/nerdSHA256.h"
//#include "ShaTests/nerdSHA256plus.h" //#include "ShaTests/nerdSHA256plus.h"
#include "stratum.h" #include "stratum.h"
@ -10,14 +12,17 @@
#include "monitor.h" #include "monitor.h"
#include "drivers/display.h" #include "drivers/display.h"
unsigned long templates = 0; nvs_handle_t stat_handle;
unsigned long hashes= 0;
unsigned long Mhashes = 0;
unsigned long totalKHashes = 0;
unsigned long elapsedKHs = 0;
unsigned int shares; // increase if blockhash has 32 bits of zeroes uint32_t templates = 0;
unsigned int valids; // increased if blockhash <= target 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 // Track best diff
double best_diff = 0.0; double best_diff = 0.0;
@ -27,6 +32,7 @@ extern char poolString[80];
extern int portNumber; extern int portNumber;
extern char btcString[80]; extern char btcString[80];
IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres
extern bool saveStatsToNVS; //Track mining stats in non volatile memory
//Global work data //Global work data
static WiFiClient client; static WiFiClient client;
@ -37,6 +43,10 @@ monitor_data mMonitor;
bool isMinerSuscribed = false; bool isMinerSuscribed = false;
unsigned long mLastTXtoPool = millis(); 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) { bool checkPoolConnection(void) {
if (client.connected()) { if (client.connected()) {
@ -375,10 +385,41 @@ void runMiner(void * task_id) {
#define DELAY 100 #define DELAY 100
#define REDRAW_EVERY 10 #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) void runMonitor(void *name)
{ {
Serial.println("[MONITOR] started"); Serial.println("[MONITOR] started");
restoreStat();
unsigned long mLastCheck = 0; unsigned long mLastCheck = 0;
@ -386,6 +427,10 @@ void runMonitor(void *name)
unsigned long frame = 0; unsigned long frame = 0;
uint32_t seconds_elapsed = 0;
totalKHashes = (Mhashes * 1000) + hashes / 1000;;
while (1) while (1)
{ {
if ((frame % REDRAW_EVERY) == 0) 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("### [Total Heap / Free heap]: %d / %d \n", ESP.getHeapSize(), ESP.getFreeHeap());
Serial.printf("### Max stack usage: %d\n", uxTaskGetStackHighWaterMark(NULL)); Serial.printf("### Max stack usage: %d\n", uxTaskGetStackHighWaterMark(NULL));
#endif #endif
seconds_elapsed++;
if(seconds_elapsed % (saveIntervals[currentIntervalIndex]) == 0){
saveStat();
seconds_elapsed = 0;
if(currentIntervalIndex < saveIntervalsSize - 1)
currentIntervalIndex++;
}
} }
animateCurrentScreen(frame); animateCurrentScreen(frame);
doLedStuff(frame); doLedStuff(frame);
// Pause the task for 1000ms
vTaskDelay(DELAY / portTICK_PERIOD_MS); vTaskDelay(DELAY / portTICK_PERIOD_MS);
frame++; frame++;
} }

View File

@ -9,14 +9,15 @@
#include "monitor.h" #include "monitor.h"
extern char poolString[80]; extern char poolString[80];
extern unsigned long templates; extern uint32_t templates;
extern unsigned long hashes; extern uint32_t hashes;
extern unsigned long Mhashes; extern uint32_t Mhashes;
extern unsigned long totalKHashes; extern uint32_t totalKHashes;
extern unsigned long elapsedKHs; extern uint32_t elapsedKHs;
extern uint64_t upTime;
extern unsigned int shares; // increase if blockhash has 32 bits of zeroes extern uint32_t shares; // increase if blockhash has 32 bits of zeroes
extern unsigned int valids; // increased if blockhash <= target extern uint32_t valids; // increased if blockhash <= targethalfshares
extern double best_diff; // track best diff 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); suffix_string(best_diff, best_diff_string, 16, 0);
char timeMining[15] = {0}; char timeMining[15] = {0};
unsigned long secElapsed = millis() / 1000; uint64_t secElapsed = upTime + (esp_timer_get_time() / 1000000);
int days = secElapsed / 86400; int days = secElapsed / 86400;
int hours = (secElapsed - (days * 86400)) / 3600; // Number of seconds in an hour 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. 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.progressPercent = (HALVING_BLOCKS - remainingBlocks) * 100 / HALVING_BLOCKS;
data.remainingBlocks = String(remainingBlocks) + " BLOCKS"; data.remainingBlocks = String(remainingBlocks) + " BLOCKS";
return data; return data;
} }

View File

@ -23,6 +23,7 @@ char poolString[80] = "public-pool.io";
int portNumber = 21496;//3333; int portNumber = 21496;//3333;
char btcString[80] = "yourBtcAddress"; char btcString[80] = "yourBtcAddress";
int GMTzone = 2; //Currently selected in spain int GMTzone = 2; //Currently selected in spain
bool saveStatsToNVS = false; //Track mining stats in non volatile memory
// Define WiFiManager Object // Define WiFiManager Object
@ -40,6 +41,7 @@ void saveConfigFile()
json["portNumber"] = portNumber; json["portNumber"] = portNumber;
json["btcString"] = btcString; json["btcString"] = btcString;
json["gmtZone"] = GMTzone; json["gmtZone"] = GMTzone;
json["saveStatsToNVS"] = String(saveStatsToNVS);
// Open config file // Open config file
File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w"); File configFile = SPIFFS.open(JSON_CONFIG_FILE, "w");
@ -93,6 +95,8 @@ bool loadConfigFile()
strcpy(btcString, json["btcString"]); strcpy(btcString, json["btcString"]);
portNumber = json["portNumber"].as<int>(); portNumber = json["portNumber"].as<int>();
GMTzone = json["gmtZone"].as<int>(); GMTzone = json["gmtZone"].as<int>();
if(json.containsKey("saveStatsToNVS"))
saveStatsToNVS = json["saveStatsToNVS"].as<int>();
return true; return true;
} }
else else
@ -206,13 +210,24 @@ void init_WifiManager()
// Text box (Number) - 2 characters maximum // Text box (Number) - 2 characters maximum
char charZone[6]; char charZone[6];
sprintf(charZone, "%d", GMTzone); 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("<hr><br><label style=\"font-weight: bold;margin-bottom: 25px;display: inline-block;\">Features</label>");
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 // Add all defined parameters
wm.addParameter(&pool_text_box); wm.addParameter(&pool_text_box);
wm.addParameter(&port_text_box_num); wm.addParameter(&port_text_box_num);
wm.addParameter(&addr_text_box); wm.addParameter(&addr_text_box);
wm.addParameter(&time_text_box_num); wm.addParameter(&time_text_box_num);
wm.addParameter(&features_html);
wm.addParameter(&save_stats_to_nvs);
Serial.println("AllDone: "); Serial.println("AllDone: ");
if (forceConfig) if (forceConfig)
@ -229,6 +244,7 @@ void init_WifiManager()
portNumber = atoi(port_text_box_num.getValue()); portNumber = atoi(port_text_box_num.getValue());
strncpy(btcString, addr_text_box.getValue(), sizeof(btcString)); strncpy(btcString, addr_text_box.getValue(), sizeof(btcString));
GMTzone = atoi(time_text_box_num.getValue()); GMTzone = atoi(time_text_box_num.getValue());
saveStatsToNVS = (strncmp(save_stats_to_nvs.getValue(), "T", 1) == 0);
saveConfigFile(); saveConfigFile();
delay(3000); delay(3000);
//reset and try again, or maybe put it to deep sleep //reset and try again, or maybe put it to deep sleep