Ethernet クライアント

Ethernetでクライアントとして動作するためのライブラリです。

EthernetClient()

概要
クライアントを作るためのコンストラクターです。
文法(例)
EthernetClient client
パラメータ
なし

connected

概要
接続しているか調べる。
文法
int8_t client.connected()
パラメータ
なし
戻り値
接続していたらtrue、接続していなかったらfalseを返す。

connect

概要
サーバーに接続します。
文法
int connect(IPAddress ip, uint16_t port)
int connect(URL, uint16_t port)
パラメータ
ip: 接続するIPアドレス (4バイトの配列)
URL: 接続するドメイン (例えば"arduino.cc")
port: 接続するポート (uint16_t)
戻り値
SUCCESS 1
INVALID_RESPONSE -4

write

概要
接続しているサーバーにデータを送信する。
文法
client.write(val)
client.write(buf, len)
パラメータ
val: 1バイトのデータ (byte or char)
buf: 複数バイトの配列データ (byte or char)
len: 配列データの長さ
戻り値
送ったバイト数。特に読む必要なし。

print

概要
接続しているサーバーにデータを送る。数字はそれぞれASCIIコードとしてデータを送る。例えば123は'1','2','3'として送られる。
文法
client.print(data)
client.print(data, BASE)
パラメータ
data: 送る文字列や数値
BASE(オプション): 基数(BYTE、BIN、DEC、HEX、OCT)
戻り値
送ったバイト数。あまり読む必要はない。

println

概要
接続しているサーバーに改行付きでデータを送る。数字はそれぞれASCIIコードとしてデータを送る。例えば123は'1','2','3'として送られる。
文法
client.println(data)
client.print(data, BASE)
パラメータ
data: 送る文字列や数値
BASE(オプション): 基数(BYTE、BIN、DEC、HEX、OCT)
戻り値
送ったバイト数。あまり読む必要はない。

available

概要
接続している相手先から受け取ったデータがあるかどうかを調べる
文法
int client.available()
パラメータ
なし
戻り値
受信したデータ数

read

概要
受信したデータを1文字取り出します。
文法
int client.read()
パラメータ
なし
戻り値
読み出したデータ。データがなければ-1を返す。

サンプルプログラム

DHCPで接続して、IPを表示するサンプルです。


#include <Arduino.h>
#include <Ethernet.h>
 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(216,58,211,3);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)
 
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,0,177);
 
// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
 
void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Start to connect..");
 
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
 
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}
 
void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
 
    // do nothing forevermore:
    while(true);
  }
}