Bitcoin Price Clock


#1

###Today I made a Bitcoin Price Clock using a Particle Photon, my new Particle Shield Shield, and a LCD Keypad Shield that I had lying around.

Here is a picture of the clock:

Here is the code for the Photon (the LiquidCrystal library is required):

#include "application.h"
#include "LiquidCrystal.h"

/*******************************************************

Display the Bitcoin price in Dollars and the time on an LCD
Nathan Robinson, October 2016

********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(A5, A4, D6, D0, D1, D7);


String getAskPrice(String data)                                 // This function gets the value in the "ask:" part of the JSON.
{
    int kLocation = data.indexOf("k");                          // Find where the "k" of ask appears
    int commaLocation = data.indexOf(",", kLocation);           // Find where the comma after the value appears

   String value = data.substring(kLocation +4, commaLocation);  // The value is from 4 characters after where "k" is and right before where the comma is.
    return value;                                               // Return the ask price of Bitcoin
}

void bitcoinHandler(const char *event, const char *data)  // This is called when the Photon recives the webhook data.
{
 Serial.println(data);                                    // Print the data to serial for debugging
 String ask = String(getAskPrice(data));                  // Get the ask price of Bitcoin
 Serial.println(ask);                                     // Print it to serial
 lcd.setCursor(0, 1);                                     // Set the cursor to the second line
 lcd.print("$");                                          // Clear the second line
 lcd.print(ask);                                          // Print the price of bitcoin
 lcd.print(" ");                                          // Print " " after
}

void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 lcd.print("Price:  Time:"); // print a simple message

 Particle.publish("bitcoin");  // Trigger bitcoin webhook

Particle.subscribe("hook-response/bitcoin/0", bitcoinHandler, MY_DEVICES);  // Subscribe to the webhook response
Time.zone(-4);
Serial.begin(9600);
}

void loop()
{
if (millis()/1000 % 10 == 0) // Every ten seconds
{
  Particle.publish("bitcoin");  // Trigger bitcoin webhook
}

 lcd.setCursor(8,1);            // move cursor to second line "1" and 8 spaces over

 if (Time.hour() < 10)
 {
   lcd.print("0");
   lcd.print(Time.hour());
 }
 else
 {
    lcd.print(Time.hour());
 }

 lcd.print(":");

 if (Time.minute() < 10)
 {
   lcd.print("0");
   lcd.print(Time.minute());
 }
 else
 {
    lcd.print(Time.minute());
 }

 lcd.print(":");

 if (Time.second() < 10)
 {
   lcd.print("0");
   lcd.print(Time.second());
 }
 else
 {
    lcd.print(Time.second());
 }
}

The Photon relies on a webhook to get data from the Bitcoin Average API:

{
  "eventName": "bitcoin",
  "url": "https://api.bitcoinaverage.com/ticker/global/USD/",
  "requestType": "GET",
  "mydevices": true
}

I hope you enjoyed my project of the day and I hope to make more random projects like this.


Bitcoin Cash real time analog meter
IOTA, Not: Bitcoin, Ethereum, Litecoin
#2

Building this, I need a screen though, Kind of want to OLED it…BTW isn’t the shieldshield a sexy piece of kit…black solder mask, mmmmm.


#3

Thanks for sharing this! I look forward to what you build next @nrobinson2000!


#4

UPDATE:

It’s been a year and Bitcoin is on track to increase tenfold of what it was when I first shared this.

Here is the updated webhook:

{
    "event": "bitcoin",
    "url": "https://blockchain.info/ticker?cors=true",
    "requestType": "GET",
    "responseTemplate": "{{USD.last}}",
    "mydevices": true
}

And here is the updated code for the photon:

#include "Particle.h"
#include "LiquidCrystal/LiquidCrystal.h"

/*******************************************************

Display the Bitcoin price in Dollars and the time on an LCD
Nathan Robinson, October 2016

Updated in October 2017, to use new API and accommodate new price

********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(A5, A4, D6, D0, D1, D7);

void initLCD() {
  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  lcd.print("Price $:   Time:"); // print
  Time.zone(-4);
  Serial.begin(9600);
}

STARTUP(initLCD());

void bitcoinHandler(const char *event, const char *data) {  // This is called when the Photon recives the webhook data.
 Serial.println(data);                                    // Print the data to serial for debugging
 String latest = String(data);                  // Get the latest price of Bitcoin
 Serial.println(latest);                                     // Print it to serial
 lcd.setCursor(0, 1);                                     // Set the cursor to the second line
 lcd.print(latest);                                          // Print the price of bitcoin
 lcd.print(" ");                                          // Print a dividing space after
}

void setup() {
  Particle.publish("bitcoin");  // Trigger bitcoin webhook
  Particle.subscribe("hook-response/bitcoin", bitcoinHandler, MY_DEVICES);  // Subscribe to the webhook response
}

void loop() {
  if (millis()/1000 % 10 == 0) // Every ten seconds
  {
    Particle.publish("bitcoin");  // Trigger bitcoin webhook
  }
  // Print the time
  lcd.setCursor(8,1);            // move cursor to second line "1" and 8 spaces over

  if (Time.hour() < 10) {
    lcd.print("0");
    lcd.print(Time.hour());
  }
  else {
    lcd.print(Time.hour());
  }

  lcd.print(":");

  if (Time.minute() < 10) {
    lcd.print("0");
    lcd.print(Time.minute());
  }
  else {
    lcd.print(Time.minute());
  }

  lcd.print(":");

  if (Time.second() < 10) {
    lcd.print("0");
    lcd.print(Time.second());
  }
  else {
    lcd.print(Time.second());
  }
}