Skip to content

ESP8266 miner ESP01 compatibility #1581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 110 additions & 68 deletions ESP8266_Code/ESP8266_Code.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@
//#include <TypeConversion.h>

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Ticker.h>
#include <ESP8266WebServer.h>

// Uncomment the line below if you wish to use a DHT sensor (Duino IoT beta)
// #define USE_DHT
Expand All @@ -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 <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#endif

#ifdef USE_OTA
#include <ArduinoOTA.h>
#endif

#ifdef USE_DHT
float temp = 0.0;
float hum = 0.0;
Expand Down Expand Up @@ -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
Expand All @@ -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. */
Expand All @@ -167,6 +187,7 @@ String AutoRigName = "";
String host = "";
String node_id = "";

#ifdef WEB_DASHBOARD
const char WEBSITE[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
Expand Down Expand Up @@ -324,7 +345,6 @@ const char WEBSITE[] PROGMEM = R"=====(
setInterval(function(){
getData();
}, 3000);

function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -451,6 +466,7 @@ void SetupWifi() {
UpdatePool();
}

#ifdef USE_OTA
void SetupOTA() {
// Prepare OTA handler
ArduinoOTA.onStart([]() {
Expand All @@ -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) {
Expand Down Expand Up @@ -514,7 +530,9 @@ void VerifyWifi() {

void handleSystemEvents(void) {
VerifyWifi();
#ifdef USE_OTA
ArduinoOTA.handle();
#endif
yield();
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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;
Expand All @@ -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;
Expand Down