WiFiClientBasic

はじめに

サーバにTCP接続して、メッセージを送信します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/*
 *  This sketch sends a message to a TCP server
 *
 */

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

WiFiMulti WiFiMulti;
 

WiFiMultiクラスは、複数のアクセスポイントを管理するためのクラスです。

setup()

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void setup()
{
    Serial.begin(115200);
    delay(10);

    // We start by connecting to a WiFi network
    WiFiMulti.addAP("SSID", "passpasspass");

    Serial.println();
    Serial.println();
    Serial.print("Waiting for WiFi... ");

    while(WiFiMulti.run() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    delay(500);
}
 
 

wifiMullti.addAP()は、WiFiアクセスポイントの登録です。SSIDとPASSWORDは、各自の環境に合わせて変更する必要があります。

wifiMulti.run()は、wifiMullti.addAP()で登録したアクセスポイントに接続するメソッドです。接続に成功するとWL_CONNECTEDが返ってきます。

WiFi.localIP()は、現在割り当てられているIPアドレスを返却します。WiFiは、WiFi.cppで定義されている変数です。

loop()

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void loop()
{
//    const uint16_t port = 80;
//    const char * host = "192.168.1.1"; // ip or dns
    const uint16_t port = 1337;
    const char * host = "192.168.1.10"; // ip or dns

    Serial.print("Connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(host, port)) {
        Serial.println("Connection failed.");
        Serial.println("Waiting 5 seconds before retrying...");
        delay(5000);
        return;
    }

    // This will send a request to the server
    //uncomment this line to send an arbitrary string to the server
    //client.print("Send this data to the server");
    //uncomment this line to send a basic document request to the server
    client.print("GET /index.html HTTP/1.1\n\n");

  int maxloops = 0;

  //wait for the server's reply to become available
  while (!client.available() && maxloops < 1000)
  {
    maxloops++;
    delay(1); //delay 1 msec
  }
  if (client.available() > 0)
  {
    //read back one line from the server
    String line = client.readStringUntil('\r');
    Serial.println(line);
  }
  else
  {
    Serial.println("client.available() timed out ");
  }

    Serial.println("Closing connection.");
    client.stop();

    Serial.println("Waiting 5 seconds before restarting...");
    delay(5000);
}

48行目で、WiFiClient型の変数 client を定義しています。この後は、この変数を利用して、操作を行います。

client.connect()を利用して、指定したホストの、指定したポートに接続します。connect()は成功したときに1を、失敗したときには0を返します。connect()に失敗したときは、5秒待ってからloop()を終了(return)します。結果として、次のloop()が開始されます。

client.print()で、指定した文字列を書き込みます。WiFiClientは、Printクラスを継承しているので、Serialと同じように、print()などを利用することができます。

66行目では、WiFiClient::available()は、サーバから受信したデータのバイト数を返します。この値が0で、maxloopsが1000未満の場合、delay()を使って1ミリ秒待ち、再度WiFiClient::available()を実行します。

71~76行目では、WiFiClient::available()が0の場合の処理を行います。WiFiClientは、Streamクラスも継承しているので、readStringUntil()が利用できます。

77行目から80行目は、WiFiClient::available()が0以外の場合の処理です。エラーメッセージを表示しています。

最後に、client.stop()を呼び出して、WiFi接続を停止します。

バージョン

Hardware:ESP-WROOM-32
Software:Arduino core for the ESP32 2.0.4

最終更新日

November 1, 2022

inserted by FC2 system