diff --git a/src/NerdMinerV2.ino.cpp b/src/NerdMinerV2.ino.cpp index 4165105..5fd9a35 100644 --- a/src/NerdMinerV2.ino.cpp +++ b/src/NerdMinerV2.ino.cpp @@ -16,6 +16,7 @@ #include "timeconst.h" #include "drivers/nerd-nos/bm1397.h" #include "drivers/nerd-nos/serial.h" +#include "drivers/nerd-nos/adc.h" #ifdef NERD_NOS #include "mining_nerdnos.h" @@ -147,6 +148,7 @@ void setup() #endif #ifdef NERD_NOS + nerdnos_adc_init(); SERIAL_init(); int chips = BM1397_init(200, 1); Serial.printf("found bm1397: %d\n", chips); diff --git a/src/drivers/displays/tDisplayDriver.cpp b/src/drivers/displays/tDisplayDriver.cpp index 3bab4a2..bb0e470 100644 --- a/src/drivers/displays/tDisplayDriver.cpp +++ b/src/drivers/displays/tDisplayDriver.cpp @@ -25,7 +25,7 @@ void tDisplay_Init(void) pinMode(PIN_ENABLE5V, OUTPUT); digitalWrite(PIN_ENABLE5V, HIGH); #endif - + tft.init(); #ifdef LILYGO_S3_T_EMBED tft.setRotation(ROTATION_270); @@ -65,9 +65,13 @@ void tDisplay_MinerScreen(unsigned long mElapsed) // Print background screen background.pushImage(0, 0, MinerWidth, MinerHeight, MinerScreen); - +#ifdef NERD_NOS + Serial.printf(">>> Completed %s share(s), %s Khashes, avg. hashrate %s GH/s, %s °C\n", + data.completedShares.c_str(), data.totalKHashes.c_str(), data.currentHashRate.c_str(), data.currentTemperature.c_str()); +#else Serial.printf(">>> Completed %s share(s), %s Khashes, avg. hashrate %s KH/s\n", data.completedShares.c_str(), data.totalKHashes.c_str(), data.currentHashRate.c_str()); +#endif // Hashrate render.setFontSize(35); @@ -211,7 +215,7 @@ void tDisplay_BTCprice(unsigned long mElapsed) { clock_data data = getClockData(mElapsed); data.currentDate ="01/12/2023"; - + //if(data.currentDate.indexOf("12/2023")>) { tDisplay_ChristmasContent(data); return; } // Print background screen @@ -231,14 +235,14 @@ void tDisplay_BTCprice(unsigned long mElapsed) render.rdrawString(data.blockHeight.c_str(), 254, 138, TFT_WHITE); // Print Hour - + background.setFreeFont(FSSB9); background.setTextSize(1); background.setTextDatum(TL_DATUM); background.setTextColor(TFT_BLACK); background.drawString(data.currentTime.c_str(), 222, 3, GFXFF); - // Print BTC Price + // Print BTC Price background.setFreeFont(FF24); background.setTextDatum(TR_DATUM); background.setTextSize(1); diff --git a/src/drivers/nerd-nos/adc.cpp b/src/drivers/nerd-nos/adc.cpp new file mode 100644 index 0000000..1699672 --- /dev/null +++ b/src/drivers/nerd-nos/adc.cpp @@ -0,0 +1,48 @@ +#include +#include +#include "driver/adc.h" +#include "esp_adc_cal.h" + +#define ADC_CHANNEL ADC1_CHANNEL_1 // GPIO2 corresponds to ADC1 channel 1 on ESP32-S3 +#define BETA 3380 // Beta value of the thermistor +#define R0 10000 // Resistance at 25°C (10kΩ) +#define ADC_MAX 4095 // Max ADC value for 12-bit resolution +#define DEFAULT_VREF 1100 // Default VREF in millivolts for ESP32 ADC + +// ADC Calibration characteristics +static esp_adc_cal_characteristics_t adc1_chars; + +void nerdnos_adc_init() { + // Configure the ADC + adc1_config_width(ADC_WIDTH_BIT_12); // Set ADC width (12-bit) + adc1_config_channel_atten(ADC_CHANNEL, ADC_ATTEN_DB_11); // Set attenuation to read the full range of 0 to 3.3V + + // Characterize ADC at given attenuation + esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, &adc1_chars); +} + +float nerdnos_get_temperature() { + // Convert the raw ADC value to a voltage using esp_adc_cal + uint32_t voltage_mv = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC_CHANNEL), &adc1_chars); // Voltage in millivolts + + // Convert millivolts to volts + float voltage = voltage_mv / 1000.0; + + // Ensure the voltage is within a valid range + if (voltage <= 0) { + printf("Error: Invalid voltage reading.\n"); + return -273.15; // Return a clearly invalid temperature to indicate an error + } + + // Calculate the thermistor resistance using the voltage divider formula + // R_T = R0 * (Vout / (VREF - Vout)) + float thermistor_resistance = R0 * (voltage / (3.3 - voltage)); + + // Use the Beta parameter equation to calculate the temperature in Kelvin + float temperature_kelvin = (float)(BETA / (log(thermistor_resistance / R0) + (BETA / 298.15))); + + // Convert the temperature to Celsius + float temperature_celsius = temperature_kelvin - 273.15; + + return temperature_celsius; +} diff --git a/src/drivers/nerd-nos/adc.h b/src/drivers/nerd-nos/adc.h new file mode 100644 index 0000000..787ce2b --- /dev/null +++ b/src/drivers/nerd-nos/adc.h @@ -0,0 +1,4 @@ +#pragma once + +void nerdnos_adc_init(); +float nerdnos_get_temperature(); diff --git a/src/drivers/nerd-nos/bm1397.cpp b/src/drivers/nerd-nos/bm1397.cpp index 1588cf0..4f2fffa 100644 --- a/src/drivers/nerd-nos/bm1397.cpp +++ b/src/drivers/nerd-nos/bm1397.cpp @@ -63,8 +63,8 @@ static void _send_BM1397(uint8_t header, uint8_t *data, uint8_t data_len, bool d packet_type_t packet_type = (header & TYPE_JOB) ? JOB_PACKET : CMD_PACKET; uint8_t total_length = (packet_type == JOB_PACKET) ? (data_len + 6) : (data_len + 5); - // allocate memory for buffer - unsigned char *buf = (unsigned char *)malloc(total_length); + // memory for buffer + uint8_t buf[total_length]; // add the preamble buf[0] = 0x55; @@ -93,8 +93,6 @@ static void _send_BM1397(uint8_t header, uint8_t *data, uint8_t data_len, bool d // send serial data SERIAL_send(buf, total_length, debug); - - free(buf); } static void _send_read_address(void) diff --git a/src/drivers/nerd-nos/mining.cpp b/src/drivers/nerd-nos/mining.cpp index 6f2ed8e..929b072 100644 --- a/src/drivers/nerd-nos/mining.cpp +++ b/src/drivers/nerd-nos/mining.cpp @@ -129,7 +129,6 @@ void nerdnos_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *ne calculate_merkle_root_hash(coinbase_tx.c_str(), job, merkle_root); //Serial.printf("asic merkle root: %s\n", merkle_root); - // we need malloc because we will save it in the job array construct_bm_job(job, merkle_root, 0x1fffe000, next_job); next_job->jobid = strdup(job->job_id.c_str()); diff --git a/src/monitor.cpp b/src/monitor.cpp index 02b4640..376df01 100644 --- a/src/monitor.cpp +++ b/src/monitor.cpp @@ -11,6 +11,7 @@ #ifdef NERD_NOS #include "mining_nerdnos.h" +#include "drivers/nerd-nos/adc.h" #endif extern uint32_t templates; @@ -243,11 +244,19 @@ String getCurrentHashRate(unsigned long mElapsed) { // we have too little space for 2 digits after the decimal point return String(nerdnos_get_avg_hashrate(), 1); } + +String getCurrentTemperature() { + return String(nerdnos_get_temperature(), 2); +} #else String getCurrentHashRate(unsigned long mElapsed) { return String((1.0 * (elapsedKHs * 1000)) / mElapsed, 2); } + +String getCurrentTemperature() { + return String(0.0, 2); +} #endif mining_data getMiningData(unsigned long mElapsed) @@ -269,6 +278,7 @@ mining_data getMiningData(unsigned long mElapsed) data.totalMHashes = Mhashes; data.totalKHashes = totalKHashes; data.currentHashRate = getCurrentHashRate(mElapsed); + data.currentTemperature = getCurrentTemperature(); data.templates = templates; data.bestDiff = best_diff_string; data.timeMining = timeMining; diff --git a/src/monitor.h b/src/monitor.h index bf2b7b6..0ac6e79 100644 --- a/src/monitor.h +++ b/src/monitor.h @@ -12,7 +12,7 @@ //Time update period #define UPDATE_PERIOD_h 5 -//API BTC price (Update to USDT cus it's more liquidity and flow price updade) +//API BTC price (Update to USDT cus it's more liquidity and flow price updade) #define getBTCAPI "https://api.blockchain.com/v3/exchange/tickers/BTC-USDT" #define UPDATE_BTC_min 1 @@ -73,6 +73,7 @@ typedef struct { String valids; String temp; String currentTime; + String currentTemperature; }mining_data; typedef struct { @@ -81,7 +82,7 @@ typedef struct { String currentHashRate; String btcPrice; String blockHeight; - String currentTime; + String currentTime; String currentDate; }clock_data;