Shares
Build a Physical Bitcoin Ticker With the ESP8266

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.

Build a Physical Bitcoin Ticker With the ESP8266

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:

Build a Physical Bitcoin Ticker With the ESP8266

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 
#include 
#include    
#include "SSD1306.h"

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:

Build a Physical Bitcoin Ticker With the ESP8266

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). Your setup should look like this:

Build a Physical Bitcoin Ticker With the ESP8266

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.

Want to learn more? Get my free eBook about the ESP8266!
The ESP8266 is an amazing WiFi chip that you can use to build all sorts of projects. Download today my free eBook "Build a WiFi Weather Station with the ESP8266". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
Eve Hunt a year ago
Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic. I really like what you have acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can’t wait to read far more from you. This is really a tremendous site
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Eve Hunt a year ago
Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely fantastic. I really like what you have acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can’t wait to read far more from you. This is really a tremendous site
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
CTB a year ago
Hello and thanks for the code, it works perfectly. How could I modify the code to display a different currency? Say EUR or GBP?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz CTB a year ago
Guess that would be quite easy, you can use an currency API for example https://fixer.io/
Tor 2 years ago
Thanks for this. I did this on my ESP8266-01 board. Without the leds of course.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
K.-J. Eich 2 years ago
Thanks for this project. I modified it for another OLED, but the main idea is great and it works from the very beginning. Is there any chance to modify for other cryptos like Ethereum? Thanks a lot.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
TicrThing 3 years ago
Hey, thanks for this post. We've been working on this concept for 3 years now and made a consumer-friendly version. We call it the TicrThing: your hands-free bitcoin companion.For those who like the idea, but don't have the time to build it yourself, you can now get this device at https://www.ticrthing.com .
Reply
Please enter your name.
Please enter your email.
Please enter your comment.