A WiFi Bitcoin Ticker with the WEMOS D1 Mini

So I got a WEMOS D1 Mini in my Hackerboxes 0023 monthly subscription box, and hadn’t done anything with it yet. I’ve been playing with crypto-currency stuff lately, and decided it would be nice to have a small bitcoin price ticker that I could stick somewhere. So I found some code online, put a few ideas together, and here’s what I’ve come up with. Using the WEMOS D1 Mini, and the OLED display for it, I’ve written an Arduino sketch that will go fetch the Bitcoin/USD price every 30 seconds from Coinbase. You’ll need to load a few libraries, namely the ArduinoJSON, AdaFruit_SSD1307, and the AdaFruit GFX Library. Also, since this is an ESP8266, you’ll need those board definitions (don’t worry, I’ll try and record a walk-through video of all these steps at a later point). I’ve got some serial debugging stuff in here too, so you can monitor the serial port if something is going sideways. But for now – the code.

/

#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
//Coindesk Configuration
const char* host = "api.coindesk.com";
//WiFi Configuration
const char* ssid = "yourSSID";
const char* password = "yourWiFiPassword";
#define OLED_RESET 0
// GPIO0 Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 48) #error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
// Begin the Wifi Connection WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

display.clearDisplay();
display.display();
delay(2000);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
}

void loop() {
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) { Serial.println("connection failed");
return;
}

String url = "/v1/bpi/currentprice.json";
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
delay(100);
String answer;
while(client.available()){
String line = client.readStringUntil('\r');
answer += line;
}

client.stop();
Serial.println();
Serial.println("closing connection");
String jsonAnswer;
int jsonIndex; for (int i = 0;
i < answer.length();
i++) { if (answer[i] == '{') {
jsonIndex = i; break;
}
}
jsonAnswer = answer.substring(jsonIndex);
Serial.println();
jsonAnswer.trim();
int rateIndex = jsonAnswer.indexOf("rate_float");
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 18);
priceString.trim();
float price = priceString.toFloat();
Serial.println();
Serial.println("Bitcoin price: ");
Serial.println(price);
int intPrice;
intPrice = (int) price;
display.clearDisplay();
display.setCursor(0,0);
display.println("BTC");
display.println(intPrice);
display.display();
delay(30000);
}

When you’re done, you’ll have something a bit like this