mpg28
Published

Bitcoin Ticker

Display the live price of Bitcoin

BeginnerFull instructions provided526
Bitcoin Ticker

Things used in this project

Story

Read more

Schematics

Bitcoin Ticker

Code

Bitcoin Ticker

Arduino
// Bitcoin Ticker with smiley face.
// with Voltage Divider (2x 10K resistor)
/*
  Resistors are aligned in series.
  One end goes to Battery - and also to Arduino GND
  The other goes to Battery + and also to Arduino Vin
  The middle (connection between two resistors) goes to Arduino A0
*/


#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h> 

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27,16,2);

int previousValue = 0;
int threshold = 1;
int lastKnown = 0;

// WiFi settings
const char* ssid     = "Your SSID";
const char* password = "Your Password";

// API server
const char* host = "api.coindesk.com";



void setup() {

  lcd.init();          // Initiate the LCD module
  lcd.backlight();     // Turn on the backlight 
  
  
  
  // Serial
  Serial.begin(115200);
  delay(10);

 
                                       // We start by connecting to a WiFi 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());

}
     
void loop() {

                                     // Connect to API
  Serial.print("connecting to ");
  Serial.println(host);
  
                                           // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
                                           // We now create a URI for the request
  String url = "/v1/bpi/currentprice.json";
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
                                             // This will 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");
  delay(100);
  
                                             // Read all the lines of the reply from server and print them to Serial
  String answer;
  while(client.available()){
    String line = client.readStringUntil('\r');
    answer += line;
  }

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

  // Process answer
  Serial.println();
  Serial.println("Answer: ");
  Serial.println(answer);

  // Convert to JSON
  String jsonAnswer;
  int jsonIndex;

  for (int i = 0; i < answer.length(); i++) {
    if (answer[i] == '{') {
      jsonIndex = i;
      break;
    }
  }

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

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

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

 // lcd.begin(16, 2);                 // Initialize 16x2 LCD Display
  lcd.clear();
 
  lcd.setCursor(0, 0);
  lcd.print("Bitcoin!!");
       lcd.setCursor(11, 0);
       lcd.print("=)");
       lcd.setCursor(1, 1);
       lcd.print("$");
       if (price > 5000) {lastKnown = price;}       // Is price greater than 5000, save last price.
    if (price == 0) {lcd.print(lastKnown);}         // Print BTC price
      else lcd.print(lastKnown);
      
  

// This is to measure the voltage

  int sensorValue = analogRead(A0); //read the A0 pin value
  float voltage = sensorValue * (3.00 / 1023.00) * 2; //convert the value to a true voltage.
  
  lcd.setCursor(11, 1);
  lcd.print(voltage);
  lcd.print("v");


 
 
  // Wait 1 seconds
  delay(1000);
}

Credits

mpg28
7 projects • 2 followers

Comments