📈
Ween's Lab
UdemyYouTubeTikTok
  • Welcome
  • 📻FPGA Tutorials
    • FPGA Boards: Getting Started
      • Getting Started with PYNQ on Kria KV260 Vision AI Starter Kit
      • Getting Started with PYNQ on Red Pitaya STEMlab 125-14
      • Getting Started with PYNQ on ZYBO
    • FPGA Ethernet Tutorial
      • FPGA Tutorial Ethernet 1: Simple TCP Server
    • PYNQ FPGA Tutorial 101
      • Part 0: Introduction
      • Part 1: GPIO
      • Part 2: Custom IP
      • Part 3: Memory
      • Part 4: ANN Processor
    • PYNQ FPGA Tutorial 102
      • Part 0: Introduction
      • Part 1: Memory Mapped
      • Part 2: Direct Memory Access
      • Part 3: AXI-Lite Multiplier
      • Part 4: AXI-Stream Multiplier with DMA
      • Part 5: AXI-Lite GCD
      • Part 6: AXI-Stream GCD with DMA
      • Part 7: Access to DDR from PL
    • ZYNQ FPGA Tutorial
      • Part 1: Gate-Level Combinational Circuit
      • Part 2: RT-Level Combinational Circuit
      • Part 3: Regular Sequential Circuit
      • Part 4: FSM Sequential Circuit
      • Part 5: ZYNQ Architecture
      • Part 6: ARM CPU and FPGA Module
      • Part 7: FPGA Memory
      • Part 8: Hardware Accelerator for Neural Networks
    • ZYNQ FPGA Linux Kernel Module
      • Cross Compiling Kernel, Kernel Module, and User Program for PYNQ
      • Configure PL to PS Interrupt in Kernel Module
      • Configure AXI DMA in Kernel Module
  • 📟Proyek Arduino
    • Kumpulan Proyek
      • Rangkaian LED
      • LED Berkedip Nyala Api
      • LED Chaser
      • LED Binary Counter
      • OLED 128x4 Bitcoin Ticker
      • Rangkaian Button
      • Button Multifungsi
      • Button Interrupt
      • Button Debouncing
    • Pelatihan Mikrokontroler Arduino ESP32
      • Bab 1 Pengenalan Mikrokontroler
      • Bab 2 Pengenalan Arduino
      • Bab 3 Pengenalan Bahasa C
      • Bab 4 Digital Output
      • Bab 5 Digital Input
      • Bab 6 Serial Communication
      • Bab 7 Analog-to-Digital Conversion
      • Bab 8 Interrupt
      • Bab 9 Timer
      • Bab 10 Pulse-Width Modulation
      • Bab 11 SPI Communication
      • Bab 12 I2C Communication
  • 💰Finance
    • Coding for Finance
      • Build a Bitcoin Price Alert with Google Cloud and Telegram
      • Build a Bitcoin Ticker with ESP32 and Arduino
      • Stock Price Forecasting with LSTM
    • Trading dan Investasi
      • Istilah Ekonomi, Keuangan, Bisnis, Trading, dan Investasi
      • Jalan Menuju Financial Abundance
      • Memahami Korelasi Emas, Oil, Dollar, BTC, Bonds, dan Saham
      • Mindset Trading dan Investasi
      • Rangkuman Buku: Rahasia Analisis Fundamental Saham
      • Rangkuman Buku: The Psychology of Money
      • Rangkuman Kuliah: Introduction to Adaptive Markets
      • Rumus Menjadi Orang Kaya
  • 📝Life
    • Life Quotes
Powered by GitBook
On this page
  • Hardware
  • Wiring Diagram
  • Arduino Code
  • Result
  • References
  1. Finance
  2. Coding for Finance

Build a Bitcoin Ticker with ESP32 and Arduino

PreviousBuild a Bitcoin Price Alert with Google Cloud and TelegramNextStock Price Forecasting with LSTM

Last updated 11 months ago

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

Bitcoin Ticker,

CoinDesk,

💰
https://github.com/AndrewBudziszek/bitcoin-ticker-esp32
https://cryptocointracker.com/api/coindesk
proyek_arduino/oled/oled_ssd1306_bitcoin_ticker/oled_ssd1306_bitcoin_ticker.ino at main · weenslab/proyek_arduinoGitHub
Logo