// Bitcoin tracer, ktery zobrazuje soucasnou cenu a jeji zmenu. Pokud cena 5x za sebou spadla, rozsviti se led.
#include <ezButton.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define SCREEN_WIDTH 128 // Sirka
#define SCREEN_HEIGHT 64 // Vyska
#define OLED_RESET -1 // Reset
#define SCREEN_ADDRESS 0x3C // Pozicovani
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DEBOUNCE_TIME 50
const int ledPin = 12;
ezButton buttonPIN(18); // zamezeni efektu zadrceni
int i = 0;
#define SSID "Wokwi-GUEST"; // jmeno wifi
#define WIFI_PASSWORD ""; // heslo wifi
const char* ssid = "Wokwi-GUEST"; // znovu jmeno wifi pro ukazatel
const char* password = ""; // znovu heslo wifi pro ukazatel
// cena ziskana ze stranky coindesk - https://www.coindesk.com/price/bitcoin
const String url = "http://api.coindesk.com/v1/bpi/currentprice/BTC.json";
const String cryptoCode = "BTC";
// Ikona bitcoinu
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
};
HTTPClient http;
String lastPrice;
String staracena;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
buttonPIN.setDebounceTime(DEBOUNCE_TIME);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Pripojovani k WiFi...");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.print("Pripojeno k: ");
Serial.println(ssid);
display.print("Pripojeno k ");
display.println(ssid);
display.display();
delay(4500);
}
void loop() {
buttonPIN.loop();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Ziskavam data...");
// Po zmacknuti tlacitka led zhasne a vynuluje se "trend".
if (buttonPIN.isPressed()) {
digitalWrite(ledPin, LOW);
i = 0;
}
// ziskavani dat
http.begin(url);
int httpCode = http.GET();
StaticJsonDocument<768> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson failed: "));
Serial.println(error.f_str());
delay(2500);
return;
}
// zpracovani dat
String BTCUSDPrice = doc["bpi"]["USD"]["rate_float"].as<String>();
if(BTCUSDPrice == lastPrice) {
Serial.print("Stala cena: ");
Serial.println(BTCUSDPrice);
delay(5000);
return;
} else {
staracena = lastPrice;
lastPrice = BTCUSDPrice;
Serial.print("Nova cena: ");
Serial.println(lastPrice);
}
http.end();
// Display
display.clearDisplay();
display.drawBitmap(52, 20, bitcoinIcon, 24, 24, WHITE);
display.display();
// Cena
display.setTextSize(2);
printpr("$" + BTCUSDPrice, 00, 50);
// Stonks/notstonks
bool isUp = lastPrice > staracena;
String percentChange;
if (isUp) {
percentChange = "Stonks";
i++;
} else {
percentChange = "NotStonks";
i--;
if (i < -5) digitalWrite(ledPin, HIGH); // Pokud "trend rustu" bude zaporny sestkrat, rozsviti se ledka.
}
display.setTextSize(2);
printpr(percentChange, 0, 0);
display.display();
delay(1250);
}
}
void printpr(const String buf, int x, int y) // tisk hodnot
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
display.setCursor((x - w / 2) + (128 / 2), y);
display.print(buf);
}