Skip to content

Commit 07c06be

Browse files
committed
Optimize ESP8266 miner
This commit changes how the sequence of strings representing consecutive integers is generated for the hasher. Instead of utilizing the Arduino String conversions, a Counter class is introduced, which can generate consecutive integers much faster. On my Wemos D1 mini this gives a hash rate increase from around 37 kH/s to around 53 kH/s, which is a 43% improvement.
1 parent e3df560 commit 07c06be

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

ESP8266_Code/ESP8266_Code.ino

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,49 @@ unsigned long lwdTimeOutMillis = LWD_TIMEOUT;
427427
#define BLINK_CLIENT_CONNECT 3
428428
#define BLINK_RESET_DEVICE 5
429429

430+
template <unsigned int max_digits>
431+
class Counter {
432+
public:
433+
Counter() { reset(); }
434+
435+
void reset() {
436+
memset(buffer, '0', max_digits);
437+
buffer[max_digits] = '\0';
438+
val = 0;
439+
len = 1;
440+
}
441+
442+
Counter & operator++() {
443+
inc_string(max_digits - 1);
444+
++val;
445+
return *this;
446+
}
447+
448+
operator unsigned int () const { return val; }
449+
const char * c_str() const { return buffer + max_digits - len; }
450+
size_t strlen() const { return len; }
451+
452+
protected:
453+
inline void inc_string(int pos) {
454+
if (pos < 0)
455+
return;
456+
457+
if (buffer[pos] < '9') {
458+
buffer[pos]++;
459+
} else {
460+
buffer[pos] = '0';
461+
inc_string(pos - 1);
462+
}
463+
464+
len = max(max_digits - pos, len);
465+
}
466+
467+
protected:
468+
char buffer[max_digits + 1];
469+
unsigned int val;
470+
size_t len;
471+
};
472+
430473
void SetupWifi() {
431474
Serial.println("Connecting to: " + String(SSID));
432475
WiFi.mode(WIFI_STA); // Setup ESP in client mode
@@ -518,6 +561,23 @@ void handleSystemEvents(void) {
518561
yield();
519562
}
520563

564+
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
565+
String getValue(String data, char separator, int index)
566+
{
567+
int found = 0;
568+
int strIndex[] = {0, -1};
569+
int max_index = data.length() - 1;
570+
571+
for (int i = 0; i <= max_index && found <= index; i++) {
572+
if (data.charAt(i) == separator || i == max_index) {
573+
found++;
574+
strIndex[0] = strIndex[1] + 1;
575+
strIndex[1] = (i == max_index) ? i + 1 : i;
576+
}
577+
}
578+
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
579+
}
580+
521581
void waitForClientData(void) {
522582
client_buffer = "";
523583

@@ -674,8 +734,7 @@ void setup() {
674734
void loop() {
675735
br_sha1_context sha1_ctx, sha1_ctx_base;
676736
uint8_t hashArray[20];
677-
String duco_numeric_result_str;
678-
737+
679738
// 1 minute watchdog
680739
lwdtFeed();
681740

@@ -742,22 +801,21 @@ void loop() {
742801

743802
String result = "";
744803
if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW);
745-
for (unsigned int duco_numeric_result = 0; duco_numeric_result < job.difficulty; duco_numeric_result++) {
804+
for (Counter<8> counter; counter < difficulty; ++counter) {
746805
// Difficulty loop
747806
sha1_ctx = sha1_ctx_base;
748-
duco_numeric_result_str = String(duco_numeric_result);
749807

750-
br_sha1_update(&sha1_ctx, duco_numeric_result_str.c_str(), duco_numeric_result_str.length());
808+
br_sha1_update(&sha1_ctx, counter.c_str(), counter.strlen());
751809
br_sha1_out(&sha1_ctx, hashArray);
752810

753811
if (memcmp(job.expected_hash, hashArray, 20) == 0) {
754812
// If result is found
755813
if (LED_BLINKING) digitalWrite(LED_BUILTIN, HIGH);
756814
unsigned long elapsed_time = micros() - start_time;
757815
float elapsed_time_s = elapsed_time * .000001f;
758-
hashrate = duco_numeric_result / elapsed_time_s;
816+
hashrate = counter / elapsed_time_s;
759817
share_count++;
760-
client.print(String(duco_numeric_result)
818+
client.print(String(counter)
761819
+ ","
762820
+ String(hashrate)
763821
+ ","
@@ -774,7 +832,7 @@ void loop() {
774832
Serial.println(client_buffer
775833
+ " share #"
776834
+ String(share_count)
777-
+ " (" + String(duco_numeric_result) + ")"
835+
+ " (" + String(counter) + ")"
778836
+ " hashrate: "
779837
+ String(hashrate / 1000, 2)
780838
+ " kH/s ("

0 commit comments

Comments
 (0)