Skip to content

Instantly share code, notes, and snippets.

@hector6872
Last active May 16, 2020 19:58
Show Gist options
  • Save hector6872/b06097fe38e7d1e0ef1af3d3bdc37a38 to your computer and use it in GitHub Desktop.
Save hector6872/b06097fe38e7d1e0ef1af3d3bdc37a38 to your computer and use it in GitHub Desktop.
api-request Arduino sketch
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
const char* SSID = "SSID";
const char* PASSWORD = "PASSWORD";
const String API_KEY = "API_KEY";
const char* SYMBOL = "BTC";
const char* CONVERT_TO = "USD";
const char* HOST = "pro-api.coinmarketcap.com";
const int HTTPS_PORT = 443;
const long DEFAULT_DELAY = 60 * 1000 * 5; // wait 5 minutes between API request; +info https://pro.coinmarketcap.com/account/plan
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
delay(100);
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
}
void loop() {
if (!client.connect(HOST, HTTPS_PORT)) {
Serial.println("Connection failed!");
delay();
return;
}
String endpoint = "/v1/cryptocurrency/quotes/latest?CMC_PRO_API_KEY=" + API_KEY + "&symbol=" + SYMBOL + "&convert=" + CONVERT_TO;
Serial.print("Requesting: ");
Serial.println(endpoint);
client.print(String("GET ") + endpoint + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
if (client.println() == 0) {
Serial.println("Failed to send request");
delay();
return;
}
// check HTTP response
char httpStatus[32] = {0};
client.readBytesUntil('\r', httpStatus, sizeof(httpStatus));
if (strcmp(httpStatus, "HTTP/1.1 200 OK") != 0) {
Serial.print("Unexpected response: ");
Serial.println(httpStatus);
delay();
return;
}
// skip HTTP headers
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
if (line == "\r") {
break;
}
}
// skip content length
if (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
// get response
String response = "";
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
line.trim();
if (line != "\r") {
response += line;
}
}
client.stop();
// parse response
DynamicJsonDocument jsonDocument;
DeserializationError error = deserializeJson(jsonDocument, response);
if (error) {
Serial.println("Deserialization failed");
delay();
return;
}
JsonObject root = jsonDocument.as<JsonObject>();
// check API status
JsonObject status = root["status"];
int statusErrorCode = status["error_code"];
if (statusErrorCode != 0) {
String statusErrorMessage = status["error_message"];
Serial.print("Error: ");
Serial.println(statusErrorMessage);
delay();
}
JsonObject coin = root["data"][SYMBOL];
String name = coin["name"];
String lastUpdated = coin["last_updated"];
Serial.println("Name: " + name);
Serial.println("Last updated: " + lastUpdated);
JsonObject quote = coin["quote"][CONVERT_TO];
float price = quote["price"];
Serial.print("Price: $");
Serial.println(price, 2);
delay();
}
void delay() {
Serial.println("");
Serial.println("Keep calm and hodl!");
delay(DEFAULT_DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment