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 = "[Link]";
void setup() {
[Link](); // Initiate the LCD module
[Link](); // Turn on the backlight
// Serial
[Link](115200);
delay(10);
// We start by connecting to a WiFi network
[Link]();
[Link]();
[Link]("Connecting to ");
[Link](ssid);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("");
[Link]("WiFi connected");
[Link]("IP address: ");
[Link]([Link]());
void loop() {
// Connect to API
[Link]("connecting to ");
[Link](host);
// Use WiFiClient class to create TCP
connections
WiFiClient client;
const int httpPort = 80;
if () {
[Link]("connection failed");
return;
}
// We now create a URI for the request
String url = "/v1/bpi/[Link]";
[Link]("Requesting URL: ");
[Link](url);
// This will send the request to the
server
[Link](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([Link]()){
String line = [Link]('\r');
answer += line;
}
[Link]();
[Link]();
[Link]("closing connection");
// Process answer
[Link]();
[Link]("Answer: ");
[Link](answer);
// Convert to JSON
String jsonAnswer;
int jsonIndex;
for (int i = 0; i < [Link](); i++) {
if (answer[i] == '{') {
jsonIndex = i;
break;
}
}
// Get JSON data
jsonAnswer = [Link](jsonIndex);
[Link]();
[Link]("JSON answer: ");
[Link](jsonAnswer);
[Link]();
// Get rate as float
int rateIndex = [Link]("rate_float");
String priceString = [Link](rateIndex + 12, rateIndex + 18);
[Link]();
int price = [Link]();
// Print price
[Link]();
[Link]("Bitcoin price: ");
[Link](price);
// [Link](16, 2); // Initialize 16x2 LCD Display
[Link]();
[Link](0, 0);
[Link]("Bitcoin!!");
[Link](11, 0);
[Link]("$ USD");
[Link](1, 1);
[Link]("$");
if (price > 5000) {lastKnown = price;} // Is price greater than 5000,
save last price.
if (price == 0) {[Link](lastKnown);} // Print BTC price
else [Link](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.
[Link](11, 1);
[Link](voltage);
[Link]("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 - [Link]
const String url = "[Link]
const String historyURL = "[Link]
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()
{
[Link](115200);
//Start the serial monitor
pinMode(upLED, OUTPUT);
//Define the LED pin outputs
pinMode(downLED, OUTPUT);
if ()
//Connect to the display
{
[Link](F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
[Link]();
//Clear the display
[Link](SSD1306_WHITE); //Set
the text colour to white
[Link](0, 0, bitcoinLogo, 128, 64, WHITE);
//Display bitmap from array
[Link]();
delay(2000);
[Link]();
//Clear the display
[Link](1); //Set
display parameters
[Link](WHITE);
[Link]("Connecting to WiFi...");
[Link]();
[Link](ssid, password);
[Link]("Connecting to WiFi...");
while ([Link]() != WL_CONNECTED)
//Connect to the WiFi network
{
delay(500);
[Link](".");
}
[Link]();
[Link]("Connected to: ");
//Display message once connected
[Link](ssid);
[Link]();
delay(1500);
[Link]();
[Link]();
}
void loop()
{
[Link]("Connecting to ");
//Display url on Serial monitor for debugging
[Link](url);
[Link](url);
int httpCode = [Link]();
//Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, [Link]());
if (error)
//Display error message if unsuccessful
{
[Link](F("deserializeJson Failed"));
[Link](error.f_str());
delay(2500);
return;
}
[Link]("HTTP Status Code: ");
[Link](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>();
[Link]();
[Link]("Getting history...");
StaticJsonDocument<2000> historyDoc;
[Link](historyURL);
//Get historical crypto price from API
int historyHttpCode = [Link]();
DeserializationError historyError = deserializeJson(historyDoc,
[Link]());
if (historyError) {
//Display error message if unsuccessful
[Link](F("deserializeJson(History) failed"));
[Link](historyError.f_str());
delay(2500);
return;
}
[Link]("History HTTP Status Code: ");
[Link](historyHttpCode);
JsonObject bpi = historyDoc["bpi"].as<JsonObject>();
double yesterdayPrice;
for (JsonPair kv : bpi) {
yesterdayPrice = [Link]().as<double>();
//Store yesterday's crypto price
}
[Link]("BTCUSD Price: ");
//Display current price on serial monitor
[Link]([Link]());
[Link]("Yesterday's Price: ");
//Display yesterday's price on serial monitor
[Link](yesterdayPrice);
bool isUp = [Link]() > yesterdayPrice;
//Check whether price has increased or decreased
double percentChange;
String dayChangeString = "24hr Change:";
if (isUp)
//If price has increased from yesterday
{
percentChange = (([Link]() - yesterdayPrice) / yesterdayPrice) *
100;
digitalWrite(upLED, HIGH);
digitalWrite(downLED, LOW);
}
else
//If price has decreased from yesterday
{
percentChange = ((yesterdayPrice - [Link]()) / yesterdayPrice) *
100;
dayChangeString = dayChangeString + "-";
digitalWrite(downLED, HIGH);
digitalWrite(upLED, LOW);
}
[Link]("Percent Change: ");
//Display the percentage change on the serial monitor
[Link](percentChange);
[Link]();
//Clear the OLED display
[Link](1);
printCenter("BTC/USD", 0, 0);
//Display the comparison header
[Link](2);
printCenter("$" + BTCUSDPrice, 0, 20);
//Display the current price
[Link](1);
//Display the change percentage
dayChangeString = dayChangeString + percentChange + "%";
printCenter(dayChangeString, 0, 55);
[Link]();
//Execute the new display
[Link]();
//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;
[Link](buf, x, y, &x1, &y1, &w, &h);
//Calculate string width
[Link]((x - w / 2) + (128 / 2), y); //Set
cursor to print string in centre
[Link](buf); //Display
string
}