Skip to content

Commit f262968

Browse files
authored
Built-in TypeConversion
1 parent 79b1e5d commit f262968

File tree

1 file changed

+94
-80
lines changed

1 file changed

+94
-80
lines changed

ESP8266_Code/ESP8266_Code.ino

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
/* If during compilation the line below causes a
2323
"fatal error: arduinoJson.h: No such file or directory"
2424
message to occur; it means that you do NOT have the
25-
ArduinoJSON library installed. To install it,
26-
go to the below link and follow the instructions:
25+
ArduinoJSON library installed. To install it,
26+
go to the below link and follow the instructions:
2727
https://github.com/revoxhere/duino-coin/issues/832 */
2828
#include <ArduinoJson.h>
2929

@@ -35,7 +35,7 @@
3535
follow the instructions of the readme file:
3636
https://github.com/esp8266/Arduino */
3737
#include <bearssl/bearssl.h>
38-
#include <TypeConversion.h>
38+
//#include <TypeConversion.h>
3939

4040
#include <ESP8266WiFi.h>
4141
#include <ESP8266mDNS.h>
@@ -56,102 +56,102 @@
5656
// https://www.techtarget.com/iotagenda/definition/MQTT-MQ-Telemetry-Transport
5757

5858
#ifdef USE_DHT
59-
float temp = 0.0;
60-
float hum = 0.0;
59+
float temp = 0.0;
60+
float hum = 0.0;
6161

62-
// Install "DHT sensor library" if you get an error
63-
#include <DHT.h>
64-
// Change D3 to the pin you've connected your sensor to
65-
#define DHTPIN D3
66-
// Set DHT11 or DHT22 accordingly
67-
#define DHTTYPE DHT11
62+
// Install "DHT sensor library" if you get an error
63+
#include <DHT.h>
64+
// Change D3 to the pin you've connected your sensor to
65+
#define DHTPIN D3
66+
// Set DHT11 or DHT22 accordingly
67+
#define DHTTYPE DHT11
6868

69-
DHT dht(DHTPIN, DHTTYPE);
69+
DHT dht(DHTPIN, DHTTYPE);
7070
#endif
7171

7272
#ifdef USE_MQTT
73-
// Install "PubSubClient" if you get an error
74-
#include <PubSubClient.h>
73+
// Install "PubSubClient" if you get an error
74+
#include <PubSubClient.h>
7575

76-
long lastMsg = 0;
76+
long lastMsg = 0;
7777

78-
// Change the part in brackets to your MQTT broker address
79-
#define mqtt_server "broker.hivemq.com"
80-
// broker.hivemq.com is for testing purposes, change it to your broker address
78+
// Change the part in brackets to your MQTT broker address
79+
#define mqtt_server "broker.hivemq.com"
80+
// broker.hivemq.com is for testing purposes, change it to your broker address
8181

82-
// Change this to your MQTT broker port
83-
#define mqtt_port 1883
84-
// If you want to use user and password for your MQTT broker, uncomment the line below
85-
// #define mqtt_use_credentials
82+
// Change this to your MQTT broker port
83+
#define mqtt_port 1883
84+
// If you want to use user and password for your MQTT broker, uncomment the line below
85+
// #define mqtt_use_credentials
8686

87-
// Change the part in brackets to your MQTT broker username
88-
#define mqtt_user "My cool mqtt username"
89-
// Change the part in brackets to your MQTT broker password
90-
#define mqtt_password "My secret mqtt pass"
87+
// Change the part in brackets to your MQTT broker username
88+
#define mqtt_user "My cool mqtt username"
89+
// Change the part in brackets to your MQTT broker password
90+
#define mqtt_password "My secret mqtt pass"
9191

92-
// Change this if you want to send data to the topic every X milliseconds
93-
#define mqtt_update_time 5000
92+
// Change this if you want to send data to the topic every X milliseconds
93+
#define mqtt_update_time 5000
9494

95-
// Change the part in brackets to your MQTT humidity topic
96-
#define humidity_topic "sensor/humidity"
97-
// Change the part in brackets to your MQTT temperature topic
98-
#define temperature_topic "sensor/temperature"
95+
// Change the part in brackets to your MQTT humidity topic
96+
#define humidity_topic "sensor/humidity"
97+
// Change the part in brackets to your MQTT temperature topic
98+
#define temperature_topic "sensor/temperature"
9999

100-
WiFiClient espClient;
101-
PubSubClient mqttClient(espClient);
100+
WiFiClient espClient;
101+
PubSubClient mqttClient(espClient);
102102

103-
void mqttReconnect()
103+
void mqttReconnect()
104+
{
105+
// Loop until we're reconnected
106+
while (!mqttClient.connected())
104107
{
105-
// Loop until we're reconnected
106-
while (!mqttClient.connected())
108+
Serial.print("Attempting MQTT connection...");
109+
110+
// Create a random client ID
111+
String clientId = "ESP8266Client-";
112+
clientId += String(random(0xffff), HEX);
113+
114+
// Attempt to connect
115+
#ifdef mqtt_use_credentials
116+
if (mqttClient.connect("ESP8266Client", mqtt_user, mqtt_password))
117+
#else
118+
if (mqttClient.connect(clientId.c_str()))
119+
#endif
120+
{
121+
Serial.println("connected");
122+
}
123+
else
107124
{
108-
Serial.print("Attempting MQTT connection...");
109-
110-
// Create a random client ID
111-
String clientId = "ESP8266Client-";
112-
clientId += String(random(0xffff), HEX);
113-
114-
// Attempt to connect
115-
#ifdef mqtt_use_credentials
116-
if (mqttClient.connect("ESP8266Client", mqtt_user, mqtt_password))
117-
#else
118-
if (mqttClient.connect(clientId.c_str()))
119-
#endif
120-
{
121-
Serial.println("connected");
122-
}
123-
else
124-
{
125-
Serial.print("failed, rc=");
126-
Serial.print(mqttClient.state());
127-
Serial.println(" try again in 5 seconds");
128-
// Wait 5 seconds before retrying
129-
delay(5000);
130-
}
125+
Serial.print("failed, rc=");
126+
Serial.print(mqttClient.state());
127+
Serial.println(" try again in 5 seconds");
128+
// Wait 5 seconds before retrying
129+
delay(5000);
131130
}
132131
}
132+
}
133133
#endif
134134

135135
namespace
136136
{
137-
// Change the part in brackets to your Duino-Coin username
138-
const char *DUCO_USER = "USERNAME";
139-
// Change the part in brackets to your mining key (if you have enabled it in the wallet)
140-
const char *MINER_KEY = "MINING_KEY";
141-
// Change the part in brackets to your WiFi name
142-
const char *SSID = "WIFI_NAME";
143-
// Change the part in brackets to your WiFi password
144-
const char *PASSWORD = "WIFI_PASSWORD";
145-
// Change the part in brackets if you want to set a custom miner name (use Auto to autogenerate, None for no name)
146-
const char *RIG_IDENTIFIER = "None";
147-
// Set to true to use the 160 MHz overclock mode (and not get the first share rejected)
148-
const bool USE_HIGHER_DIFF = true;
149-
// Set to true if you want to host the dashboard page (available on ESPs IP address)
150-
const bool WEB_DASHBOARD = false;
151-
// Set to true if you want to update hashrate in browser without reloading the page
152-
const bool WEB_HASH_UPDATER = false;
153-
// Set to false if you want to disable the onboard led blinking when finding shares
154-
const bool LED_BLINKING = true;
137+
// Change the part in brackets to your Duino-Coin username
138+
const char *DUCO_USER = "USERNAME";
139+
// Change the part in brackets to your mining key (if you have enabled it in the wallet)
140+
const char *MINER_KEY = "MINING_KEY";
141+
// Change the part in brackets to your WiFi name
142+
const char *SSID = "WIFI_NAME";
143+
// Change the part in brackets to your WiFi password
144+
const char *PASSWORD = "WIFI_PASSWORD";
145+
// Change the part in brackets if you want to set a custom miner name (use Auto to autogenerate, None for no name)
146+
const char *RIG_IDENTIFIER = "None";
147+
// Set to true to use the 160 MHz overclock mode (and not get the first share rejected)
148+
const bool USE_HIGHER_DIFF = true;
149+
// Set to true if you want to host the dashboard page (available on ESPs IP address)
150+
const bool WEB_DASHBOARD = false;
151+
// Set to true if you want to update hashrate in browser without reloading the page
152+
const bool WEB_HASH_UPDATER = false;
153+
// Set to false if you want to disable the onboard led blinking when finding shares
154+
const bool LED_BLINKING = true;
155155

156156
/* Do not change the lines below. These lines are static and dynamic variables
157157
that will be used by the program for counters and measurements. */
@@ -596,6 +596,20 @@ void dashboard() {
596596

597597
} // namespace
598598

599+
// https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TypeConversion.cpp
600+
const char base36Chars[36] PROGMEM = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
601+
const uint8_t base36CharValues[75] PROGMEM {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, // 0 to 9
602+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0, 0, 0, 0, 0, 0, // Upper case letters
603+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 // Lower case letters
604+
};
605+
uint8_t *hexStringToUint8Array(const String &hexString, uint8_t *uint8Array, const uint32_t arrayLength) {
606+
assert(hexString.length() >= arrayLength * 2);
607+
for (uint32_t i = 0; i < arrayLength; ++i) {
608+
uint8Array[i] = (pgm_read_byte(base36CharValues + hexString.charAt(i * 2) - '0') << 4) + pgm_read_byte(base36CharValues + hexString.charAt(i * 2 + 1) - '0');
609+
}
610+
return uint8Array;
611+
}
612+
599613
void setup() {
600614
Serial.begin(500000);
601615
Serial.println("\nDuino-Coin " + String(MINER_VER));
@@ -709,9 +723,9 @@ void loop() {
709723
int job_len = last_block_hash.length() + expected_hash_str.length() + String(difficulty).length();
710724

711725
Serial.println("Received job with size of " + String(job_len) + " bytes: " + last_block_hash + " " + expected_hash_str + " " + difficulty);
712-
726+
713727
uint8_t expected_hash[20];
714-
experimental::TypeConversion::hexStringToUint8Array(expected_hash_str, expected_hash, 20);
728+
hexStringToUint8Array(expected_hash_str, expected_hash, 20);
715729

716730
br_sha1_init(&sha1_ctx_base);
717731
br_sha1_update(&sha1_ctx_base, last_block_hash.c_str(), last_block_hash.length());

0 commit comments

Comments
 (0)