0% found this document useful (0 votes)
38 views

Sketch

- The document describes code for displaying the Bitcoin price and voltage reading on a 16x2 LCD using an Arduino and ESP8266 WiFi module. - It connects to a CoinDesk API to retrieve the current Bitcoin price in USD and displays it on the LCD along with a smiley face graphic. - It also measures the voltage input from a voltage divider circuit and displays the reading on the second LCD line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Sketch

- The document describes code for displaying the Bitcoin price and voltage reading on a 16x2 LCD using an Arduino and ESP8266 WiFi module. - It connects to a CoinDesk API to retrieve the current Bitcoin price in USD and displays it on the LCD along with a smiley face graphic. - It also measures the voltage input from a voltage divider circuit and displays the reading on the second LCD line.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

bitcoin LCD 16x2

// 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 = "Pergunta";
const char* password = "Eurespondo";

// 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("$ USD");
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);
}

-----------------------------------------------------------------------------------
--------------------------------------------------

Biticoin_LED_SSD1306
#include <Adafruit_SSD1306.h>
//Include the required libraries
#include <WiFi.h>
#include <Wire.h>
#include <HTTPClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>

#define SCREEN_WIDTH 128


//Define the OLED display width and height
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 //
Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C //I2C
address for display
#define upLED 18
#define downLED 19
Adafruit_SSD1306 display (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//Create the display object

const char* ssid = "Pergunta";


const char* password = "Eurespondo";

const int httpsPort = 443;


//Bitcoin price API powered by CoinDesk - https://www.coindesk.com/price/bitcoin
const String url = "http://api.coindesk.com/v1/bpi/currentprice/BTC.json";
const String historyURL = "http://api.coindesk.com/v1/bpi/historical/close.json";
const String cryptoCode = "BTC";

WiFiClient client;
//Create a new WiFi client
HTTPClient http;

String formattedDate;
//Create variables to store the date and time
String dayStamp;
String timeStamp;

const unsigned char bitcoinLogo [] PROGMEM = //


'Bitcoin Logo', 128x64px
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0xe7, 0xc0, 0x07, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x03, 0xe7, 0xc0, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x02, 0x24, 0x40, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x02, 0x24, 0x40, 0x00, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x02, 0x24, 0x40, 0x00, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x24, 0x40, 0x00, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x02, 0x3c, 0x40, 0x00, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0xfe, 0x3c, 0x7c, 0x00, 0x0e, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x1f, 0x00, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0e, 0x03, 0x00, 0x00, 0x01, 0xc0, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x03, 0xf0, 0x3f, 0x80, 0x60, 0x03, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0xf0, 0x3f, 0xe0, 0x20, 0x01, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x18, 0x30, 0x70, 0x30, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x18, 0x30, 0x10, 0x30, 0x00, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x18, 0x30, 0x10, 0x30, 0x00, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0x10, 0x30, 0x00, 0xe0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0x30, 0x20, 0x00, 0xe0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0xe0, 0x60, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x3f, 0xc0, 0xc0, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x00, 0x01, 0xc0, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x00, 0x00, 0xf0, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x00, 0x00, 0x38, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x3f, 0xe0, 0x18, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x18, 0x30, 0xf8, 0x0c, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0x1c, 0x0c, 0x00, 0x60, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0x0c, 0x0c, 0x00, 0xe0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x18, 0x30, 0x04, 0x0c, 0x00, 0xe0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x18, 0x30, 0x0c, 0x0c, 0x00, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x18, 0x30, 0x0c, 0x0c, 0x00, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x38, 0x00, 0x18, 0x30, 0x38, 0x0c, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x18, 0x01, 0xf0, 0x3f, 0xf0, 0x08, 0x01, 0xc0, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x01, 0xe0, 0x1f, 0x00, 0x18, 0x01, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1c, 0x01, 0x00, 0x00, 0x00, 0x30, 0x03, 0x80, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0e, 0x03, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x0f, 0xc0, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0xfe, 0x3c, 0x7e, 0x00, 0x0e, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x02, 0x3c, 0x40, 0x00, 0x0c, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x02, 0x24, 0x40, 0x00, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0xc0, 0x02, 0x24, 0x40, 0x00, 0x38, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x02, 0x24, 0x40, 0x00, 0x70, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x02, 0x24, 0x40, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x03, 0xe7, 0xc0, 0x01, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x03, 0xe7, 0xc0, 0x07, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};

void setup()
{
Serial.begin(115200);
//Start the serial monitor
pinMode(upLED, OUTPUT);
//Define the LED pin outputs
pinMode(downLED, OUTPUT);

if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
//Connect to the display
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}

display.clearDisplay();
//Clear the display
display.setTextColor(SSD1306_WHITE); //Set
the text colour to white
display.drawBitmap(0, 0, bitcoinLogo, 128, 64, WHITE);
//Display bitmap from array
display.display();
delay(2000);

display.clearDisplay();
//Clear the display
display.setTextSize(1); //Set
display parameters
display.setTextColor(WHITE);
display.println("Connecting to WiFi...");
display.display();

WiFi.begin(ssid, password);

Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED)
//Connect to the WiFi network
{
delay(500);
Serial.print(".");
}
Serial.println();

display.println("Connected to: ");


//Display message once connected
display.print(ssid);
display.display();
delay(1500);
display.clearDisplay();
display.display();
}

void loop()
{
Serial.print("Connecting to ");
//Display url on Serial monitor for debugging
Serial.println(url);

http.begin(url);
int httpCode = http.GET();
//Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error)
//Display error message if unsuccessful
{
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
return;
}

Serial.print("HTTP Status Code: ");


Serial.println(httpCode);

String BTCUSDPrice = doc["bpi"]["USD"]["rate_float"].as<String>();


//Store crypto price and update date in local variables
String lastUpdated = doc["time"]["updated"].as<String>();
http.end();

Serial.print("Getting history...");
StaticJsonDocument<2000> historyDoc;
http.begin(historyURL);
//Get historical crypto price from API
int historyHttpCode = http.GET();
DeserializationError historyError = deserializeJson(historyDoc,
http.getString());

if (historyError) {
//Display error message if unsuccessful
Serial.print(F("deserializeJson(History) failed"));
Serial.println(historyError.f_str());
delay(2500);
return;
}

Serial.print("History HTTP Status Code: ");


Serial.println(historyHttpCode);
JsonObject bpi = historyDoc["bpi"].as<JsonObject>();
double yesterdayPrice;
for (JsonPair kv : bpi) {
yesterdayPrice = kv.value().as<double>();
//Store yesterday's crypto price
}

Serial.print("BTCUSD Price: ");


//Display current price on serial monitor
Serial.println(BTCUSDPrice.toDouble());

Serial.print("Yesterday's Price: ");


//Display yesterday's price on serial monitor
Serial.println(yesterdayPrice);

bool isUp = BTCUSDPrice.toDouble() > yesterdayPrice;


//Check whether price has increased or decreased
double percentChange;
String dayChangeString = "24hr Change:";
if (isUp)
//If price has increased from yesterday
{
percentChange = ((BTCUSDPrice.toDouble() - yesterdayPrice) / yesterdayPrice) *
100;
digitalWrite(upLED, HIGH);
digitalWrite(downLED, LOW);
}
else
//If price has decreased from yesterday
{
percentChange = ((yesterdayPrice - BTCUSDPrice.toDouble()) / yesterdayPrice) *
100;
dayChangeString = dayChangeString + "-";
digitalWrite(downLED, HIGH);
digitalWrite(upLED, LOW);
}

Serial.print("Percent Change: ");


//Display the percentage change on the serial monitor
Serial.println(percentChange);

display.clearDisplay();
//Clear the OLED display
display.setTextSize(1);
printCenter("BTC/USD", 0, 0);
//Display the comparison header

display.setTextSize(2);
printCenter("$" + BTCUSDPrice, 0, 20);
//Display the current price

display.setTextSize(1);
//Display the change percentage
dayChangeString = dayChangeString + percentChange + "%";
printCenter(dayChangeString, 0, 55);
display.display();
//Execute the new display

http.end();
//End the WiFi connection
esp_sleep_enable_timer_wakeup(900000000);
//Sleep for 15 minutes
}

void printCenter(const String buf, int x, int y)


//Function to centre the current price in the display width
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h);
//Calculate string width
display.setCursor((x - w / 2) + (128 / 2), y); //Set
cursor to print string in centre
display.print(buf); //Display
string
}

You might also like