Skip to content

Instantly share code, notes, and snippets.

@kawashimaken
Created May 22, 2019 01:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kawashimaken/553eedb46e42ab0bfbfabf2edbe5b396 to your computer and use it in GitHub Desktop.
#include <M5Stack.h>
#include <WiFi.h>
#include "ArduinoJson.h"
#include <HTTPClient.h>
//WIFIのID
const char *ssid = "";
//WIFIのパースワード、パスワードのないWIFIはこここのままでも良い
const char *password = "";
//今回ビットコインの値段を取得するためのAPIのURL
const char *apiServer = "https://api.coindesk.com/v1/bpi/currentprice/JPY.json";
// セットアップ
void setup()
{
// M5Stack objectの初期化
M5.begin();
// text の設定
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(GREEN);
M5.Lcd.setTextSize(3);
M5.Lcd.printf("Connecting to WIFI");
//
WiFi.begin(ssid, password);
//接続済 ではない場合
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
M5.Lcd.fillScreen(BLACK);
//点を表示する
M5.Lcd.printf(".");
}
//接続できた場合
M5.Lcd.clear(GREEN);
M5.Lcd.setTextColor(BLACK);
M5.Lcd.printf("connected!");
M5.Lcd.setTextColor(GREEN);
M5.Lcd.setTextSize(4);
}
// メインループ
void loop()
{
//WIFIの接続状態を確認する
//WIFI接続している場合
if ((WiFi.status() == WL_CONNECTED))
{
HTTPClient http;
//APIサーバに接続開始
http.begin(apiServer);
//HTTP Requestを作成する
int httpCode = http.GET();
//返り値を確認する
if (httpCode > 0)
{
//レスポンスを文字列として取得する
String payload = http.getString();
//正常の場合は200
//Serial.println(httpCode);
M5.Lcd.clear(BLACK);
M5.Lcd.setCursor(0, 0);
//jsonに変換するためにメモリを確保
DynamicJsonDocument doc(1024);
//payloadをjson objectに変換する
deserializeJson(doc, payload);
JsonObject obj = doc.as<JsonObject>();
//その中のbpi.JPYを取り出す
JsonObject result = obj[String("bpi")][String("JPY")];
//ビットコインの価格を取り出す、同時に文字列に変換しておく
String rate = result[String("rate")];
//表示する、一回char* から 文字列への変換が必要です。
M5.Lcd.printf(String(rate).c_str());
}
else
{
M5.Lcd.printf("HTTP request error!");
return;
}
M5.update();
//リソースを解放する
http.end();
delay(5000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment