Build a Physical Bitcoin Ticker With the ESP8266

The Internet of Things can be used in many applications. For instance, in this project, we are going to use ESP8266 to venture a little bit into the Bitcoin industry. The project we will be looking at will involve getting real-time prices of Bitcoin and displaying the prices on an OLED screen. We’ll also add some LEDs to show if the price of Bitcoin is going up or down. Let’s start!

Hardware & Software Requirements

The ESP8266 Wi-Fi chip is the backbone of this project. So the most important hardware requirement is an ESP8266 Wi-Fi board. Any ESP8266 module will do the trick.

You also require an OLED screen for display functions. There is a wide array of displays that you can use. I chose a 128×64 OLED with SSD1306 driver after considering several factors, such as compatibility of the display with the ESP8266 module.

When it comes to the price trend indicator, LEDs are the best bet. So, you will need two LEDs, preferably one red and one green. You should also get a 330Ω limiting resistor for each LED.

Other hardware components you will need include a 3.3V/5V FTDI module for programming the ESP8266 and jumper wires and a breadboard to connect the different hardware components. Here is a list of the hardware requirements:

With the hardware requirements out of the way, you can now proceed to the software requirements. The first thing you need is the latest Arduino IDE. You can download it from the Arduino official website.

You will also need a library for the SSD1306 OLED screen which you can download from the Arduino library manager or get it from GitHub.

To get the bitcoin price from the internet you will require some extra help. Since the ESP8266 cannot access the bitcoin prices directly, you have to use an API that will return the current Bitcoin prices in a suitable format that can be processed by the ESP8266 chip. A good example is the Coindesk API http://api.coindesk.com/v1/bpi/currentprice.json.

The API returns the current price of Bitcoin in different currencies. We will use it in this project to get the price of Bitcoin.

Hardware Configuration

Now it’s time for the fun part – putting everything together. Start by mounting the ESP8266 module and the OLED screen on the breadboard.

Once that is done, connect the power pins to the OLED screen. Run a jumper wire between the Vin pin of the screen and the 3.3V pin of the ESP8266 and another jumper from GND of the screen to GND of the ESP8266.

The screen will be communicating with the ESP8266 via I2C. So, we need two jumpers to go from the DATA pin and the CLK pin of the OLED screen to pin 14 and pin 12 of the ESP8266 board respectively. One thing to note before connecting the I2C pins is that the screen can be configured to use either I2C or SPI. Therefore, make sure that your module is configured to use I2C.

The final step is to connect the RST pin of the OLED screen to ESP8266 board’s pin 2. This connection might not be necessary at all times, though it’s always advisable to have it.

The setup will look like this:

Testing the Ticker

Once you are satisfied with your hardware connections, start configuring your setup. We will first look at the project code section by section, so that you can understand every bit of it.

The first piece of code includes the required library files.

#include 

The I2C pins and the I2C address are then defined:

#define SDA 14
#define SCL 12
#define I2C 0x3D

We then proceed to create an instance of the LCD display:

SSD1306 display(I2C, SDA, SCL);

To get the current Bitcoin price, we will have to use the Coindesk API that was described earlier. Its URL is defined in the code like this:

const char* host = "api.coindesk.com";

The WiFi credentials are then defined. They include the name and password:

const char* ssid     = "wifi-network";
const char* password = "wif-password";

Several things are done in the setup() function of our code. First of all, we initialize the display:

display.init();
display.flipScreenVertically();
display.clear();
display.display();

We then connect to the Wi-Fi network:

Serial.println();
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());

The loop() function of the program contains most of the code. First we connect to the API server:

WiFiClient client;

const int httpPort = 80;

if (!client.connect(host, httpPort)) {

  Serial.println("connection failed");
  return;

}

Next we prepare the URL that we will call once connected to the server:

String url = "/v1/bpi/currentprice.json";

After preparing the URL we 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");

We then read the incoming data from the server:

String answer;
while(client.available()){

  String line = client.readStringUntil('\r');
  answer += line;

}



client.stop();
Serial.println();
Serial.println("closing connection");

Now we have the answer from the server. The next step is to extract the JSON answer from the raw data we have:

String jsonAnswer;

int jsonIndex;



for (int i = 0; i < answer.length(); i++) {

  if (answer[i] == '{') {

    jsonIndex = i;
   
    break;

  }

}

Then we extract the JSON object and place it in a string:

jsonAnswer = answer.substring(jsonIndex);
Serial.println();
Serial.println("JSON answer: ");
Serial.println(jsonAnswer);
jsonAnswer.trim();

Once we have the JSON object we can get the Bitcoin price by doing some string operations:

int rateIndex = jsonAnswer.indexOf("rate_float");
String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 18);
priceString.trim();
float price = priceString.toFloat();

We then print out the price on the serial monitor:

Serial.println();
Serial.println("Bitcoin price: ");
Serial.println(price);

Then finally display the price on the screen with large font and make it centered:

display.clear();
display.setFont(ArialMT_Plain_24);
display.drawString(26, 20, priceString);
display.display();

To prevent the sketch from repeating itself every time, we should change the delay between one update and the next:

delay(5000);

Now it’s time to test the sketch. Get the complete code in our GitHub repository and change the Wi-Fi name and password in the sketch to match your Wi-Fi networks credentials. Upload the code to the board and open the serial monitor. The ESP8266 should receive the answer from the CoinDesk server almost immediately. During the time of testing, the price was 416.45USD, as shown on the serial monitor. This information should also be shown on the OLED screen as shown in the image below:

That’s it! You have successfully created your first physical Bitcoin ticker.

Add Alert LEDs to the Ticker

Now that the hardest and most important part of the project is done, we can now proceed to the easier part – adding alert LEDs. We will add two LEDs to our setup that will indicate the price trend. Flashing the green LED will indicate that the price is going up, while flashing the red one will indicate that the price is going down.

Adding the hardware to the project is simple. All you need to do is place the LEDs in series with the 330Ω limiting resistors on the breadboard. Then connect the LEDs to the ESP8266 pin 5 (red LED) and pin 4 (green LED).

Apart from making changes to the hardware, you also need to tweak the code a little. The first thing you will need to do is define which pins of the ESP8266 board the LEDs are connected to:

#define LED_PIN_UP 4
#define LED_PIN_DOWN 5

You also need to define a variable to hold the previous price and the threshold of amount of change we need to flash the LEDs:

float previousValue = 0.0;
float threshold = 0.05;

Set the LED pins as outputs in the setup() function of the code:

pinMode(LED_PIN_DOWN, OUTPUT);
pinMode(LED_PIN_UP, OUTPUT);

In the loop() section of the sketch check if it is the first time the code is being run. If it is, set previousvalue to be equal to the current Bitcoin price:

if (previousValue == 0.0) {

  previousValue = price;

}

Check if the price went down by a value more or equal to the threshold. If it did, flash the red LED.

if (price < (previousValue - threshold)) {

  // Flash LED
  digitalWrite(LED_PIN_DOWN, HIGH);
  delay(100);
  digitalWrite(LED_PIN_DOWN, LOW);
  delay(100);
  digitalWrite(LED_PIN_DOWN, HIGH);
  delay(100);
  digitalWrite(LED_PIN_DOWN, LOW);

}

Do the same for the green LED if the price has gone up:

if (price > (previousValue + threshold)) {

  // Flash LED
  digitalWrite(LED_PIN_UP, HIGH);
  delay(100);
  digitalWrite(LED_PIN_UP, LOW);
  delay(100);
  digitalWrite(LED_PIN_UP, HIGH);
  delay(100);
  digitalWrite(LED_PIN_UP, LOW);

}

Then set the previous price to be equal to the current price:

previousValue = price;

You can now upload the updated project and see it in operation. Either of LEDs should flash quickly depending on the fluctuation in price.

How to Go Further

In this project, we have managed to do several things: use the ESP8266 Wi-Fi board to get the price of bitcoin from a web API, display the price on an OLED screen and use LEDs to indicate price fluctuations.  However, that’s just a tip of the iceberg.

There are many other things that you can do with this project. For instance, you can tweak the setup to display the price of Bitcoin in more than one currency and add a switch button to change the displayed currency. You can also integrate everything into a nice PCB & build a fun product from it. Therefore, feel free and play around with the project and see what other cool stuff you can make it do.