Skip to content

Instantly share code, notes, and snippets.

@SeolHa314
Created January 13, 2021 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeolHa314/2c6faf78de71fe9d51190af32c2f0350 to your computer and use it in GitHub Desktop.
Save SeolHa314/2c6faf78de71fe9d51190af32c2f0350 to your computer and use it in GitHub Desktop.
Display BTCUSD chart on e-ink display
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <vector>
#include "epd_driver.h"
#include "firasans.h"
uint8_t* eink_framebuffer;
const char* ssid = "";
const char* password = "";
int eink_cursor[2] = {0, 50};
String bitAPIurl = "http://api.coindesk.com/v1/bpi/historical/close.json";
HTTPClient http;
DynamicJsonDocument bpiJson(1536);
std::vector<double> bpiPriceList;
void setup() {
Serial.begin(115200);
epd_init(); epd_poweron(); epd_clear();
eink_framebuffer = (uint8_t *)heap_caps_malloc(EPD_WIDTH * EPD_HEIGHT / 2, MALLOC_CAP_SPIRAM);
memset(eink_framebuffer, 0xff, EPD_WIDTH * EPD_HEIGHT / 2);
WiFi.begin(ssid, password);
write_string((GFXfont *)&FiraSans, "Connecting WiFi", &eink_cursor[0], &eink_cursor[1], NULL);
int retries = 0;
while (WiFi.status() != WL_CONNECTED)
{
if(retries++ > 10)
{
esp_restart();
}
delay(500);
eink_cursor[1] = 50;
write_string((GFXfont *)&FiraSans, ".", &eink_cursor[0], &eink_cursor[1], NULL);
}
eink_cursor[0] = 0;
writeln((GFXfont *)&FiraSans, "Connected! <3", &eink_cursor[0], &eink_cursor[1], NULL);
delay(300);
Serial.println("Connected.");
eink_cursor[0] = 0; eink_cursor[1] += 50;
writeln((GFXfont *)&FiraSans, "Getting historical price...", &eink_cursor[0], &eink_cursor[1], NULL);
http.begin(bitAPIurl);
int respCode = http.GET();
if(respCode == 0)
{
Serial.print("Http error :");
Serial.println(respCode);
eink_cursor[0] = 0;
eink_cursor[1] += 50;
writeln((GFXfont *)&FiraSans, "Http internal error", &eink_cursor[0], &eink_cursor[1], NULL);
} else if (respCode != 200)
{
char errorString[30];
sprintf(errorString, "Http Error : %d", respCode);
eink_cursor[0] = 0; eink_cursor[1] += 50;
writeln((GFXfont *)&FiraSans, errorString, &eink_cursor[0], &eink_cursor[1], NULL);
}
String respString = http.getString();
Serial.println(respCode);
Serial.println(respString.length());
deserializeJson(bpiJson, respString);
JsonObject bpiObject = bpiJson.as<JsonObject>();
http.end();
for(auto bpiPair: bpiObject["bpi"].as<JsonObject>()) {
bpiPriceList.push_back(bpiPair.value().as<double>());
}
double maxPrice = *std::max_element(std::begin(bpiPriceList), std::end(bpiPriceList));
double minPrice = *std::min_element(std::begin(bpiPriceList), std::end(bpiPriceList));
Serial.printf("%f, %f", maxPrice, minPrice);
for(int i = 0; i < bpiPriceList.size() - 1; i++) {
double beforeHeight = EPD_HEIGHT - (bpiPriceList[i] - minPrice) / (maxPrice - minPrice) * EPD_HEIGHT;
double afterHeight = EPD_HEIGHT - (bpiPriceList[i + 1] - minPrice) / (maxPrice - minPrice) * EPD_HEIGHT;
epd_draw_line(i * EPD_WIDTH / (bpiPriceList.size() - 1), (int)beforeHeight, (i + 1) * EPD_WIDTH / (bpiPriceList.size() - 1), (int)afterHeight, 0, eink_framebuffer);
}
epd_clear();
epd_draw_grayscale_image(epd_full_screen(), eink_framebuffer);
epd_poweroff();
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment