-
Notifications
You must be signed in to change notification settings - Fork 2
/
coinmarketcap.ino
66 lines (54 loc) · 2.35 KB
/
coinmarketcap.ino
1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2019 Bonsai Software, Inc. All Rights Reserved.
String cmc_host = "pro-api.coinmarketcap.com";
int cmc_port = 443;
String cmc_url = "/v1/cryptocurrency/quotes/latest";
void cmc_rate() {
// Loop until we succeed
while (true) {
Serial.printf("cmc_rate updating BTC%s\n", cfg_cmc_currency.c_str());
displayText(10, 100, "CoinMarketCap BTC" + cfg_cmc_currency + " ...");
WiFiClientSecure client;
while (!client.connect(cmc_host.c_str(), cmc_port)) {
Serial.printf("cmc_rate connect failed\n");
setupNetwork();
}
if (cfg_cmc_fingerprint.length() > 0 &&
!client.verify(cfg_cmc_fingerprint.c_str(), NULL)) {
Serial.printf("cmc_rate verify failed\n");
continue;
}
String args = "?convert=" + cfg_cmc_currency + "&symbol=BTC";
client.print(String("GET ") + cmc_url + args + " HTTP/1.1\r\n" +
"Host: " + cmc_host + "\r\n" +
"User-Agent: ESP32\r\n" +
"X-CMC_PRO_API_KEY: " + cfg_cmc_apikey + "\r\n" +
"Accept: application/json\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
// Discard the length of the returned payload
String line = client.readStringUntil('\n');
// Read the rest of the payload
line = client.readString();
const size_t capacity =
JSON_ARRAY_SIZE(1) + 2*JSON_OBJECT_SIZE(1) +
JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(5) +
JSON_OBJECT_SIZE(7) + JSON_OBJECT_SIZE(14) + 563 + 2048;
DynamicJsonDocument doc(capacity);
DeserializationError retval = deserializeJson(doc, line);
if (retval == DeserializationError::NoMemory) {
continue; // retry
} else if (retval != DeserializationError::Ok) {
Serial.printf("deserializeJson failed: %s\n", retval.c_str());
continue; // retry
}
String temp = doc["data"]["BTC"]["quote"][cfg_cmc_currency]["price"];
g_rate = temp.toDouble();
Serial.printf("1 BTC = %f %s\n", g_rate, cfg_cmc_currency.c_str());
return;
}
}