Skip to content

Commit 4de8189

Browse files
committed
Move to CoinMarketCap API
1 parent 45d12db commit 4de8189

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

api/api.ino

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

9-
#define API "https://api.coindesk.com/v1/bpi/currentprice.json"
9+
#define API_URL "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1,2,3"
10+
#define API_KEY ""
1011

1112
void setup() {
1213
Serial.begin(115200);
1314
delay(10);
1415
Serial.print("Connecting to ");
1516
Serial.println(ssid);
1617
WiFi.begin(ssid, password);
17-
1818
while (WiFi.status() != WL_CONNECTED) {
1919
delay(500);
2020
Serial.print(".");
2121
}
22-
2322
Serial.println("WiFi connected");
2423
Serial.print("IP address: ");
2524
Serial.println(WiFi.localIP());
@@ -40,23 +39,29 @@ void getData() {
4039

4140
HTTPClient http;
4241

43-
http.begin(*client, API);
42+
http.begin(*client, API_URL);
43+
http.addHeader("X-CMC_PRO_API_KEY", API_KEY);
4444
int httpCode = http.GET();
4545

4646
if (httpCode > 0) {
4747

4848
String payload = http.getString();
49+
4950
String json_str = String(payload);
50-
DynamicJsonDocument doc(1024);
51+
DynamicJsonDocument doc(4096);
5152
DeserializationError error = deserializeJson(doc, payload);
5253

53-
JsonObject bpi = doc["bpi"];
54-
JsonObject usd = bpi["USD"];
55-
String rate_float = usd["rate_float"];
54+
JsonObject data = doc["data"];
55+
JsonObject crypto = data["1"];
56+
String ticker = crypto["name"];
57+
58+
// get price
59+
JsonObject quote = crypto["quote"];
60+
JsonObject usd = quote["USD"];
61+
String price = usd["price"];
5662

57-
Serial.print(rate_float);
63+
Serial.print(price);
5864
}
59-
6065
http.end();
6166
}
6267
}

0 commit comments

Comments
 (0)