Newer
Older
M5StickCPlus_FactoryTest2022 / SampleSrc / httpclient01.ino
@motoki miura motoki miura on 10 May 2022 1004 bytes SampleSource
#include <WiFi.h>
#include <HTTPClient.h> // ステータスコードの定義もここにある

const char* ssid = "miura2g";
const char* password = "jikken2022";

// 天気予報API https://weather.tsukumijima.net/ から、千葉の天気を取得 
const char* weatherapi_url = "http://weather.tsukumijima.net/api/forecast/city/120010";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { // 接続中...
    delay(50);
    Serial.print(".");
  }
  delay(1000);
  
  HTTPClient http;            // クライアント作成
  http.begin(weatherapi_url); // HTTPでサーバに接続
  int httpCode = http.GET();  // ステータスコードを取得
  if (httpCode > 0) {
      Serial.println(httpCode);
    if (httpCode == HTTP_CODE_OK) { // ステータスコードが「成功」(200) なら
      String payload = http.getString();
      Serial.println(payload);
    }
  }
  http.end();
}

void loop() {
}