Skip to content

Commit 14c0dcd

Browse files
committed
Add support few cryptocurrencies
1 parent 4de8189 commit 14c0dcd

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

api/api.ino

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
const char* ssid = "TP-Link_408";
77
const char* password = "84570550";
88

9-
#define API_URL "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1,2,3"
10-
#define API_KEY ""
9+
const int timeout = 300000; // timeout between next API query
10+
11+
const String API_URL = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest";
12+
const String CRYPTO = "?slug=bitcoin,ethereum,litecoin";
13+
#define API_KEY "2831fae4-a23b-4b17-85ee-cce0a4520df2"
1114

1215
void setup() {
1316
Serial.begin(115200);
@@ -26,7 +29,6 @@ void setup() {
2629

2730
void loop() {
2831
getData();
29-
delay(7650);
3032
}
3133

3234
void getData() {
@@ -39,7 +41,7 @@ void getData() {
3941

4042
HTTPClient http;
4143

42-
http.begin(*client, API_URL);
44+
http.begin(*client, API_URL + CRYPTO);
4345
http.addHeader("X-CMC_PRO_API_KEY", API_KEY);
4446
int httpCode = http.GET();
4547

@@ -52,16 +54,21 @@ void getData() {
5254
DeserializationError error = deserializeJson(doc, payload);
5355

5456
JsonObject data = doc["data"];
55-
JsonObject crypto = data["1"];
56-
String ticker = crypto["name"];
5757

58-
// get price
59-
JsonObject quote = crypto["quote"];
60-
JsonObject usd = quote["USD"];
61-
String price = usd["price"];
62-
63-
Serial.print(price);
58+
for (JsonPair key : data) {
59+
JsonObject crypto = data[key.key().c_str()];
60+
String ticker = crypto["name"];
61+
62+
// get price
63+
JsonObject quote = crypto["quote"];
64+
JsonObject usd = quote["USD"];
65+
String price = usd["price"];
66+
67+
Serial.print(ticker + ",$" + price);
68+
delay(timeout / data.size());
69+
}
6470
}
6571
http.end();
6672
}
73+
delay(timeout);
6774
}

main/main.ino

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ void setup() {
1111
esp8266.begin(115200);
1212

1313
lcd.init();
14-
lcd.backlight();
15-
lcd.print("Bitcoin");
16-
14+
lcd.backlight();
1715
}
1816

1917
String strData = "";
@@ -28,15 +26,39 @@ void loop() {
2826
}
2927

3028
if (recievedFlag) {
31-
lcd.setCursor(4, 1);
32-
lcd.print(strData);
33-
Serial.println(strData);
29+
printOnDisplay(strData);
3430
strData = "";
3531
recievedFlag = false;
36-
}
32+
}
3733

3834

3935
if (Serial.available()) {
4036
esp8266.write(Serial.read());
4137
}
4238
}
39+
40+
void printOnDisplay(String string) {
41+
// length (with one extra character for the null terminator)
42+
int str_len = string.length() + 1;
43+
44+
// prepare the character array (the buffer)
45+
char sz[str_len];
46+
47+
// copy it over
48+
string.toCharArray(sz, str_len);
49+
50+
char *saveptr;
51+
char *crypto, *price;
52+
53+
crypto = strtok_r(sz, ",", &saveptr);
54+
price = strtok_r(NULL, ",", &saveptr);
55+
56+
lcd.clear();
57+
lcd.setCursor(0, 0);
58+
lcd.print(crypto);
59+
lcd.setCursor(4, 1); // (16 - 8) / 2
60+
lcd.print(price);
61+
62+
Serial.println(crypto);
63+
Serial.println(price);
64+
}

0 commit comments

Comments
 (0)