From bd374d170d606126adf2cdf9f002eb8a081035fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hilmi=20S=C3=B6nmez?= Date: Fri, 8 Sep 2023 01:39:46 +0200 Subject: [PATCH] Telnet & update shares without reload page If the ESP8266 is not connected to the PC there is no possibility of output via the serial port. I added a Telnet server to the code. Now a connection can be established via the IP using a Telnet client such as Putty and the mining progress can be displayed. Secondly, I expanded the code so that the current shares, in addition to the hashrate, are updated via push without the page having to be reloaded. --- ESP8266_Code/ESP8266_Code.ino | 162 ++++++++++++++++++++++++++++++++-- 1 file changed, 155 insertions(+), 7 deletions(-) diff --git a/ESP8266_Code/ESP8266_Code.ino b/ESP8266_Code/ESP8266_Code.ino index ffccd876..8eae2e03 100644 --- a/ESP8266_Code/ESP8266_Code.ino +++ b/ESP8266_Code/ESP8266_Code.ino @@ -55,6 +55,9 @@ // If you don't know what MQTT means check this link: // https://www.techtarget.com/iotagenda/definition/MQTT-MQ-Telemetry-Transport +// Uncomment the line below if you wish to use Telnet connection +#define USE_TELNET + #ifdef USE_DHT float temp = 0.0; float hum = 0.0; @@ -132,9 +135,18 @@ void mqttReconnect() } #endif +#ifdef USE_TELNET +uint8_t i; +bool ConnectionEstablished; // Flag for successfully handled connection +#define MAX_TELNET_CLIENTS 2 +WiFiServer TelnetServer(23); +WiFiClient TelnetClient[MAX_TELNET_CLIENTS]; +#endif + namespace { // Change the part in brackets to your Duino-Coin username +// Change the part in brackets to your Duino-Coin username const char *DUCO_USER = "USERNAME"; // Change the part in brackets to your mining key (if you have enabled it in the wallet) const char *MINER_KEY = "MINING_KEY"; @@ -207,7 +219,7 @@ const char WEBSITE[] PROGMEM = R"=====(
- @@HASHRATE@@kH/s + @@HASHRATE@@ kH/s
Hashrate @@ -223,7 +235,7 @@ const char WEBSITE[] PROGMEM = R"=====(
- @@SHARES@@ + @@SHARES@@
Shares @@ -322,10 +334,10 @@ const char WEBSITE[] PROGMEM = R"=====(
+ )====="; +#ifdef USE_TELNET +void TelnetMsg(String text) +{ + for(i = 0; i < MAX_TELNET_CLIENTS; i++) + { + if (TelnetClient[i] || TelnetClient[i].connected()) + { + TelnetClient[i].println(text); + } + } + delay(10); // to avoid strange characters left in buffer +} + +// https://www.nikolaus-lueneburg.de/2017/09/wifi-telnet-server-auf-dem-esp8266/ +// English Google translation: +// https://www-nikolaus--lueneburg-de.translate.goog/2017/09/wifi-telnet-server-auf-dem-esp8266/?_x_tr_sl=de&_x_tr_tl=en&_x_tr_hl=de&_x_tr_pto=wapp +// Connect with Telnet client to read messages +// changes from original + +void Telnet() { + String readTelnet; + // Cleanup disconnected session + for(i = 0; i < MAX_TELNET_CLIENTS; i++) + { + if (TelnetClient[i] && !TelnetClient[i].connected()) + { + Serial.print("Client disconnected ... terminate session "); Serial.println(i+1); + TelnetClient[i].stop(); + } + } + + // Check new client connections + if (TelnetServer.hasClient()) + { + ConnectionEstablished = false; // Set to false + + for(i = 0; i < MAX_TELNET_CLIENTS; i++) + { + // Serial.print("Checking telnet session "); Serial.println(i+1); + + // find free socket + if (!TelnetClient[i]) + { + TelnetClient[i] = TelnetServer.available(); + + Serial.print("New Telnet client connected to session "); Serial.println(i+1); + + TelnetClient[i].flush(); // clear input buffer, else you get strange characters + TelnetClient[i].println("Welcome!"); + + TelnetClient[i].print("Seconds since start: "); + TelnetClient[i].println(millis()/1000); + + TelnetClient[i].print("Free Heap RAM: "); + TelnetClient[i].println(ESP.getFreeHeap()); + + TelnetClient[i].println("----------------------------------------------------------------"); + + ConnectionEstablished = true; + + break; + } + else + { + // Serial.println("Session is in use"); + } + } + + if (ConnectionEstablished == false) + { + Serial.println("No free sessions ... drop connection"); + TelnetServer.available().stop(); + // TelnetMsg("An other user cannot connect ... MAX_TELNET_CLIENTS limit is reached!"); + } + } + + for(i = 0; i < MAX_TELNET_CLIENTS; i++) + { + if (TelnetClient[i] && TelnetClient[i].connected()) + { + if(TelnetClient[i].available()) + { + //get data from the telnet client + while(TelnetClient[i].available()) + { + Serial.write(TelnetClient[i].read()); + } + } + } + } +} +#endif + ESP8266WebServer server(80); void hashupdater(){ //update hashrate every 3 sec in browser without reloading page @@ -348,6 +469,11 @@ void hashupdater(){ //update hashrate every 3 sec in browser without reloading p Serial.println("Update hashrate on page"); }; +void hashupdatershares(){ //update hashrate every 15 sec in browser without reloading page + server.send(200, "text/plain", String(share_count )); + Serial.println("Update shares on page"); +}; + void UpdateHostPort(String input) { // Thanks @ricaun for the code DynamicJsonDocument doc(256); @@ -516,6 +642,9 @@ void handleSystemEvents(void) { VerifyWifi(); ArduinoOTA.handle(); yield(); + #ifdef USE_TELNET + Telnet(); // Handle telnet connections + #endif } void waitForClientData(void) { @@ -541,7 +670,7 @@ void ConnectToServer() { while (!client.connect(host.c_str(), port)); waitForClientData(); - Serial.println("Connected to the server. Server version: " + client_buffer ); + Serial.println("Connected to the server. Server version: " + client_buffer); blink(BLINK_CLIENT_CONNECT); // Sucessfull connection with the server } @@ -647,7 +776,11 @@ void setup() { SetupWifi(); SetupOTA(); - + Serial.println("Starting Telnet server"); + #ifdef USE_TELNET + TelnetServer.begin(); + TelnetServer.setNoDelay(true); + #endif lwdtFeed(); lwdTimer.attach_ms(LWD_TIMEOUT, lwdtcb); if (USE_HIGHER_DIFF) START_DIFF = "ESP8266NH"; @@ -665,6 +798,7 @@ void setup() { + ")"); server.on("/", dashboard); if (WEB_HASH_UPDATER) server.on("/hashrateread", hashupdater); + if (WEB_HASH_UPDATER) server.on("/shareread", hashupdatershares); server.begin(); } @@ -724,8 +858,11 @@ void loop() { #endif waitForClientData(); - Serial.println("Received job with size of " + String(client_buffer)); + Serial.println("Received job with size of " + String(client_buffer)); + #ifdef USE_TELNET + TelnetMsg("Received job ... Mining now "); + #endif MiningJob job; job.parse((char*)client_buffer.c_str()); difficulty = job.difficulty; @@ -780,6 +917,17 @@ void loop() { + " kH/s (" + String(elapsed_time_s) + "s)"); + #ifdef USE_TELNET + TelnetMsg(client_buffer + + " share #" + + String(share_count) + + " (" + String(duco_numeric_result) + ")" + + " hashrate: " + + String(hashrate / 1000, 2) + + " kH/s (" + + String(elapsed_time_s) + + "s)"); + #endif break; } if (max_micros_elapsed(micros(), 500000)) {