Skip to content

Instantly share code, notes, and snippets.

@sowbug
Created May 29, 2021 22:42
Show Gist options
  • Save sowbug/23825ed0db9238d6b906f1b98f9dbc11 to your computer and use it in GitHub Desktop.
Save sowbug/23825ed0db9238d6b906f1b98f9dbc11 to your computer and use it in GitHub Desktop.
Bitcoin tracker on ESP8266-based OLED
// For Heltec Wifi Kit 8
// Code stolen/adapted from many sources
#include <U8g2lib.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4);
//U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 4, /* clock=*/ 14, /* data=*/ 2);
u8g2_uint_t offset; // current offset for the scrolling text
u8g2_uint_t width; // pixel width of the scrolling text (must be lesser than 128 unless U8G2_16BIT is defined
char text[32] = "Loading... "; // scroll this text from right to left
const char* url = "https://api.coinbase.com/v2/prices/spot?currency=USD";
const char* host = "api.coinbase.com";
WiFiClientSecure c;
void setup(void) {
u8g2.begin();
u8g2.setFont(u8g2_font_logisoso32_tf); // set the target font to calculate the pixel width
width = u8g2.getUTF8Width(text); // calculate the pixel width of the text
u8g2.setFontMode(0); // enable transparent mode, which is faster
Serial.begin(115200);
Serial.print("Connecting");
WiFi.begin("yourssid", "yourpw");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
c.setInsecure();
if (!c.connect(host, 443)) {
Serial.println("WiFiClientSecure connection failed");
return;
}
}
int last = 0;
void loop(void) {
u8g2_uint_t x;
u8g2.firstPage();
do {
// draw the scrolling text at current offset
x = offset;
u8g2.setFont(u8g2_font_logisoso32_tf); // set the target font
do { // repeated drawing of the scrolling text...
u8g2.drawUTF8(x, 32, text); // draw the scolling text
x += width; // add the pixel width of the scrolling text
} while ( x < u8g2.getDisplayWidth() ); // draw again until the complete display is filled
u8g2.setFont(u8g2_font_logisoso32_tf); // draw the current pixel width
u8g2.setCursor(0, 64);
u8g2.print(width); // this value must be lesser than 128 unless U8G2_16BIT is set
} while ( u8g2.nextPage() );
offset -= 1; // scroll by one pixel
if ( (u8g2_uint_t)offset < (u8g2_uint_t) - width )
offset = 0; // start over again
if (millis() - last > 30000) {
Serial.printf("trying.......\n");
last = millis();
if (!c.connected()) {
c.connect(host, 443);
}
HTTPClient http;
http.begin(c, url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& root = jsonBuffer.parseObject(payload);
JsonObject& data = root["data"];
const char* data_amount = data["amount"];
Serial.println(data_amount);
strcpy(text, data_amount);
strcat(text, " ");
} else {
Serial.printf("[HTTP] GET... failed, error: %d %s\n", httpCode, http.errorToString(httpCode).c_str());
}
http.end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment