Skip to content

Commit

Permalink
use bitstamp API with https, lower refresh rate
Browse files Browse the repository at this point in the history
  • Loading branch information
nebman committed Sep 16, 2017
1 parent 5ff9733 commit 6f8d995
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions btc-ticker-esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@

#include <LedControl.h>

const String url = "http://api.coindesk.com/v1/bpi/currentprice.json";
const char* fingerprint = "D0 26 AB 06 64 07 BC 88 56 6D 83 BE 0A 29 00 B5 10 E5 27 D2";
const char* host = "www.bitstamp.net";
const int port = 443;
const char* url = "/api/v2/ticker/btcusd/";
WiFiClientSecure https;

const int brightness_full = 8; // 0-15 normal brightness
const int brightness_load = 8; // 0-15 while requesting data

const int refresh_time = 15000; // time to refresh, 15sec default
unsigned long next_refresh = 0;

LedControl lc = LedControl(D7, D8, D6, 1);
WiFiManager wifiManager;

HTTPClient http;

void setup() {
// start serial for debug output
Serial.begin(115200);
Expand Down Expand Up @@ -77,24 +80,37 @@ void loop() {

if( (long)(millis() - next_refresh) >= 0)
{
unsigned int err = 0;

setDashes();
setBrightnessLoading(true);

Serial.print("REQUEST ");
Serial.println(url);

http.begin(url);
int code = http.GET();
Serial.println(code);
if (https.connect(host, port)) {
Serial.println("OK");
} else {
Serial.println("NOCONN");
err = 100;
}

https.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266 7-Segment Ticker\r\n" +
"Connection: close\r\n\r\n");

while (https.connected()) {
String line = https.readStringUntil('\n');
if (line == "\r") break; // end of headers
}

if (code == 200) {
String payload = http.getString();

String line = https.readStringUntil('\n'); // one line of json

if (line) {
DynamicJsonBuffer jsonBuffer(1100);
JsonObject& root = jsonBuffer.parseObject(payload);
JsonObject& bpi = root["bpi"];
JsonObject& bpi_USD = bpi["USD"];
int last = bpi_USD["rate_float"]; // last USD btc
JsonObject& root = jsonBuffer.parseObject(line);
int last = root["last"]; // last USD btc

Serial.print("last = ");
Serial.println(last);
Expand All @@ -111,13 +127,12 @@ void loop() {
setBrightnessLoading(false);
} else {
lc.setChar (0, 3, 'E', true);
lc.setDigit(0, 2, (code%1000)/100, false);
lc.setDigit(0, 1, (code%100)/10, false);
lc.setDigit(0, 0, (code%10), false);
lc.setDigit(0, 2, (err%1000)/100, false);
lc.setDigit(0, 1, (err%100)/10, false);
lc.setDigit(0, 0, (err%10), false);
}

http.end();
next_refresh = millis() + 30000;
next_refresh = millis() + refresh_time;
}

}
Expand Down

0 comments on commit 6f8d995

Please sign in to comment.