I got this piece of code from here.
Basically the part below I have trouble understanding. The data returned by coinbase api have several data sets in it. I don't get how the program parses the Bitcoin price in dollars; having said that: how can I parse other data like the Bitcoin Euro price.
// We now create a URI for the request
String url = "/v1/bpi/currentprice.json";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1
" +
"Host: " + host + "
" +
"Connection: close
");
delay(100);
// Read all the lines of the reply from server and print them to Serial
String answer;
while (client.available()) {
String line = client.readStringUntil('
');
answer += line;
}
client.stop();
Serial.println();
Serial.println("closing connection");
// Process answer
Serial.println();
Serial.println("Answer: ");
Serial.println(answer);
// Convert to JSON
String jsonAnswer;
int jsonIndex;
for (int i = 0; i < answer.length(); i++) {
if (answer[i] == '{') {
jsonIndex = i;
break;
}
}
// Get JSON data
jsonAnswer = answer.substring(jsonIndex);
Serial.println();
Serial.println("JSON answer: ");
Serial.println(jsonAnswer);
jsonAnswer.trim();
// Get rate as float
int rateIndex = jsonAnswer.indexOf("rate_float");
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 18);
priceString.trim();
float price = priceString.toFloat();