Build a Bitcoin Ticker with ESP32 and Arduino

In this article, I am going to share how to build a Bitcoin ticker that displays the price of BTC/USD. I use an ESP32 microcontroller programmed in an Arduino environment. The Arduino code will read the Bitcoin price from the CoinDesk API, and it will display the price on the 128x64 OLED module.

Hardware

First, we need the following hardware in order to build this project:

  • ESP32 development board

  • 0.96" 128x64 OLED SSD1306 module

  • Jumper cable

Wiring Diagram

The OLED module uses an I2C bus. So, we just need two connections between the ESP32 and OLED module, which are pin 32 for SCL and pin 33 for SDA.

Arduino Code

In order to use the OLED module, we need the library Adafruit_SSD1306. We can install it from the Arduino library manager.

We need to set up several parts in the setup() function, as shown in the following list:

  • Serial communication for debugging. The code to set this up is in line 30.

  • I2C communication to communicate with the OLED module. The code to set this up is in line 33. In lines 8 and 9, we define the SDA and SCL pins.

  • OLED module. We instantiate the object in line 15, and then initialize it in lines 36 and 37. The I2C address for this module is 0x3C, as defined in line 12.

  • Wi-Fi connection to a Wi-Fi access point. In lines 40–50, we need to set up a Wi-Fi connection to an access point that has an internet connection. The access point name and password are defined in lines 18 and 19. You need to change this to yours.

  • HTTP client. We instantiate an HTTP client and declare requests and response variables in lines 22–25. We are going to use the currentURL to get the current price and the historyURL to get yesterday's price.

oled_ssd1306_bitcoin_ticker.ino
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

// Pin I2C
#define I2C_SDA 33
#define I2C_SCL 32

// Address I2C OLED
#define OLED_ADDR 0x3C

// Deklarasi object untuk OLED display
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

// SSID dan password Wi-Fi AP
const char ssid[] = "<your_wifi_name>";
const char pass[] = "<yuor_wifi_password>";

// Koneksi HTTP, request, dan response
HTTPClient http;
const String currentURL = "http://api.coindesk.com/v1/bpi/currentprice/BTC.json";
const String historyURL = "http://api.coindesk.com/v1/bpi/historical/close.json";
String response = "";

void setup()
{
  // Setup serial communication
  Serial.begin(115200);

  // Inisialisasi pin I2C
  Wire.begin(I2C_SDA, I2C_SCL, 400000);
  
  // Inisialisasi OLED
  oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  oled.clearDisplay();
  
  // Connect to a WiFi access point
  Serial.printf("Connecting to %s ...\n", ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  if (WiFi.waitForConnectResult() != WL_CONNECTED)
  {
    Serial.printf("WiFi connect failed! Rebooting...\n");
    delay(1000);
    ESP.restart();
  }
  Serial.printf("Connected\n");
  Serial.printf("IP address: %s\n", WiFi.localIP().toString().c_str());
}

void loop()
{
  // Request data hari ini
  http.begin(currentURL);
  http.GET();
  response = http.getString();
  http.end();

  // Parse JSON
  StaticJsonDocument<2000> doc;
  DeserializationError error = deserializeJson(doc, response);
  if (error)
  {
    Serial.printf("JSON parseObject() failed\n");
    return;
  }  

  // Ambil nilai time dan price hari ini
  String time = doc["time"]["updated"].as<String>();
  double price = doc["bpi"]["USD"]["rate_float"].as<double>();

  // Request data kemarin
  http.begin(historyURL);
  http.GET();
  response = http.getString();
  http.end();

  // Parse JSON
  StaticJsonDocument<2000> history_doc;
  DeserializationError history_error = deserializeJson(history_doc, response);
  if (history_error)
  {
    Serial.printf("JSON parseObject() failed\n");
    return;
  }  

  // Ambil nilai price kemarin
  JsonObject bpi_obj = history_doc["bpi"].as<JsonObject>();
  double yesterday_price;
  for (JsonPair kv : bpi_obj)
    yesterday_price = kv.value().as<double>();

  // Hitung persentase perubahan harga
  bool is_up = price > yesterday_price;
  double percent_change = is_up ? ((price-yesterday_price)/yesterday_price)*100 : ((yesterday_price-price)/yesterday_price)*100;
  String sign = is_up ? "+" : "";

  // Format text
  String txt0 = "BTC/USD " + sign + String(percent_change, 1) + "%";
  String txt1 = String(price, 2);
  String txt2 = "Last updated " + time.substring(13, 18);

  // Tampilkan hasil
  oled.clearDisplay();
  oled.setTextColor(WHITE);
  oled.setTextSize(1);
  print_center(txt0, 0, 0);
  oled.setTextSize(2);
  print_center(txt1, 0, 18);
  oled.setTextSize(1);
  print_center(txt2, 0, 50);
  oled.display();

  // Delay
  delay(1000);
}

// Fungsi untuk print text ke OLED dengan alignment center
void print_center(const String str, int x, int y)
{
  int16_t x1, y1;
  uint16_t w, h;
  oled.getTextBounds(str, x, y, &x1, &y1, &w, &h);
  oled.setCursor((x-w/2)+(128/2), y);
  oled.print(str);
}

After we initialize the code, we can build the main loop() function. This is how it works:

  • In lines 56–72, we make an HTTP request in order to get the current Bitcoin price.

  • In lines 75–93, we make an HTTP request in order to get the yesterday's Bitcoin price.

  • In lines 96–98, we do calculations to get the daily percentage change.

  • In lines 101–114, we display the Bitcoin information on the OLED module.

We use a print_center() function that is defined in lines 121–128 to print the text to the OLED module with center alignment.

Result

The following figure shows the result. On the OLED, there are daily percentage changes, the current price itself, and the last updated time.

References

Last updated