###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
}