An improved ESP8266 and Bitcoin price ticker example

This is similar to our previous ESP8266 bitcoin example – Basic ESP8266 and bitcoin price example

We used the same Wemos board as in the previous example

In this example we use ArduinoJson library to parse the json response from coindesk. A better approach than the previous example

Again you will have to change the username and password for your wifi credentials

const char* ssid = “your username here”;
const char* pass = “your password here”;

Again we will display the results via the serial monitor

Code

Open the library manager and add support for the ArduinoJson library to the Arduino IDE

Copy and paste the following code into the Arduino IDE, build and program your board

[c]

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>

const char* ssid = “your username here”;
const char* pass = “your password here”;

String id;
String value;
String json;

WiFiClient client;

const unsigned long postingInterval = 60L * 1000L;
unsigned long lastConnectionTime = 0;

void setup()
{
delay(100);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
delay(50);

}

int check_connect = 0;

void httpRequest()
{
client.stop();

// if there's a successful connection:
if (client.connect(“api.coindesk.com”, 80))
{
Serial.println(“connecting…”);
client.println(“GET /v1/bpi/currentprice.json HTTP/1.1”);
client.println(“Host: api.coindesk.com”);
client.println(“User-Agent: ESP8266/1.1”);
client.println(“Connection: close”);
client.println();
lastConnectionTime = millis();
}
else
{
// if you couldn't make a connection:
Serial.println(“connection failed”);
}
}

void loop()
{
int cnt;

if (cnt++ == 10000) {
cnt = 0;
if (check_connect++ == 50) {
check_connect = 0;
if (WiFi.status() != WL_CONNECTED) {

}
}
}

if (millis() – lastConnectionTime > postingInterval) {
httpRequest();
unsigned int i = 0; //timeout counter
int n = 1; // char counter
char json[500] =”{“;

while (!client.find(“\”USD\”:{“)){}

while (i<20000) {
if (client.available()) {
char c = client.read();
json[n]=c;
if (c=='}') break;
n++;
i=0;
}
i++;
}

Serial.println(json);

StaticJsonBuffer<500> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);

String newjson = root[“code”];
String value = root[“rate”];
id = newjson.substring(9,12);

Serial.print(“1 bitcoin = USD value : “);
Serial.println(value);

id=””;
value=””;
}
}

[/c]

 

Testing

Open the serial monitor and you should see something like this if everything goes to plan

Connecting to ****************
…………..
WiFi connected
IP address:
192.168.1.15
connecting…
{“code”:”USD”,”symbol”:”$”,”rate”:”9,692.3175″,”description”:”United States Dollar”,”rate_float”:9692.3175}
1 bitcoin = USD value : 9,692.3175

Related posts

Displaying the bitcoin price using the coindesk API

Let’s build a blockchain in Ruby

Cryptocurrency Blockchain in 30 minutes in C++