#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String url = "http://api.coindesk.com/v1/bpi/currentprice/BTC.json";
const String historyURL = "http://api.coindesk.com/v1/bpi/historical/close.json";
const String cryptoCode = "BTC";
HTTPClient http;
String lastPrice;
// 'icons8-bitcoin-24', 24x24px
const unsigned char bitcoinIcon [] PROGMEM = {
0x00, 0x7e, 0x00, 0x03, 0xff, 0xc0, 0x07, 0x81, 0xe0, 0x0e, 0x00, 0x70, 0x18, 0x28, 0x18, 0x30,
0x28, 0x0c, 0x70, 0xfc, 0x0e, 0x60, 0xfe, 0x06, 0x60, 0xc7, 0x06, 0xc0, 0xc3, 0x03, 0xc0, 0xc7,
0x03, 0xc0, 0xfe, 0x03, 0xc0, 0xff, 0x03, 0xc0, 0xc3, 0x83, 0xc0, 0xc1, 0x83, 0x60, 0xc3, 0x86,
0x60, 0xff, 0x06, 0x70, 0xfe, 0x0e, 0x30, 0x28, 0x0c, 0x18, 0x28, 0x18, 0x0e, 0x00, 0x70, 0x07,
0x81, 0xe0, 0x03, 0xff, 0xc0, 0x00, 0x7e, 0x00
};
void setup() {
// โค๊ดจัดเตรียมข้อมูล
Serial.begin(115200);
Serial.print("CONNECTED to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("Connected");
//display.println(ssid);
//Serial.println("");
//display.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Hello, krurueng!");//เปลียนข้อมูลให้เป็นชื่อตัวเอง
delay(5000);
}
void loop() {
// โค๊ดให้โปรแกรมทำซ้ำ
clientcon();
delay(10000); //
//Display Header
//display.clearDisplay();
//display.setTextSize(1);
//printCenter("BTC/USD", 0, 0);
//display.clearDisplay();
//display.drawBitmap((128 / 2) - (24 / 2), 0, bitcoinIcon, 24, 24, WHITE);
//Display BTC Price
//String BTCUSDPrice = "123456789";
//display.setTextSize(1);
//printCenter("$" + BTCUSDPrice, 0, 32);
//display.display();
}
void clientcon() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Getting current data...");
http.begin(url);
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
if (httpCode > 0) {
StaticJsonDocument<768> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson failed: "));
Serial.println(error.f_str());
delay(2500);
return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String BTCUSDPrice = doc["bpi"]["USD"]["rate_float"].as<String>();
if (BTCUSDPrice == lastPrice) {
Serial.print("Price hasn't changed (Current/Last): ");
Serial.print(BTCUSDPrice);
Serial.print(" : ");
Serial.println(lastPrice);
delay(1250);
return;
} else {
lastPrice = BTCUSDPrice;
}
String lastUpdated = doc["time"]["updated"].as<String>();
http.end();
Serial.println("Getting history...");
StaticJsonDocument<1536> historyDoc;
http.begin(historyURL);
int historyHttpCode = http.GET();
DeserializationError historyError = deserializeJson(historyDoc, http.getString());
if (historyError) {
Serial.print(F("deserializeJson(History) failed: "));
Serial.println(historyError.f_str());
delay(2500);
return;
}
//Display Header
//display.clearDisplay();
//display.setTextSize(1);
//printCenter("BTC/USD", 0, 0);
//display.clearDisplay();
//display.drawBitmap((128 / 2) - (24 / 2), 0, bitcoinIcon, 24, 24, WHITE);
//display.display();
//Display BTC Price
//display.setTextSize(1);
//printCenter("$" + BTCUSDPrice, 0, 32);
//Display 24hr. Percent Change
double yesterdayPrice = historyDoc["bpi"]["2021-03-20"].as<double>();
bool isUp = BTCUSDPrice.toDouble() > yesterdayPrice;
double percentChange;
String dayChangeString = "24hr. Change: ";
if (isUp) {
percentChange = ((BTCUSDPrice.toDouble() - yesterdayPrice) / yesterdayPrice) * 100;
} else {
percentChange = ((yesterdayPrice - BTCUSDPrice.toDouble()) / yesterdayPrice) * 100;
dayChangeString = dayChangeString + "-";
}
//display.setTextSize(1);
//dayChangeString = dayChangeString + percentChange + "%";
//printCenter(dayChangeString, 0, 55);
//display.display();
http.end();
}
delay(1250);
}
}