Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kendx/3fde7e2c2c7eda36cb89 to your computer and use it in GitHub Desktop.
Save kendx/3fde7e2c2c7eda36cb89 to your computer and use it in GitHub Desktop.
/*
* Bitcoin Price Ticker using ESP8266 WiFi Module
* www.KendrickTabi.com
* http://www.kendricktabi.com/2015/06/bitcoin-price-ticker-using-esp8266-wifi-module.html
*/
#include <SoftwareSerial.h>
#define SSID "mySSID" // insert your SSID
#define PASS "myPassword" // insert your password
String id;
String value;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial1.begin(9600);
while (!Serial1) {
; // wait for serial port to connect. Needed for Leonardo only
}
delay(1000);
Serial.println("Bitcoin Price Ticker");
Serial.println("by: www.KendrickTabi.com");
Serial.println();
Serial1.println("AT+RST"); // restet and test if module is redy
Serial.println("Resetting the module...");
delay(1000);
if(Serial1.find("ready")) {
Serial.println("WiFi - Module is ready");
}
else{
Serial.println("Module dosn't respond. Please restart.");
return;
}
delay(3000);
Serial1.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial1.println(cmd);
delay(5000);
if(Serial1.find("OK")){
Serial.println("OK, Connected to WiFi.");
Serial.println();
}
else{
Serial.println("Can not connect to the WiFi.");
Serial.println();
return;
}
delay(5000);
Serial1.println(F("AT+CIPMUX=0")); // set to single connection mode
}
void loop() {
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "api.coindesk.com";
cmd += "\",80";
Serial1.println(cmd);
if(Serial1.find("Error")) return;
cmd = "GET /v1/bpi/currentprice.json";
cmd += " HTTP/1.0\r\nHost: api.coindesk.com\r\n\r\n";
Serial1.print("AT+CIPSEND=");
Serial1.println(cmd.length());
if(Serial1.find(">")){
Serial.println("Connection established.");
}
else{
Serial1.println("AT+CIPCLOSE");
Serial.println("Connection timeout.");
delay(1000);
return;
}
Serial1.println(cmd);
unsigned int i = 0; //timeout counter
int n = 1; // char counter
char json[500] ="{";
while (!Serial1.find("\"USD\":{")){}
while (i<600) {
if(Serial1.available()) {
char c = Serial1.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);
Serial.println();
id="";
value="";
delay(60000); // Repeat every minute
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment