0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

前の記事

LEDチカ編

Arudiono IDEで使う

Wi-Fiに接続する

スケッチ

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>

WiFiMulti wifiMulti;

const String wifiSSID = "SSID";
const String wifiPassword = "PASSWORD";
const int wifiConnectCheckTerm = 500; //  Wi-Fi接続チェック時間(ms)

const int loopTerm = 50000; //  Wi-Fi接続チェック時間(ms)
const String connectServer = "http://example.com/index.html";

void setup() {

  Serial.begin(115200);

  wifiMulti.addAP(wifiSSID.c_str(), wifiPassword.c_str());

  Serial.print("Connect To Wi-Fi");
  // 最も条件の良いWi-Fiに自動接続
  while (wifiMulti.run() != WL_CONNECTED) {
    delay(wifiConnectCheckTerm);
    Serial.print(".");
  }
  
  Serial.println("Connected Wi-Fi.");
  Serial.print("接続先SSID: ");
  Serial.println(WiFi.SSID());
}

loop(){
}

#include
#include
#include
を使用します。

httpサイトに接続する

loop()で実装します。

スケッチ

void loop() {

  if ((wifiMulti.run() == WL_CONNECTED)) {

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");

    http.begin(connectServer);  //HTTP

    Serial.print("[HTTP] GET...\n");
    int httpCode = http.GET();

    // httpCode will be negative on error
    if (httpCode > 0) {
      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        Serial.println(payload);
      } else {
        // HTTP ヘッダの応答コード
        Serial.printf("[HTTP] GET... Error Code: %d\n", httpCode);
      }

    } else {
      Serial.printf("[HTTP] GET Success. return code: %d\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }

  delay(loopTerm);
}

http.begin()で接続する。オブジェクトは{}スコープ内で宣言しています。

最後に

スケッチコードはCC BY-SA 4.0(著作者の情報とCCライセンス継承はお願いします。商用利用・改変・再配布は問題なし)です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?