NTP time funcitons added
- Get current time specifying offset (GMT) - Update local time vars only every determined period
This commit is contained in:
parent
9139fa7a18
commit
e60e935107
@ -40,3 +40,4 @@ lib_deps =
|
||||
bblanchon/ArduinoJson@^6.21.2
|
||||
https://github.com/tzapu/WiFiManager.git
|
||||
mathertel/OneButton @ ^2.0.3
|
||||
arduino-libraries/NTPClient
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "OpenFontRender.h"
|
||||
#include "wManager.h"
|
||||
#include "mining.h"
|
||||
#include "monitor.h"
|
||||
|
||||
//3 seconds WDT
|
||||
#define WDT_TIMEOUT 3
|
||||
@ -131,9 +132,9 @@ void setup()
|
||||
//Serial.printf("Starting %s %s!\n", "1", res3 == pdPASS? "successful":"failed");
|
||||
|
||||
|
||||
/******** TIME ZONE SETTING *****/
|
||||
configTime(0, 0, ntpServer);
|
||||
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
|
||||
/******** MONITOR SETUP *****/
|
||||
setup_monitor();
|
||||
|
||||
}
|
||||
|
||||
void app_error_fault_handler(void *arg) {
|
||||
|
@ -27,6 +27,7 @@ unsigned int valids; // increased if blockhash <= target
|
||||
extern char poolString[80];
|
||||
extern int portNumber;
|
||||
extern char btcString[80];
|
||||
IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres
|
||||
|
||||
extern OpenFontRender render;
|
||||
extern TFT_eSprite background;
|
||||
@ -37,35 +38,56 @@ static miner_data mMiner; //Global miner data (Create a miner class TODO)
|
||||
mining_subscribe mWorker;
|
||||
mining_job mJob;
|
||||
monitor_data mMonitor;
|
||||
|
||||
bool isMinerSuscribed = false;
|
||||
unsigned long mLastTXtoPool = millis();
|
||||
|
||||
void checkPoolConnection(bool* isMinerSuscribed) {
|
||||
void checkPoolConnection(void) {
|
||||
|
||||
if (client.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
*isMinerSuscribed = false;
|
||||
isMinerSuscribed = false;
|
||||
|
||||
Serial.println("Client not connected, trying to connect...");
|
||||
IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres
|
||||
WiFi.hostByName(poolString, serverIP);
|
||||
Serial.printf("Resolved DNS got: %s\n", serverIP.toString());
|
||||
if (!client.connect(poolString, portNumber)) {
|
||||
|
||||
//Resolve first time pool DNS and save IP
|
||||
if(serverIP == IPAddress(1,1,1,1)) {
|
||||
WiFi.hostByName(poolString, serverIP);
|
||||
Serial.printf("Resolved DNS and save ip (first time) got: %s\n", serverIP.toString());
|
||||
}
|
||||
|
||||
//Try connecting pool IP
|
||||
if (!client.connect(serverIP, portNumber)) {
|
||||
Serial.println("Imposible to connect to : " + String(poolString));
|
||||
WiFi.hostByName(poolString, serverIP);
|
||||
Serial.printf("Resolved DNS got: %s\n", serverIP.toString());
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
//Checks if pool is not sending any data and reconnects again
|
||||
//verifies it even connection is alive, because pool could be stopped sending NOTIFY
|
||||
//Implements a socketKeepAlive function and
|
||||
//checks if pool is not sending any data to reconnect again.
|
||||
//Even connection could be alive, pool could stop sending new job NOTIFY
|
||||
unsigned long mStart0Hashrate = 0;
|
||||
bool checkPoolInactivity(unsigned long inactivityTime){
|
||||
bool checkPoolInactivity(unsigned int keepAliveTime, unsigned long inactivityTime){
|
||||
|
||||
unsigned long currentKHashes = (Mhashes*1000) + hashes/1000;
|
||||
unsigned long elapsedKHs = currentKHashes - totalKHashes;
|
||||
|
||||
// If no shares sent to pool
|
||||
// send something to pool to hold socket oppened
|
||||
if(millis() - mLastTXtoPool > keepAliveTime){
|
||||
mLastTXtoPool = millis();
|
||||
Serial.println(" Sending : KeepAlive suggest_difficulty");
|
||||
//if (client.print("{}\n") == 0) {
|
||||
tx_suggest_difficulty(client, DEFAULT_DIFFICULTY);
|
||||
/*if(tx_suggest_difficulty(client, DEFAULT_DIFFICULTY)){
|
||||
Serial.println(" Sending keepAlive to pool -> Detected client disconnected");
|
||||
return true;
|
||||
}*/
|
||||
}
|
||||
|
||||
if(elapsedKHs == 0){
|
||||
//Check if hashrate is 0 during inactivityTIme
|
||||
if(mStart0Hashrate == 0) mStart0Hashrate = millis();
|
||||
@ -114,7 +136,7 @@ void runStratumWorker(void *name) {
|
||||
portNumber = 3333;
|
||||
//strcpy(btcString,"test");
|
||||
|
||||
checkPoolConnection(&isMinerSuscribed);
|
||||
checkPoolConnection();
|
||||
|
||||
if(!isMinerSuscribed){
|
||||
|
||||
@ -136,10 +158,11 @@ void runStratumWorker(void *name) {
|
||||
tx_suggest_difficulty(client, DEFAULT_DIFFICULTY);
|
||||
|
||||
isMinerSuscribed=true;
|
||||
mLastTXtoPool = millis();
|
||||
}
|
||||
|
||||
//Check if pool is down for almost 5minutes and then restart connection with pool (5min=300000ms)
|
||||
if(checkPoolInactivity(120000)){
|
||||
//Check if pool is down for almost 5minutes and then restart connection with pool (1min=600000ms)
|
||||
if(checkPoolInactivity(KEEPALIVE_TIME_ms, POOLINACTIVITY_TIME_ms)){
|
||||
//Restart connection
|
||||
Serial.println(" Detected more than 2 min without data form stratum server. Closing socket and reopening...");
|
||||
client.stop();
|
||||
@ -256,6 +279,7 @@ void runMiner(void * name){
|
||||
for (size_t i = 0; i < 32; i++)
|
||||
Serial.printf("%02x", hash[i]);
|
||||
Serial.println("");
|
||||
mLastTXtoPool = millis();
|
||||
}
|
||||
|
||||
// check if 32bit share
|
||||
@ -280,6 +304,7 @@ void runMiner(void * name){
|
||||
mbedtls_sha256_free(midstate);
|
||||
|
||||
mMiner.inRun = false;
|
||||
Serial.print(">>> Finished job waiting new data from pool");
|
||||
|
||||
if(hashes>=MAX_NONCE) {
|
||||
Mhashes=Mhashes+MAX_NONCE/1000000;
|
||||
|
@ -6,6 +6,8 @@
|
||||
#define MAX_NONCE 5000000U
|
||||
#define TARGET_NONCE 471136297U
|
||||
#define DEFAULT_DIFFICULTY "1e-9"
|
||||
#define KEEPALIVE_TIME_ms 30000
|
||||
#define POOLINACTIVITY_TIME_ms 60000
|
||||
|
||||
#define TARGET_BUFFER_SIZE 64
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include "media/images.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "OpenFontRender.h"
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include "mining.h"
|
||||
#include "utils.h"
|
||||
#include "monitor.h"
|
||||
@ -23,6 +25,21 @@ extern OpenFontRender render;
|
||||
extern TFT_eSprite background;
|
||||
extern monitor_data mMonitor;
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000);
|
||||
|
||||
void setup_monitor(void){
|
||||
/******** TIME ZONE SETTING *****/
|
||||
|
||||
timeClient.begin();
|
||||
|
||||
// Adjust offset depending on your zone
|
||||
// GMT +2 in seconds (zona horaria de Europa Central)
|
||||
timeClient.setTimeOffset(7200);
|
||||
|
||||
Serial.println("TimeClient setup done");
|
||||
}
|
||||
|
||||
String printLocalTime(){
|
||||
struct tm timeinfo;
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
@ -35,6 +52,36 @@ String printLocalTime(){
|
||||
return LocalHour;
|
||||
}
|
||||
|
||||
unsigned long mTriggerUpdate = 0;
|
||||
unsigned long initialMillis = millis();
|
||||
unsigned long initialTime = 0;
|
||||
|
||||
String getTime(void){
|
||||
|
||||
//Check if need an NTP call to check current time
|
||||
if((mTriggerUpdate == 0) || (millis() - mTriggerUpdate > UPDATE_PERIOD_h * 60 * 1000)){
|
||||
if(WiFi.status() != WL_CONNECTED) return "";
|
||||
timeClient.update(); //NTP call to get current time
|
||||
mTriggerUpdate = millis();
|
||||
initialTime = timeClient.getEpochTime(); // Guarda la hora inicial (en segundos desde 1970)
|
||||
Serial.print("TimeClient NTPupdateTime ");
|
||||
}
|
||||
|
||||
unsigned long elapsedTime = (millis() - mTriggerUpdate) / 1000; // Tiempo transcurrido en segundos
|
||||
unsigned long currentTime = initialTime + elapsedTime; // La hora actual
|
||||
|
||||
// convierte la hora actual en horas, minutos y segundos
|
||||
unsigned long currentHours = currentTime % 86400 / 3600;
|
||||
unsigned long currentMinutes = currentTime % 3600 / 60;
|
||||
unsigned long currentSeconds = currentTime % 60;
|
||||
|
||||
char LocalHour[10];
|
||||
sprintf(LocalHour, "%02d:%02d", currentHours, currentMinutes);
|
||||
|
||||
String mystring(LocalHour);
|
||||
return LocalHour;
|
||||
}
|
||||
|
||||
void show_MinerScreen(unsigned long mElapsed){
|
||||
|
||||
//Print background screen
|
||||
@ -82,12 +129,6 @@ void show_MinerScreen(unsigned long mElapsed){
|
||||
//Valid Blocks
|
||||
render.setFontSize(48);
|
||||
render.drawString(String(valids).c_str(), 285, 56, 0xDEDB);
|
||||
//Print Temp
|
||||
//background.setTextColor(TFT_BLACK);
|
||||
//background.setFreeFont(FF0);
|
||||
//background.drawString("30", 230, 4);
|
||||
//Print Hour
|
||||
//background.drawString("22:10", 250, 4);
|
||||
|
||||
//Print Temp
|
||||
String temp = String(temperatureRead(), 0);
|
||||
@ -99,7 +140,7 @@ void show_MinerScreen(unsigned long mElapsed){
|
||||
|
||||
//Print Hour
|
||||
render.setFontSize(20);
|
||||
render.rdrawString(String(printLocalTime()).c_str(), 286, 1, TFT_BLACK);
|
||||
render.rdrawString(getTime().c_str(), 286, 1, TFT_BLACK);
|
||||
|
||||
//Push prepared background to screen
|
||||
background.pushSprite(0,0);
|
||||
|
@ -8,6 +8,9 @@
|
||||
#define SCREEN_CLOCK 1
|
||||
#define SCREEN_BLOCK 2
|
||||
|
||||
//Time update period
|
||||
#define UPDATE_PERIOD_h 5
|
||||
|
||||
typedef struct{
|
||||
uint8_t screen;
|
||||
bool rotation;
|
||||
@ -23,6 +26,7 @@ typedef struct{
|
||||
*/
|
||||
}monitor_data;
|
||||
|
||||
void setup_monitor(void);
|
||||
void show_MinerScreen(unsigned long mElapsed);
|
||||
|
||||
#endif //MONITOR_API_H
|
@ -245,7 +245,6 @@ bool tx_suggest_difficulty(WiFiClient& client, const char * difficulty)
|
||||
sprintf(payload, "{\"id\": %d, \"method\": \"mining.suggest_difficulty\", \"params\": [%s]}\n", id, difficulty);
|
||||
|
||||
Serial.print(" Sending : "); Serial.print(payload);
|
||||
client.print(payload);
|
||||
return client.print(payload);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user