HTTPS Get Request for Nano Price from Coingecko with ESP8266

Description: A Wemos Lolin D1 Mini Pro V2 connects to the internet, sends a GET Request to the Coingecko API for the Nano Price, and then updates the Nano price on an LCD display every 20 seconds.

HTTPS Get Request for Nano Price from Coingecko with ESP8266

Notes: CS (Chip-Select) is on pin 15. Make sure to set the baud rate to 115200 in the Serial Monitor.

Resources: https://arduinojson.org/v6/assistant/

Serial Monitor Nano Price with ESP8266

Supplies:

1 – Breadboard 400 Points
1 – Wemos Lolin D1 Mini Pro V2  >> Link <<
1 – SPI Colour Square LCD 240×240 (Pimoroni)  >> Link <<

Arduino Code:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

#include "DFRobot_GDL.h"
#define TFT_DC 16
#define TFT_CS 15
#define TFT_RST 5 // BL Pin on the LCD

DFRobot_ST7789_240x240_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);

void setup() {

  Serial.begin(115200);
  screen.begin();

  WiFi.mode(WIFI_STA);
  WiFi.begin("Network Name", "password"); // Change WiFi "Network Name" and "password" to yours
  Serial.println("");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println(F("\n\rWiFi connected!"));

}

void loop() {

  std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  client->setInsecure();
  HTTPClient https;
  https.begin(*client, "https://api.coingecko.com/api/v3/simple/price?ids=nano&vs_currencies=USD");

    //get json data
    int httpCode = https.GET();

    if (httpCode > 0) {  //Check the returning code
      
      const char* json = https.getString().c_str();
      
      // Stream& input;
      StaticJsonDocument<64> doc;
      DeserializationError error = deserializeJson(doc, json);

      if (error) {
       Serial.print(F("deserializeJson() failed: "));
       Serial.println(error.f_str());
        return;
        }

       float nano_usd = doc["nano"]["usd"]; // 6.46
       Serial.println("");
       Serial.print("$");
       Serial.print(nano_usd);
       Serial.print(" USD");

  screen.setTextWrap(true); //Set to text auto-wrap mode, true=Auto-wrap, false=No auto-wrap

  screen.fillScreen(COLOR_RGB565_WHITE);
  screen.setTextColor(COLOR_RGB565_BLACK);

  screen.setTextSize(1);
  screen.setFont(&FreeMono18pt7b);
  screen.setCursor(/*x=*/80,/*y=*/55);
  screen.print("NANO");

  screen.setTextSize(1);
  screen.setFont(&FreeMono9pt7b);
  screen.setCursor(/*x=*/34,/*y=*/78);
  screen.print("(Realtime Price)");

  screen.setTextSize(1);
  screen.setFont(&FreeMono18pt7b);
  screen.setCursor(/*x=*/65,/*y=*/135);
  screen.print("$");

  screen.setTextSize(1);
  screen.setFont(&FreeMono18pt7b);
  screen.setCursor(/*x=*/85,/*y=*/135);
  screen.print(nano_usd);

  screen.setTextSize(1);
  screen.setFont(&FreeMono18pt7b);
  screen.setCursor(/*x=*/87,/*y=*/170);
  screen.print("USD");
  
  screen.setCursor(/*x=*/49,/*y=*/205);
  screen.setFont(&FreeMono9pt7b);
  screen.setTextSize(1);
  screen.print("coingecko.com");
  delay(20000); // Wait 20s before next round to not get banned on API server

}
}

Leave a Comment

6 + 4 =