66const char * ssid = " TP-Link_408" ;
77const 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
1215void setup () {
1316 Serial.begin (115200 );
@@ -26,7 +29,6 @@ void setup() {
2629
2730void loop () {
2831 getData ();
29- delay (7650 );
3032}
3133
3234void 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}
0 commit comments