Devices & Components
Breadboard - 830 contacts
OLED Display
Jumper wires (generic)
ESP32
Software & Tools
Arduino IDE
Project description
Code
arduino-main.ino
cpp
Track Bitcoin prices automatically with an ESP32.
1//============================================================================= 2// Track Bitcoin prices automatically with an ESP32 and an OLED display 3// Created on 19 August 2024 4// Created by Lucas Fernando (https://www.youtube.com/@lucasfernandochannel) 5// You are free to use this code the way you want 6//============================================================================= 7 8#include <WiFi.h> 9#include <HTTPClient.h> 10#include <ArduinoJson.h> 11#include <Adafruit_GFX.h> 12#include <Adafruit_ST7789.h> 13#include <SPI.h> 14 15// Replace with your network credentials 16const char* ssid = ""; 17const char* password = ""; 18 19// Replace with your CoinAPI key 20const char* api_key = ""; 21const char* api_url = "https://rest.coinapi.io/v1/exchangerate/BTC/USD"; 22 23// Define the waiting time between the API calls 24const int waiting_time = 900000; // Wait for 15 minutes 25 26// Define the pins 27#define TFT_CS 5 28#define TFT_RST 4 29#define TFT_DC 2 30 31// Initialize the ST7789 display 32Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); 33 34void setup() { 35 // Initialize serial communication 36 Serial.begin(115200); 37 38 // Initialize the display 39 tft.init(135, 240); 40 tft.setRotation(3); 41 tft.fillScreen(ST77XX_BLACK); 42 43 // Connect to Wi-Fi 44 connectToWiFi(); 45 46 // Make API call and display the result 47 displayBitcoinPrice(); 48} 49 50void loop() { 51 delay(waiting_time); 52 displayBitcoinPrice(); 53} 54 55void connectToWiFi() { 56 tft.setCursor(10, 10); 57 tft.setTextColor(ST77XX_WHITE); 58 tft.setTextSize(2); 59 tft.print("Connecting to Wi-Fi"); 60 61 WiFi.begin(ssid, password); 62 63 while (WiFi.status() != WL_CONNECTED) { 64 delay(1000); 65 Serial.println("Connecting..."); 66 } 67 68 Serial.println("Connected to Wi-Fi"); 69 tft.fillScreen(ST77XX_BLACK); 70} 71 72String formatTime(String time) { 73 // Extract date and time components 74 String date = time.substring(0, 10); 75 String timeOfDay = time.substring(11, 16); 76 77 // Convert date components 78 String year = date.substring(0, 4); // Year 79 String month = date.substring(5, 7); // Month 80 String day = date.substring(8, 10); // Day 81 82 // Convert the month number to the month name 83 String monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 84 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 85 int monthIndex = month.toInt() - 1; 86 String monthName = monthNames[monthIndex]; 87 88 // Convert time from 24-hour to 12-hour format 89 int hour = timeOfDay.substring(0, 2).toInt(); 90 String minute = timeOfDay.substring(3, 5); // Minute 91 String period = "AM"; 92 93 if (hour >= 12) { 94 period = "PM"; 95 if (hour > 12) hour -= 12; 96 } else if (hour == 0) { 97 hour = 12; // Midnight case 98 } 99 100 String formattedTime = monthName + " " + day + ", " + year + ", "; 101 formattedTime += String(hour) + ":" + minute + " " + period + " UTC"; 102 103 return formattedTime; 104} 105 106 107void displayBitcoinPrice() { 108 if (WiFi.status() == WL_CONNECTED) { 109 HTTPClient http; 110 http.begin(api_url); 111 http.addHeader("X-CoinAPI-Key", api_key); 112 113 int httpCode = http.GET(); // Make the request 114 115 if (httpCode > 0) { 116 String payload = http.getString(); 117 Serial.println(payload); 118 119 DynamicJsonDocument doc(1024); 120 deserializeJson(doc, payload); 121 122 float price = doc["rate"]; 123 String date = doc["time"]; 124 125 // Display the price on the OLED 126 tft.fillScreen(ST77XX_BLACK); 127 tft.setTextColor(ST77XX_WHITE); 128 tft.setCursor(0, 0); 129 tft.setTextSize(3); 130 tft.print("Bitcoin Price"); 131 tft.setCursor(0, 30); 132 tft.print("USD "); 133 tft.setCursor(70, 30); 134 tft.print(price, 2); 135 136 String formatted_date = formatTime(date); 137 138 tft.setTextSize(1); 139 tft.setCursor(0, 115); 140 tft.print(formatted_date); 141 tft.setCursor(0, 125); 142 tft.print("Update every 15 minutes"); 143 } else { 144 Serial.println("Error on HTTP request"); 145 } 146 http.end(); // End the request 147 } 148}
Downloadable files
bitcoin-price-monitoring-schematics.png
Bitcoin price monitoring - schematics.
bitcoin-price-monitoring-schematics.png

arduino-main.ino
Code to run this project on an Arduino board.
arduino-main.ino
Comments
Only logged in users can leave comments