Created
August 16, 2015 13:21
-
-
Save kendx/e8812eaf01cebdbe2a77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Bitcoin Price Ticker using ESP8266 WiFi Module - Part 2 | |
* www.KendrickTabi.com | |
* http://www.kendricktabi.com/2015/06/part-2-bitcoin-price-ticker-esp8266.html | |
*/ | |
#include <ESP8266WiFi.h> | |
const char* ssid = "mySSID"; // insert your SSID | |
const char* password = "myPassword"; // insert your password | |
const char* host = "api.coindesk.com"; | |
String url = "/v1/bpi/currentprice.json"; | |
String id; | |
String value; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
Serial.println("Bitcoin Price Ticker"); | |
Serial.println("by: www.KendrickTabi.com"); | |
// We start by connecting to a WiFi network | |
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()); | |
} | |
void loop() { | |
Serial.println(); | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
unsigned int i = 0; //timeout counter | |
int n = 1; // char counter | |
char json[500] ="{"; | |
while (!client.find("\"USD\":{")){} | |
while (i<600) { | |
if(client.available()) { | |
char c = client.read(); | |
json[n]=c; | |
if(c=='}') break; | |
n++; | |
i=0; | |
} | |
i++; | |
} | |
Serial.println(json); | |
id += json; | |
value += json; | |
id = id.substring(9,12); | |
Serial.print(id); | |
Serial.print("= "); | |
value = value.substring(99,106); | |
Serial.println(value); | |
id=""; | |
value=""; | |
Serial.println("closing connection"); | |
delay(60000); // Repeat every minute | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment