Last active
July 13, 2022 11:39
-
-
Save sowbug/6696521e43a5c9585bc494163b436eb5 to your computer and use it in GitHub Desktop.
Display Bitcoin prices on a $10 OLED ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// For Wemos Lolin ESP32 OLED Module For Arduino ESP32 OLED WiFi + Bluetooth | |
// | |
// All of these are available in Arduino Library Manager | |
#include <ArduinoJson.h> | |
#include <HTTPClient.h> | |
#include <SSD1306.h> | |
#include <WiFiMulti.h> | |
#define SDA 5 | |
#define SCL 4 | |
#define I2C 0x3c | |
SSD1306 display(I2C, SDA, SCL); | |
const char* ssid = "your-ssid"; | |
const char* password = "your-ap-password"; | |
WiFiMulti wifiMulti; | |
int sleep_time = 0; | |
const int SLEEP_TIME_MIN = 1000 * 30; | |
const int SLEEP_TIME_MAX = 1000 * 60 * 15; | |
unsigned long last_success = 0; | |
unsigned long time_since_last_success = 0; | |
void adjust_sleep(bool success) { | |
if (success) { | |
last_success = millis(); | |
sleep_time = SLEEP_TIME_MIN; | |
} else { | |
sleep_time *= 2; | |
if (sleep_time > SLEEP_TIME_MAX) { | |
sleep_time = SLEEP_TIME_MAX; | |
} | |
} | |
time_since_last_success = millis() - last_success; | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
display.init(); | |
display.flipScreenVertically(); | |
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH); | |
display.clear(); | |
display.display(); | |
wifiMulti.addAP(ssid, password); | |
adjust_sleep(true); | |
} | |
void checkApi() { | |
HTTPClient http; | |
http.begin("http://api.coindesk.com/v1/bpi/currentprice.json"); | |
int httpCode = http.GET(); | |
if (httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
StaticJsonBuffer<1536> jsonBuffer; | |
JsonObject& root = jsonBuffer.parseObject(payload); | |
if (root.success()) { | |
adjust_sleep(true); | |
display.clear(); | |
{ | |
JsonObject& time = root["time"]; | |
const char* time_updated = time["updated"]; | |
display.setFont(ArialMT_Plain_10); | |
display.drawString(64, 10, time_updated); | |
} | |
{ | |
display.setFont(ArialMT_Plain_10); | |
if (time_since_last_success > 10 * 1000) { | |
display.drawString(64, 64 - 10, "PROBLEM"); | |
} | |
} | |
{ | |
JsonObject& bpi = root["bpi"]; | |
JsonObject& bpi_USD = bpi["USD"]; | |
const char* bpi_USD_rate = bpi_USD["rate"]; | |
display.setFont(ArialMT_Plain_16); | |
display.drawString(64, 32, bpi_USD_rate); | |
} | |
display.display(); | |
} else { | |
Serial.println("parseObject() failed"); | |
Serial.println(payload); | |
adjust_sleep(false); | |
} | |
} else { | |
Serial.println("HTTP error"); | |
Serial.println(httpCode); | |
adjust_sleep(false); | |
} | |
http.end(); | |
} | |
void loop() { | |
if (wifiMulti.run() == WL_CONNECTED) { | |
checkApi(); | |
} else { | |
adjust_sleep(false); | |
} | |
delay(sleep_time); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment