Created
September 13, 2017 05:57
-
-
Save hemalchevli/9ad43a8d216a6130364e8dedb30ddfc1 to your computer and use it in GitHub Desktop.
BTC ticker Wemos mini with oled shield
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
#include <ESP8266WiFi.h> | |
#include <ArduinoJson.h> | |
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
// SCL GPIO5 | |
// SDA GPIO4 | |
#define OLED_RESET 0 // GPIO0 | |
Adafruit_SSD1306 display(OLED_RESET); | |
#define NUMFLAKES 10 | |
#define XPOS 0 | |
#define YPOS 1 | |
#define DELTAY 2 | |
#if (SSD1306_LCDHEIGHT != 48) | |
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); | |
#endif | |
const char* ssid = "ssid"; | |
const char* password = "password"; | |
const char* host = "api.coindesk.com"; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48) | |
// Clear the buffer. | |
display.clearDisplay(); | |
display.display(); | |
} | |
void loop() { | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
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(); | |
String jsonAnswer; | |
int jsonIndex; | |
for (int i = 0; i < answer.length(); i++) { | |
if (answer[i] == '{') { | |
jsonIndex = i; | |
break; | |
} | |
} | |
// Lettura dei dati JSON | |
jsonAnswer = answer.substring(jsonIndex); | |
jsonAnswer.trim(); | |
// Ottenimento del prezzo come valore float | |
int rateIndex = jsonAnswer.indexOf("rate_float"); | |
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 18); | |
priceString.trim(); | |
float price = priceString.toFloat(); | |
Serial.println(price); | |
if(price > 0){ | |
display.clearDisplay(); | |
display.setTextSize(2); | |
display.setTextColor(WHITE); | |
display.setCursor(0,0); | |
display.println(price); | |
display.display(); | |
} | |
delay(10000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment