1
2
3
4
5
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
16const char* ssid = "";
17const char* password = "";
18
19
20const char* api_key = "";
21const char* api_url = "https://rest.coinapi.io/v1/exchangerate/BTC/USD";
22
23
24const int waiting_time = 900000;
25
26
27#define TFT_CS 5
28#define TFT_RST 4
29#define TFT_DC 2
30
31
32Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
33
34void setup() {
35
36 Serial.begin(115200);
37
38
39 tft.init(135, 240);
40 tft.setRotation(3);
41 tft.fillScreen(ST77XX_BLACK);
42
43
44 connectToWiFi();
45
46
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
74 String date = time.substring(0, 10);
75 String timeOfDay = time.substring(11, 16);
76
77
78 String year = date.substring(0, 4);
79 String month = date.substring(5, 7);
80 String day = date.substring(8, 10);
81
82
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
89 int hour = timeOfDay.substring(0, 2).toInt();
90 String minute = timeOfDay.substring(3, 5);
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;
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();
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
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();
147 }
148}