Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24 hour percentage // manual? #3

Open
TechSplendid opened this issue Aug 2, 2021 · 3 comments
Open

24 hour percentage // manual? #3

TechSplendid opened this issue Aug 2, 2021 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@TechSplendid
Copy link

Maybe not a issue, but do you have to change this code each day to get the right percentage in the code below?

//Display 24hr. Percent Change
double yesterdayPrice = historyDoc["bpi"]["2021-03-20"].as();

@AndrewBudziszek
Copy link
Owner

@Tenderlaan54 Hi! Thanks for checking out this project. Yeah, as of right now it is manual. If you're interested, you could look into something like this https://randomnerdtutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/ to help get the date and generate the string to put in for the lookup.

If you do, please submit a merge request and I'd be more than happy to add your code to the project!

@AndrewBudziszek AndrewBudziszek added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Aug 2, 2021
@TechSplendid
Copy link
Author

I tried but I can't get the correct format of the date. The format in the link you gave is: 2021-8-3 instead of 2021-08-03.

Isn't it possible and easier to get this info from the api call?

@TechSplendid
Copy link
Author

I don't know how to submit a merge request. So i will post my code here below.
You will need this library that i modified for this. Maybe there are better way's butt i am new @ coding.

////// my library //////
https://github.com/TechSplendid/ESP8266-NTP-Yesterday-library

///// The code i use /////

#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "NTPClientYesterday.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
//#include "secrets.h" // WiFi Configuration (WiFi name and Password)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//const char* ssid = SECRET_SSID;
//const char* password = SECRET_WIFI_PASSWORD;

const char* ssid = "SECRET_SSID";
const char* password = "SECRET_WIFI_PASSWORD";

const long utcOffsetInSeconds = -28800;

// 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";

// 'icons8-bitcoin-24', 24x24px
const unsigned char bitcoinIcon [] PROGMEM = {
0x00, 0x7e, 0x00, 0x03, 0xff, 0xc0, 0x07, 0x81, 0xe0, 0x0e, 0x00, 0x70, 0x18, 0x28, 0x18, 0x30,
0x28, 0x0c, 0x70, 0xfc, 0x0e, 0x60, 0xfe, 0x06, 0x60, 0xc7, 0x06, 0xc0, 0xc3, 0x03, 0xc0, 0xc7,
0x03, 0xc0, 0xfe, 0x03, 0xc0, 0xff, 0x03, 0xc0, 0xc3, 0x83, 0xc0, 0xc1, 0x83, 0x60, 0xc3, 0x86,
0x60, 0xff, 0x06, 0x70, 0xfe, 0x0e, 0x30, 0x28, 0x0c, 0x18, 0x28, 0x18, 0x0e, 0x00, 0x70, 0x07,
0x81, 0xe0, 0x03, 0xff, 0xc0, 0x00, 0x7e, 0x00
};

HTTPClient http;
String lastPrice;

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

void setup() {
Serial.begin(115200);

timeClient.begin();

// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(7200); // Netherlands

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

display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Connecting to WiFi...");
display.display();

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}

Serial.print("CONNECTED to SSID: ");
Serial.println(ssid);

display.print("Connected to ");
display.println(ssid);
display.display();
delay(5000);
}

void loop() {

timeClient.update();

if (WiFi.status() == WL_CONNECTED) {
Serial.println("Getting current data...");

http.begin(url);
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
if (httpCode > 0) {
  StaticJsonDocument<768> doc;
  DeserializationError error = deserializeJson(doc, http.getString());

  if (error) {
    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>();
  if(BTCUSDPrice == lastPrice) {
    Serial.print("Price hasn't changed (Current/Last): ");
    Serial.print(BTCUSDPrice);
    Serial.print(" : ");
    Serial.println(lastPrice);
    delay(1250);
    return;
  } else {
    lastPrice = BTCUSDPrice;
  }
  String lastUpdated = doc["time"]["updated"].as<String>();
  http.end();

  Serial.println("Getting history...");
  StaticJsonDocument<1536> historyDoc;
  http.begin(historyURL);
  int historyHttpCode = http.GET();
  DeserializationError historyError = deserializeJson(historyDoc, http.getString());

  if (historyError) {
    Serial.print(F("deserializeJson(History) failed: "));
    Serial.println(historyError.f_str());
    delay(2500);
    return;
  }

  //Display Header

// display.clearDisplay();
// display.setTextSize(1);
// printCenter("BTC/USD", 0, 0);
display.clearDisplay();
display.drawBitmap((128/2) - (24/2), 0, bitcoinIcon, 24, 24, WHITE);
display.display();

  //Display BTC Price
  display.setTextSize(2);
  printCenter("$" + BTCUSDPrice, 0, 32);

  //Display 24hr. Percent Change
  double yesterdayPrice = historyDoc["bpi"][timeClient.getFormattedDate()].as<double>();
  bool isUp = BTCUSDPrice.toDouble() > yesterdayPrice;
  double percentChange;
  String dayChangeString = "24hr. Change: ";
  if (isUp) {
    percentChange = ((BTCUSDPrice.toDouble() - yesterdayPrice) / yesterdayPrice) * 100;
  } else {
    percentChange = ((yesterdayPrice - BTCUSDPrice.toDouble()) / yesterdayPrice) * 100;
    dayChangeString = dayChangeString + "-";
  }
  display.setTextSize(1);
  dayChangeString = dayChangeString + percentChange + "%";
  printCenter(dayChangeString, 0, 55);

  display.display();
  http.end();
}
delay(1250);

}
}

void printCenter(const String buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((x - w / 2) + (128 / 2), y);
display.print(buf);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants