diff --git a/ESP8266_Code/ESP8266_Code.ino b/ESP8266_Code/ESP8266_Code.ino index 77a93c42..8b3efe33 100644 --- a/ESP8266_Code/ESP8266_Code.ino +++ b/ESP8266_Code/ESP8266_Code.ino @@ -38,13 +38,10 @@ //#include #include -#include #include -#include #include #include #include -#include // Uncomment the line below if you wish to use a DHT sensor (Duino IoT beta) // #define USE_DHT @@ -55,6 +52,31 @@ // If you don't know what MQTT means check this link: // https://www.techtarget.com/iotagenda/definition/MQTT-MQ-Telemetry-Transport +// Uncomment to disable the web dashboard +// #define WEB_DASHBOARD + +// Uncomment to disable the update hashrate in browser without reloading the page +// #define WEB_HASH_UPDATER + +// Uncomment to use the 160 MHz overclock mode, comment out to run at 80 MHz +#define USE_HIGHER_DIFF + +// Comment out to disable the onboard led blinking when finding shares +#define LED_BLINKING + +// Comment out to disable OTA upgrades +#define USE_OTA + + +#ifdef WEB_DASHBOARD +#include +#include +#endif + +#ifdef USE_OTA +#include +#endif + #ifdef USE_DHT float temp = 0.0; float hum = 0.0; @@ -132,6 +154,12 @@ void mqttReconnect() } #endif +#ifdef USE_HIGHER_DIFF +#define START_DIFF "ESP8266NH" +#else +#define START_DIFF "ESP8266N" +#endif + namespace { // Change the part in brackets to your Duino-Coin username @@ -144,14 +172,6 @@ const char *SSID = "WIFI_NAME"; const char *PASSWORD = "WIFI_PASSWORD"; // Change the part in brackets if you want to set a custom miner name (use Auto to autogenerate, None for no name) const char *RIG_IDENTIFIER = "None"; -// Set to true to use the 160 MHz overclock mode (and not get the first share rejected) -const bool USE_HIGHER_DIFF = true; -// Set to true if you want to host the dashboard page (available on ESPs IP address) -const bool WEB_DASHBOARD = false; -// Set to true if you want to update hashrate in browser without reloading the page -const bool WEB_HASH_UPDATER = false; -// Set to false if you want to disable the onboard led blinking when finding shares -const bool LED_BLINKING = true; /* Do not change the lines below. These lines are static and dynamic variables that will be used by the program for counters and measurements. */ @@ -167,6 +187,7 @@ String AutoRigName = ""; String host = ""; String node_id = ""; +#ifdef WEB_DASHBOARD const char WEBSITE[] PROGMEM = R"=====( @@ -324,7 +345,6 @@ const char WEBSITE[] PROGMEM = R"=====( setInterval(function(){ getData(); }, 3000); - function getData() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { @@ -342,11 +362,7 @@ const char WEBSITE[] PROGMEM = R"=====( )====="; ESP8266WebServer server(80); - -void hashupdater(){ //update hashrate every 3 sec in browser without reloading page - server.send(200, "text/plain", String(hashrate / 1000)); - Serial.println("Update hashrate on page"); -}; +#endif void UpdateHostPort(String input) { // Thanks @ricaun for the code @@ -408,7 +424,6 @@ void UpdatePool() { WiFiClient client; String client_buffer = ""; String chipID = ""; -String START_DIFF = ""; // Loop WDT... please don't feed me... // See lwdtcb() and lwdtFeed() below @@ -451,6 +466,7 @@ void SetupWifi() { UpdatePool(); } +#ifdef USE_OTA void SetupOTA() { // Prepare OTA handler ArduinoOTA.onStart([]() { @@ -474,18 +490,18 @@ void SetupOTA() { ArduinoOTA.setHostname(RIG_IDENTIFIER); // Give port a name not just address ArduinoOTA.begin(); } +#endif void blink(uint8_t count, uint8_t pin = LED_BUILTIN) { - if (LED_BLINKING){ +#ifdef LED_BLINKING uint8_t state = HIGH; - for (int x = 0; x < (count << 1); ++x) { - digitalWrite(pin, state ^= HIGH); - delay(50); + digitalWrite(pin, state ^= HIGH); + delay(50); } - } else { +#else digitalWrite(LED_BUILTIN, HIGH); - } +#endif } void RestartESP(String msg) { @@ -514,7 +530,9 @@ void VerifyWifi() { void handleSystemEvents(void) { VerifyWifi(); +#ifdef USE_OTA ArduinoOTA.handle(); +#endif yield(); } @@ -559,7 +577,8 @@ void ConnectToServer() { waitForClientData(); Serial.println("Connected to the server. Server version: " + client_buffer ); - blink(BLINK_CLIENT_CONNECT); // Sucessfull connection with the server + + blink(BLINK_CLIENT_CONNECT); // Successfull connection with the server } bool max_micros_elapsed(unsigned long current, unsigned long max_elapsed) { @@ -572,28 +591,6 @@ bool max_micros_elapsed(unsigned long current, unsigned long max_elapsed) { return false; } -void dashboard() { - Serial.println("Handling HTTP client"); - - String s = WEBSITE; - s.replace("@@IP_ADDR@@", WiFi.localIP().toString()); - - s.replace("@@HASHRATE@@", String(hashrate / 1000)); - s.replace("@@DIFF@@", String(difficulty / 100)); - s.replace("@@SHARES@@", String(share_count)); - s.replace("@@NODE@@", String(node_id)); - - s.replace("@@DEVICE@@", String(DEVICE)); - s.replace("@@ID@@", String(RIG_IDENTIFIER)); - s.replace("@@MEMORY@@", String(ESP.getFreeHeap())); - s.replace("@@VERSION@@", String(MINER_VER)); -#ifdef USE_DHT - s.replace("@@TEMP@@", String(temp)); - s.replace("@@HUM@@", String(hum)); -#endif - server.send(200, "text/html", s); -} - } // namespace // https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TypeConversion.cpp @@ -636,28 +633,59 @@ void setup() { } SetupWifi(); + +#ifdef USE_OTA SetupOTA(); +#endif lwdtFeed(); lwdTimer.attach_ms(LWD_TIMEOUT, lwdtcb); - if (USE_HIGHER_DIFF) START_DIFF = "ESP8266NH"; - else START_DIFF = "ESP8266N"; - if(WEB_DASHBOARD) { - if (!MDNS.begin(RIG_IDENTIFIER)) { +#ifdef WEB_DASHBOARD + if (!MDNS.begin(RIG_IDENTIFIER)) { Serial.println("mDNS unavailable"); - } - MDNS.addService("http", "tcp", 80); - Serial.print("Configured mDNS for dashboard on http://" - + String(RIG_IDENTIFIER) - + ".local (or http://" - + WiFi.localIP().toString() - + ")"); - server.on("/", dashboard); - if (WEB_HASH_UPDATER) server.on("/hashrateread", hashupdater); - server.begin(); + } else { + MDNS.addService("http", "tcp", 80); + Serial.print("Configured mDNS for dashboard on http://" + + String(RIG_IDENTIFIER) + + ".local (or http://" + + WiFi.localIP().toString() + + ")"); } + server.on("/", [] { + Serial.println("Handling HTTP client"); + + String s = WEBSITE; + s.replace("@@IP_ADDR@@", WiFi.localIP().toString()); + + s.replace("@@HASHRATE@@", String(hashrate / 1000)); + s.replace("@@DIFF@@", String(difficulty / 100)); + s.replace("@@SHARES@@", String(share_count)); + s.replace("@@NODE@@", String(node_id)); + + s.replace("@@DEVICE@@", String(DEVICE)); + s.replace("@@ID@@", String(RIG_IDENTIFIER)); + s.replace("@@MEMORY@@", String(ESP.getFreeHeap())); + s.replace("@@VERSION@@", String(MINER_VER)); +#ifdef USE_DHT + s.replace("@@TEMP@@", String(temp)); + s.replace("@@HUM@@", String(hum)); +#endif + server.send(200, "text/html", s); + }); + +#ifdef WEB_HASH_UPDATER + server.on("/hashrateread", [] { + //update hashrate every 3 sec in browser without reloading page + server.send(200, "text/plain", String(hashrate / 1000)); + Serial.println("Update hashrate on page"); + }); +#endif + + server.begin(); +#endif + blink(BLINK_SETUP_COMPLETE); } @@ -671,18 +699,24 @@ void loop() { // OTA handlers VerifyWifi(); + +#ifdef USE_OTA ArduinoOTA.handle(); - if(WEB_DASHBOARD) server.handleClient(); +#endif + +#ifdef WEB_DASHBOARD + server.handleClient(); +#endif ConnectToServer(); Serial.println("Asking for a new job for user: " + String(DUCO_USER)); - #ifndef USE_DHT - client.print("JOB," + +#ifndef USE_DHT + client.print("JOB," + String(DUCO_USER) + SEP_TOKEN + String(START_DIFF) + SEP_TOKEN + String(MINER_KEY) + END_TOKEN); - #endif +#endif #ifdef USE_DHT temp = dht.readTemperature(); @@ -718,7 +752,9 @@ void loop() { String expected_hash_str = getValue(client_buffer, SEP_TOKEN, 1); difficulty = getValue(client_buffer, SEP_TOKEN, 2).toInt() * 100 + 1; - if (USE_HIGHER_DIFF) system_update_cpu_freq(160); +#ifdef USE_HIGHER_DIFF + system_update_cpu_freq(160); +#endif int job_len = last_block_hash.length() + expected_hash_str.length() + String(difficulty).length(); @@ -734,7 +770,11 @@ void loop() { max_micros_elapsed(start_time, 0); String result = ""; - if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW); + +#ifdef LED_BLINKING + digitalWrite(LED_BUILTIN, LOW); +#endif + for (unsigned int duco_numeric_result = 0; duco_numeric_result < difficulty; duco_numeric_result++) { // Difficulty loop sha1_ctx = sha1_ctx_base; @@ -745,7 +785,9 @@ void loop() { if (memcmp(expected_hash, hashArray, 20) == 0) { // If result is found - if (LED_BLINKING) digitalWrite(LED_BUILTIN, HIGH); +#ifdef LED_BLINKING + digitalWrite(LED_BUILTIN, HIGH); +#endif unsigned long elapsed_time = micros() - start_time; float elapsed_time_s = elapsed_time * .000001f; hashrate = duco_numeric_result / elapsed_time_s;