5
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

M5Stackでビットコインの価格を表示

M5Stackでビットコインの価格を表示してみた。

aliexpressのM5Stackのストアのページに写真が載っているビットコイン価格を表示するアプリを試してみました。

ビットコイン価格のアプリを表示するアプリのソースコードはgithubにありました。
https://github.com/dankelley2/M5Stack_BTCTicker

そのままだと動かなかったので、現在のArduino IDEだと発生するエラー修正、SDメモリの読み書きをしない、などの変更しました。

#include <M5Stack.h>

WiFiMulti WiFiMulti;

int status = WL_IDLE_STATUS;
int lastPrice = 0;
int currentPrice;
int minPrice = 9999999; 
int maxPrice = 0;
char servername[] = "api.coindesk.com";
String answer;
WiFiClient client;

// the setup routine runs once when M5Stack starts up
void setup(){
  WiFiMulti.addAP("SSID", "PASS");
  Serial.begin(115200);

  // Initialize the M5Stack object
  M5.begin();

  // LCD display
  M5.Lcd.println("bitcoin");

  while (WiFiMulti.run() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.printf(".");
  }
  M5.Lcd.println("wifi connect ok");

  ConnectToClient();
}

void ConnectToClient(){
  if (client.connect(servername, 80)) {
    // Make a HTTP request:
    client.print(String("GET ") + "/v1/bpi/currentprice.json HTTP/1.1\r\n" +
                 "Host: api.coindesk.com\r\n" +
                 "Connection: close\r\n\r\n");
  }
}

// the loop routine runs over and over again forever
void loop() {

  if (client.available()) {
    char c = client.read();
    answer += c;
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    m5.update();
    client.stop();

    String jsonAnswer;
    int jsonIndex;

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

    jsonAnswer = answer.substring(jsonIndex);
    jsonAnswer.trim();

    int rateIndex = jsonAnswer.indexOf("rate_float");
    String priceString = jsonAnswer.substring(rateIndex + 12, rateIndex + 19);
    Serial.println(jsonAnswer);
    Serial.println("");
    Serial.println(priceString);
    priceString.trim();
    int decimalplace = priceString.indexOf(".");
    String Dollars = priceString.substring(0, decimalplace);
    String Cents = priceString.substring(decimalplace+1);
    while (Cents.length() < 2) {
      Cents += "0";
    }
    String Amount = "$" + Dollars + "." + Cents;

    currentPrice = (Dollars + Cents).toInt();
    minPrice = min(minPrice,currentPrice);
    maxPrice = max(maxPrice,currentPrice);

    m5.Lcd.fillScreen(0x0000);
    m5.Lcd.setFont(&FreeSans9pt7b);

    m5.Lcd.setTextColor(RED);
    m5.Lcd.setCursor(20, 20);
    m5.Lcd.printf(("Min: " + String(minPrice).substring(0,Dollars.length()) + "." + String(minPrice).substring(Dollars.length())).c_str());

    m5.Lcd.setTextColor(GREEN);
    m5.Lcd.setCursor(205, 20);
    m5.Lcd.printf(("Max: " + String(maxPrice).substring(0,Dollars.length()) + "." + String(maxPrice).substring(Dollars.length())).c_str());

    m5.Lcd.setTextColor(WHITE);
    m5.Lcd.setCursor(30, 80);
    m5.Lcd.setFont(&FreeMonoBold24pt7b);
    m5.Lcd.printf("BTC Price");
    m5.Lcd.printf("\r\n");

    m5.Lcd.setCursor(50, 140);
    m5.Lcd.printf(Amount.c_str());

    if (currentPrice >= lastPrice) //UP
    {
      m5.Lcd.fillTriangle(140, 205, 180, 205, 160, 180, GREEN);
    }
    else if (currentPrice < lastPrice) //Down
    {
      m5.Lcd.fillTriangle(140, 205, 180, 205, 160, 230, RED);
    }

    lastPrice = currentPrice;

    // wait 20 seconds and key check
    for (int i = 0; i < 20; i++){
      if(M5.BtnA.wasPressed()) {
        m5.lcd.setBrightness(0);
      }
      if(M5.BtnB.wasPressed()) {
        m5.lcd.setBrightness(25);
      }
      if(M5.BtnC.wasPressed()) {
        m5.lcd.setBrightness(150);
      }
      m5.update();
      delay(1000);
    }
    answer = "";
    Amount = "";
    currentPrice = 0;
    ConnectToClient();
  }
}

メモ

coideskのAPIの価格更新は1分間隔です。

M5Stack store の表示例 と githubのコードの表示が微妙に違っているる。

apiで取得したjson

{
    "bpi": {
        "EUR": {
            "code": "EUR", 
            "description": "Euro", 
            "rate": "7,131.6382", 
            "rate_float": 7131.6382, 
            "symbol": "&euro;"
        }, 
        "GBP": {
            "code": "GBP", 
            "description": "British Pound Sterling", 
            "rate": "6,350.6633", 
            "rate_float": 6350.6633, 
            "symbol": "&pound;"
        }, 
        "USD": {
            "code": "USD", 
            "description": "United States Dollar", 
            "rate": "8,827.9675", 
            "rate_float": 8827.9675, 
            "symbol": "&#36;"
        }
    }, 
    "chartName": "Bitcoin", 
    "disclaimer": "This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org", 
    "time": {
        "updated": "Feb 14, 2018 06:05:00 UTC", 
        "updatedISO": "2018-02-14T06:05:00+00:00", 
        "updateduk": "Feb 14, 2018 at 06:05 GMT"
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
5
Help us understand the problem. What are the problem?