Posted on

BTCmarket ticker using ESP8266

BTCmarket Ticker with ESP8266

With the recent fall of Bitcoin and other crypto currency and my continue interest in learning more about Arduino, after reading several other instruction of using OLED display, I had combine it all to create a BTCmarket ticker using ESP8266. The reason why I use BTCmarket as the sample is because this is the one that I am currently using, but you can adapt that to any market ticker providing they have API to support data exchange.

Step 1: Gather all the materials needed

The materials used in this are:

  • ESP8266
  • 128×64 0.96″ OLED display

Step 2: Connect the ESP8266 to the OLED display using the following :

  • Connect the SCL of OLED display to D2 of ESP8266
  • Connect the SDA of OLED display to D4 of ESP8266
  • Connect the VCC to 3.3V
  • connect the GND to ground

This is shown in the figure below:

ESP8266 connection diagram
ESP8266 Connection

Step 3: Load library to Arduino

The following instruction assume that you are already familiar with Arduino interface and know where the library is located. For Mac users the Arduino library is located in Documents/Arduino/Libraries.

You will need the ESP8266RestClient library, TimeLib library and theĀ ESP8266_and_ESP32_Oled_Driver_for_SSD1306_display library. Credit goes to their respective author for creating such an awesome library.

Step 4: Load the following code in Arduino interface and upload it to the ESP8266

// BTCMarket Crypto currency ticker
//
// 128x64 OLED pinout:
// GND goes to ground
// Vin goes to 3.3V
// Data to I2C SDA (GPIO 4)
// Clk to I2C SCL (GPIO 2)
#include <TimeLib.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include "RestClient.h"
#include <SSD1306.h>
#include <SSD1306Wire.h>


char ssid[] = "INSERT YOUR SSID HERE"; // your network SSID (name)
char pass[] = "INSERT YOUR NETWORK PASSWORD"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

RestClient restclient = RestClient("api.btcmarkets.net",443,1); 
SSD1306 display(0x3c, 4, 2); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED


void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(115200);
 //Setup Rest Client
 WiFi.begin(ssid, pass);

 Wire.pins(4, 2); // Start the OLED with GPIO 4 and 2 on ESP-01
 Wire.begin(4, 2); // 4=sda, 2=scl
 display.init();
 display.flipScreenVertically();
 display.drawString(0, 24, "Connected.");
 display.display();
 delay(1000);
}

void processticker(){
 String response = "";
 //Get the ETH ticker
 int statusCode = restclient.get("/market/ETH/AUD/tick", &response);
 // for some reason sometimes the response is not clean
 String temp = response.substring(response.lastIndexOf("{"), response.lastIndexOf("}")+1);
 //Serial.println("status:" + String(statusCode) + " response:" + temp);
 int rlength = temp.length();
 //Serial.println("Response Length=" + String(rlength));
 char jvar[rlength+1];
 
 temp.toCharArray(jvar, rlength+1);
 //jvar[rlength] = 0;
 StaticJsonBuffer<300> jsonBuffer;
 Serial.println("jvar:" + String(jvar));
 JsonObject& root = jsonBuffer.parseObject(jvar);
if (!root.success()){
 Serial.println(F("Parsing failed!"));
 Serial.println("temp:" + temp);
 } else{
 String Buy = root["bestBid"];
 String Sell = root["bestAsk"];
 String LastTrade = root["lastPrice"];
 time_t tStamp = root["timestamp"];
 tStamp = tStamp + 600 * 60; // GMT + 10
 String TheDate = String(String(day(tStamp)) + "/" + String(month(tStamp)));
 String TheTime = String(String(hour(tStamp)) + ":" + String(minute(tStamp)));
 Serial.println("Buy:"+Buy+"; Sell:"+Sell + "; Last:"+LastTrade );
 display.clear();
 display.drawString(0,0, "ETH/AUD");
 display.drawString(0,13, TheDate +" " + TheTime);
 display.drawString(0,23, "Buy: "+Buy);
 display.drawString(0,33, "Sell: "+Sell);
 display.display();
 }
 
 delay(1000);
}

int timer =0;

void loop() {

if (timer > 5){ // so that we don't call it too frequently we add delay of 5 second
 timer = 0;
 //process ticker
 processticker(); 
 }else{ // handle http client while waiting
 timer++;
 }

delay(1000);
}

You should be able to have the Ethereum ticker now that you can expand to give you an alert they way you want it.

If you like this post, please give a comment or share with your friends. You can also subscribe to my blog for regular updates.

Leave a Reply