Created
January 19, 2016 19:01
-
-
Save buzztiaan/1c51046481e829de06d6 to your computer and use it in GitHub Desktop.
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
// bitcointicker v0.04 | |
// buZz / NURDspace | |
// libraries used; | |
// | |
// * https://github.com/squix78/esp8266-oled-ssd1306 | |
// * https://github.com/tzapu/WiFiManager | |
#include <ESP8266WiFi.h> | |
#include <DNSServer.h> | |
#include <ESP8266WebServer.h> | |
#include <WiFiManager.h> | |
#include <ArduinoJson.h> | |
#include <Wire.h> | |
#include "SSD1306.h" | |
// Initialize the oled display for address 0x3c | |
// sda-pin=14 and sdc-pin=12 | |
SSD1306 display(0x3c, D3, D4); | |
const char* host = "api.coindesk.com"; | |
String url = "/v1/bpi/currentprice.json"; | |
#define DEBUG 1 | |
void setup() { | |
display.init(); | |
display.setContrast(0x01); | |
//display.setColor(0); | |
display.fillRect(1,30,128,1); | |
display.display(); | |
Serial.begin(115200); | |
WiFiManager wm; | |
wm.autoConnect(); | |
if (DEBUG) Serial.println("connected to wifi!"); | |
//display.drawRect(1,30,128,2); | |
// display.flipScreenVertically(); | |
} | |
WiFiClient client; | |
const int httpPort = 80; | |
int y = 0; | |
void loop() { | |
StaticJsonBuffer<800> jsonBuffer; | |
if (!client.connect(host, httpPort)) { | |
if (DEBUG) Serial.println("connection failed"); | |
ESP.deepSleep(1000000); | |
return; | |
} | |
client.print(String("GET ") + url + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n"); | |
delay(10); | |
unsigned int i = 0; | |
int n = 1; | |
char json[500] ="{"; | |
while (!client.find("\"USD\":{")){delay(1);} | |
int timeout = millis() + 5000; | |
while (client.available() == 0) { | |
delay(1); | |
if (timeout - millis() < 0) { | |
if (DEBUG) Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
ESP.deepSleep(1000000); | |
return; | |
} | |
} | |
while (i<600) { | |
delay(1); | |
if(client.available()) { | |
char c = client.read(); | |
json[n]=c; | |
if(c=='}') break; | |
n++; | |
i=0; | |
} | |
i++; | |
} | |
JsonObject& root = jsonBuffer.parseObject(json); | |
if (!root.success()) { | |
if (DEBUG) Serial.println("parseObject() failed"); | |
ESP.deepSleep(1000000); | |
} else { | |
const char* code = root["code"]; | |
double rate_float = root["rate_float"]; | |
y++; | |
if (y>20) y = 1; | |
display.clear(); | |
display.setFont(ArialMT_Plain_24); | |
display.drawString(15, 10, root["rate"]); | |
display.display(); | |
if (DEBUG) Serial.println(rate_float); | |
ESP.deepSleep(60000000); // 1 minute sleep | |
//delay(25000); // Repeat 10 secs | |
} | |
while (client.available()) { | |
delay(1); | |
char bla = client.read(); | |
} | |
client.stop(); | |
ESP.deepSleep(1000000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment