#include <M5Unified.h>
#include <WiFi.h>
#include <HTTPClient.h> // ステータスコードの定義もここにある
const char* ssid = "ics-ap";
const char* password = "jikkenics";
// 天気予報API https://weather.tsukumijima.net/ から、千葉の天気を取得
const char* weatherapi_url = "https://weather.tsukumijima.net/api/forecast/city/120010";
void setup() {
auto cfg = M5.config();
cfg.serial_baudrate = 115200;
M5.begin(cfg);
M5.Display.setRotation(3); //横向き
M5.Display.setFont(&fonts::lgfxJapanGothic_16);
M5.Display.println("webclient01 ボタンを押すと天気を取得");
M5.Display.setTextScroll(true);
M5.Display.fillScreen(BLACK);
M5.Display.setTextColor(WHITE,BLACK);
WiFi.begin(ssid, password); // 接続開始
while (WiFi.status() != WL_CONNECTED) { // 接続中...
M5.Display.print(".");
M5.delay(500);
}
// 接続完了!!
M5.Display.fillScreen(GREEN);
M5.Display.setCursor(0, 30, 4);//x,y,fonttype
M5.Display.setTextColor(BLACK, GREEN);
M5.Display.print(" Wifi Connected!\n");
String gotip = WiFi.localIP().toString(); // m5デバイスのIPアドレス
M5.Display.println(gotip);
M5.Display.println("BtnA to get weather");
M5.delay(1500);
}
void loop() {
M5.update();
if (M5.BtnA.wasReleased()) {
HTTPClient http; // クライアント作成
http.begin(weatherapi_url); // HTTPでサーバに接続
int httpCode = http.GET(); // ステータスコードを取得
if (httpCode > 0) {
Serial.println(httpCode);
M5.Display.setTextColor(GREEN,BLACK);
M5.Display.printf("status code = %d\n ", httpCode);
if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら
String payload = http.getString();
Serial.println(payload);
}
}
http.end();
}
M5.delay(20);
}