Ethernet
License unknownV2.0.2 Various (see AUTHORS file for details)2026/07/03
Arduino

Arduino Ethernetボードやシールドを使い、ネットワーク接続(ローカル/インターネット)を行う。

このライブラリを使うと、Arduino Ethernet(シールドもしくはボード)をインターネットに接続できる。ライブラリは、クライアントとサーバの機能を提供する。DNSを使ってローカルネットワークに接続したり、DNSの解決を行うこともできる。

リポジトリ

このライブラリは、Arduino Ethernet ShieldとArduino Ethernet Shield2、Leonardo Ethernet、他のW5100/W5200/W5500ベースのデバイスで動作する。 Ethernetシールドとこのライブラリを使うことで、Arduinoボードをインターネットに接続することができる。このライブラリは、サーバとして接続を受け付けることも、クライアントとして外部への接続を行うこともできる。このライブラリは8つ(ただし、W5100とSRAMが2kB以下のボードでは4つまで)のコネクションまで同時に確立することができる(サーバ・クライアントもしくは混在)。

ArduinoはシールドとSPIバスを使って通信する。このために利用するのは、デジタルピンの11番と12番、13番(Unoの場合)、あるいは、50番と51番、52番(Megaの場合)である。どちらのボードでも、10番はSSとして利用する。Megaでは、53番(ハードウェアSSピン)はW5100を選択するためには使わず、出力として確保しておく必要がある。そうでないと、SPIインターフェイスは動作しない。

このライブラリを使うためには、以下を宣言する。

1
2
#include <SPI.h> 
#include <Ethernet.h>
このライブラリは全てのアーキテクチャで利用できるので、全てのArduinoボードで利用できる。

このライブラリを使うためには、Arduino IDEでライブラリマネージャを開きインストールする。

Ethernet.begin()

名称

Ethernet.begin()

説明

Ethernetライブラリとネットワーク設定を初期化する。

Arduino 1.0では、DHCPをサポートする。Ethernet.begin(mac)を適切なネットワーク設定の下で利用すると、EthernetシールドはIPアドレスを自動取得する。これは、スケッチのサイズをかなり大きくする。DHCPリースを適切に更新するためには、Ethernet.maintain()を定期的に呼び出すこと。

書式

int EthernetClass::begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);

void EthernetClass::begin(uint8_t *mac, IPAddress ip);

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns);

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway);

void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet);

引数

mac
デバイスのMAC(Media Access Control)アドレス(6バイトの配列)。これは、シールドのEthernetハードウェアアドレスである。新しいArduino EthernetシールドにはそのデバイスのMACアドレスを書いたシールが貼ってある。古いシールドでは、自分で選択する。
timeoutbegin()のタイムアウト時間。
responseTimeoutDHCPサーバへの問い合わせのタイムアウト時間。
ipデバイスのIPアドレス(4バイトの配列)。
dns
DNSサーバのIPアドレス。
gateway
ネットワークゲートウェイのIPアドレス(4バイトの配列)。デフォルトはデバイスのIPアドレスの最後のオクテットを1にしたもの。
subnetネットワークのサブネットマスク(4バイトの配列)。デフォルトは、255.255.255.0

戻り値

DHCPを使う場合は、アドレスを確保できれば1、できなければ0.

DHCPを使わない場合は、なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <SPI.h>
#include <Ethernet.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    

void setup()
{
  Ethernet.begin(mac, ip);
}

void loop () {}

訳者註

Ethernetライブラリを利用すると、Ethernetというオブジェクトが自動生成される(Serialと同じ)。

Ethernet.dnsServerIP()

名称

Ethernet.dnsServerIP()

説明

デバイスに設定された、DNSサーバのIPアドレスを取得する。

書式

IPAddress EthernetClass::dnsServerIP();

引数

なし。

戻り値

デバイスに設定された、DNSサーバのIPアドレス。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);

  Serial.print("The DNS server IP address is: ");
  Serial.println(Ethernet.dnsServerIP());
}

void loop () {}
Ethernet.gatewayIP()

名称

Ethernet.gatewayIP()

説明

デバイスに設定された、ゲートウェイのIPアドレスを取得する。

書式

IPAddress EthernetClass::gatewayIP();

引数

なし。

戻り値

デバイスに設定された、ゲートウェイのIPアドレス。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);

  Serial.print("The gateway IP address is: ");
  Serial.println(Ethernet.gatewayIP());
}

void loop () {}
Ethernet.hardwareStatus()

名称

Ethernet.hardwareStatus()

説明

Ethernet.hardwareStatus()は、Ethernet.begin()によって認識した、WIZnetイーサネットコントローラの種類を返却する。これは、トラブルシューティングに利用することができる。もしもイーサネットコントローラが検出できなかった場合は、ハードウェアに問題があることが疑われる。

書式

EthernetHardwareStatus EthernetClass::hardwareStatus();

引数

なし。

戻り値

Ethernet.begin()によって認識した、WIZnetイーサネットコントローラの種類。

  • EthernetNoHardware
  • EthernetW5100
  • EthernetW5200
  • EthernetW5500

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5100) {
    Serial.println("W5100 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5200) {
    Serial.println("W5200 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5500) {
    Serial.println("W5500 Ethernet controller detected.");
  }
}

void loop () {}
Ethernet.init()

名称

Ethernet.init()

説明

イーサネットコントローラチップのCS(チップセレクト)ピンを設定する。EthernetライブラリはデフォルトのCSピンを持っていて、通常は正しく設定されている。しかし、非標準のイーサネットハードウェアでは、異なるCSピンを使う必要がある。

書式

void EthernetClass::init(uint8_t sspin);

引数

sspinCSピン用のピン番号。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Ethernet.init(53);  // use pin 53 for Ethernet CS
  Ethernet.begin(mac, ip);
}

void loop () {}
Ethernet.linkStatus()

名称

Ethernet.linkStatus()

説明

リンクがアクティブかどうかを返却する。LinkOFFはイーサネットケーブルが接続されていないか、不良であるかを示す。この機能は、W5200かw5500イーサネットコントローラチップを使っているときだけ利用できる。

書式

EthernetLinkStatus EthernetClass::linkStatus();

引数

なし。

戻り値

リンク状態。

  • Unknown
  • LinkON
  • LinkOFF

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <SPI.h>
#include <Ethernet.h>

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop () {
  if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown. Link status detection is only available with W5200 and W5500.");
  }
  else if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Link status: On");
  }
  else if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Link status: Off");
  }
}
Ethernet.localIP()

名称

Ethernet.localIP()

説明

EthernetシールドのIPアドレスを取得する。DHCPを利用してIPアドレスを取得したときに有用である。

書式

IPAddress EthernetClass::localIP();

引数

なし。

戻り値

IPアドレス。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// 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() {
  // start the serial library:
  Serial.begin(9600);
  // 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:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.println(Ethernet.localIP());

}

void loop() {

}
Ethernet.MACAddress()

名称

Ethernet.MACAddress()

説明

与えられたバッファに、デバイスのMACアドレスを設定する。

書式

void EthernetClass::MACAddress(uint8_t *mac_address);

引数

mac_addressMACアドレスを格納するためのバッファ(6バイトの配列)。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);

  byte macBuffer[6];  // create a buffer to hold the MAC address
  Ethernet.MACAddress(macBuffer); // fill the buffer
  Serial.print("The MAC address is: ");
  for (byte octet = 0; octet < 6; octet++) {
    Serial.print(macBuffer[octet], HEX);
    if (octet < 5) {
      Serial.print('-');
    }
  }
}

void loop () {}
Ethernet.maintain()

名称

Ethernet.maintain()

説明

DHCPリースの期限を延長する。DHCPサーバ経由でIPアドレスを割り当てたとき、イーサネットデバイスは一定期間アドレスをリースされる。maintain()を使うことで、DHCPサーバに期限の延長を要求することができる。サーバの設定により、同じアドレスを取得したり、新規アドレスを取得したり、何ももらえなかったりする。

この関数は何回でも呼び出すことができるが、必要な時にDHCPリースの再要求が行われる。もっとも簡単なのは、loop()が実行されるときに呼び出すことであるが、より少ない呼び出しの方がいい。DHCPプロトコルが延長を要求するときに、この関数を呼び出さなければリースは更新されず、期限切れのリースを使い続けることになる。これにより、ただちに接続が切れることはないが、DHCPサーバが同じアドレスを他にリースした場合は問題が生じる。

maintain()は、Arduino 1.0.1から導入された。

書式

int EthernetClass::maintain();

引数

なし。

戻り値

0 何も起こらなかった。
1 期限延長失敗。
2 期限延長成功。
3 再取得失敗。
4 再取得成功。
Ethernet.setDnsServerIP()

名称

Ethernet.setDnsServerIP()

説明

DNSサーバのIPアドレスを設定する。DHCPのときは利用しない。

書式

void EthernetClass::setDnsServerIP(const IPAddress dns_server);

引数

dns_serverDNSサーバのIPアドレス。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
IPAddress myDns(192, 168, 1, 1);

void setup() {
  Ethernet.begin(mac, ip, myDns);
  IPAddress newDns(192, 168, 1, 1);
  Ethernet.setDnsServerIP(newDns);  // change the DNS server IP address
}

void loop () {}
Ethernet.setGatewayIP()

名称

Ethernet.setGatewayIP()

説明

ネットワークゲートウェイのIPアドレスを設定する。DHCPのときは利用しない。

書式

void EthernetClass::setGatewayIP(const IPAddress gateway);

引数

gatewayネットワークゲートウェイのIPアドレス。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);

void setup() {
  Ethernet.begin(mac, ip, myDns, gateway);
  IPAddress newGateway(192, 168, 100, 1);
  Ethernet.setGatewayIP(newGateway);  // change the gateway IP address
}

void loop () {}
Ethernet.setLocalIP()

名称

Ethernet.setLocalIP()

説明

デバイスのIPアドレスを設定する。DHCPのときは利用しない。

書式

void EthernetClass::setLocalIP(const IPAddress local_ip);

引数

local_ip利用するIPアドレス。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Ethernet.begin(mac, ip);
  IPAddress newIp(10, 0, 0, 178);
  Ethernet.setLocalIP(newIp);  // change the IP address
}

void loop () {}
Ethernet.setMACAddress()

名称

Ethernet.setMACAddress()

説明

MACアドレスを設定する。DHCPのときは利用しない。

書式

void EthernetClass::setMACAddress(const uint8_t *mac_address);

引数

mac_address利用するMACアドレス(6バイトの配列)。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Ethernet.begin(mac, ip);
  byte newMac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
  Ethernet.setMACAddress(newMac);  // change the MAC address
}

void loop () {}
Ethernet.setRetransmissionCount()

名称

Ethernet.setRetransmissionCount()

説明

イーサネットコントローラが送信をやめるまでの、送信の試行回数を設定する。初期値は8である。デフォルトのタイムアウト値である200ms間隔で、8回試行するので、通信が失敗するまでに1600ms、ブロッキングによる遅延が発生する。通信の状態が悪いときに、反応を早くするためには、小さい値を設定する。名称とは異なり、最初の送信失敗後の再送回数ではなく、送信の合計回数を設定する。このため、最小値は1である。

書式

void EthernetClass::setRetransmissionCount(uint8_t num);

引数

numイーサネットコントローラが送信をやめるまでの、送信の試行回数。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Ethernet.begin(mac, ip);
  Ethernet.setRetransmissionCount(1);  // configure the Ethernet controller to only attempt one transmission before giving up
}

void loop () {}
Ethernet.setRetransmissionTimeout()

名称

Ethernet.setRetransmissionTimeout()

説明

イーサネットコントローラのタイムアウトを設定する。初期値は200msである。デフォルトのタイムアウト値である200ms間隔で、8回試行するので、通信が失敗するまでに1600ms、ブロッキングによる遅延が発生する。通信の状態が悪いときに、反応を早くするためには、小さい値を設定する。アプリケーション向けに最適な値を決定するために、実験する必要がある。

書式

void EthernetClass::setRetransmissionTimeout(uint16_t milliseconds);

引数

millisecondsタイムアウト間隔。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Ethernet.begin(mac, ip);
  Ethernet.setRetransmissionTimeout(50);  // set the Ethernet controller's timeout to 50 ms
}

void loop () {}
Ethernet.setSubnetMask()

名称

Ethernet.setSubnetMask()

説明

ネットワークのサブネットマスクを設定する。DHCPのときは利用しない。

書式

void EthernetClass::setSubnetMask(const IPAddress subnet);

引数

subnetネットワークのサブネットマスク。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

void setup() {
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  IPAddress newSubnet(255, 255, 255, 0);
  Ethernet.setSubnetMask(newSubnet);  // change the subnet mask
}

void loop () {}
Ethernet.subnetMask()

名称

Ethernet.subnetMask()

説明

デバイスのサブネットマスクを返却する。

書式

IPAddress EthernetClass::subnetMask();

引数

なし。

戻り値

デバイスのサブネットマスク。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);

  Serial.print("The subnet mask is: ");
  Serial.println(Ethernet.subnetMask());
}

void loop () {}
IPAddress()

名称

IPAddress()

説明

IPアドレスを定義する。ローカルIPアドレス・リモートIPアドレス双方の宣言に利用できる。

書式

IPAddress.IPAddress();

IPAddress.IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);

IPAddress.IPAddress(uint32_t address);

IPAddress.IPAddress(const uint8_t *address);

引数

first_octet, second_octet, third_octet, fourth_octetアドレスを表すコンマ区切りのリスト(4バイトで、例えば、192, 168, 1, 1)。
addressIPアドレス。

戻り値

なし(この関数はコンストラクタ)。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <SPI.h>
#include <Ethernet.h>

// network configuration. dns server, gateway and subnet are optional.

 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  

// the dns server ip
IPAddress dnServer(192, 168, 0, 1);
// the router's gateway address:
IPAddress gateway(192, 168, 0, 1);
// the subnet:
IPAddress subnet(255, 255, 255, 0);

//the IP address is dependent on your network
IPAddress ip(192, 168, 0, 2);

void setup() {
  Serial.begin(9600);

  // initialize the ethernet device
  Ethernet.begin(mac, ip, dnServer, gateway, subnet);
  //print out the IP address
  Serial.print("IP = ");
  Serial.println(Ethernet.localIP());
}

void loop() {
}

訳者註

この関数(クラス)は、Ethernetライブラリの一部ではなく、coreに含まれる。

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ipaddress/

Server

名称

Server

説明

Serverは、全てのEthernetServerの呼び出しに対するベースクラスである。直接呼ばれることはないが、この機能に依存する関数を利用すると呼び出される。

EthernetServer()

名称

EthernetServer()

説明

指定したポートへの接続を待ち受けるサーバを作成する。

書式

EthernetServer::EthernetServer(uint16_t port);

引数

port
待ち受けるポート番号。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
#include <SPI.h>
#include <Ethernet.h>

// network configuration.  gateway and subnet are optional.

 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}
server.begin()

名称

EthernetServer::begin()

説明

指定したポートへの接続を待ち受けるサーバを作成する。

書式

EthernetServer::EthernetServer(uint16_t port);

引数

port
待ち受けるポート番号。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
#include <SPI.h>
#include <Ethernet.h>

// network configuration.  gateway and subnet are optional.

 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}
accept()

名称

EthernetServer::accept()

説明

伝統的なserver.available()関数は、新しいクライアントがデータを送信したことを伝える。これにより、FTP(正しく実装するのは不可能だが)のようなプロトコルを作成することができる。

プログラムがgref available()“とaccept()の両方を利用することはなく、片方だけを利用する。available()では、クライアントからの接続は、EthernetServerによって監理され続ける。available()を呼び出すと、クライアントが送信したデータを取得できるので、クライアントオブジェクトを保持する必要はない。簡単なサーバであれば、available()を使うことでほとんどコーディングする必要はなくなる。

accep()を使うと、EthernetServerは、クライアントがデータを送信したかどうかに関係なく、一度だけクライアントオブジェクトを返却する。プログラマは接続してきたクライアントを保持し続ける必要がある。このため、多くのコードが必要となるが、柔軟な制御を行うことが可能となる。

書式

EthernetClient EthernetServer::accept();

引数

なし。

戻り値

クライアントオブジェクト。どのクライアントからもデータ読み出しができないときは、このオブジェクトをif()文で評価したとき、falseとなる。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 69, 104);

// telnet defaults to port 23
EthernetServer server(23);

EthernetClient clients[8];

void setup() {
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start listening for clients
  server.begin();
}

void loop() {
  // check for any new client connecting, and say hello (before any incoming data)
  EthernetClient newClient = server.accept();
  if (newClient) {
    for (byte i = 0; i < 8; i++) {
      if (!clients[i]) {
        newClient.print("Hello, client number: ");
        newClient.println(i);
        // Once we "accept", the client is no longer tracked by EthernetServer
        // so we must store it into our list of clients
        clients[i] = newClient;
        break;
      }
    }
  }

  // check for incoming data from all clients
  for (byte i = 0; i < 8; i++) {
    while (clients[i] && clients[i].available() > 0) {
      // read incoming data from the client
      Serial.write(clients[i].read());
    }
  }

  // stop any clients which disconnect
  for (byte i = 0; i < 8; i++) {
    if (clients[i] && !clients[i].connected()) {
      clients[i].stop();
    }
  }
}
server.available()

名称

EthernetServer::available()

説明

読み取り用のデータが利用可能な、サーバに接続しているクライアントを取得する。取得したクライアントがなくなってもコネクションは持続する。stop()を呼ぶことでクローズすることができる。

available()はStreamクラスを継承する。

書式

EthernetClient available();

引数

なし。

戻り値

クライアントオブジェクト。利用可能なクライアントがない場合、このオブジェクトをif文で評価すると偽(false)になる。下記の例を参照のこと。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
#include <Ethernet.h>
#include <SPI.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };


// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}
if(server)

名称

if(server)

説明

サーバが新しいクライアントをlistenしているかを示す。これを使ってbegin()が成功したかを確認することができる。また、同時接続可能なクライアント数に達したため、これ以上クライアントをlistenするためのソケットがないことも示すことができる。

書式

if(server);

引数

なし。

戻り値

サーバが新しいクライアントをlistenしていればtrue、そうでなければfalse。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // initialize the Ethernet device
  Ethernet.begin(mac, ip);

  // start listening for clients
  server.begin();
}

void loop() {
  if (server) {
    Serial.println("Server is listening");
  }
  else {
    Serial.println("Server is not listening");
  }
}
server.write()

名称

EthernetServer::write()

説明

サーバに接続しているすべてのクライアントにデータを書く。このとき、データはバイトもしくはバイト列として送信される。

書式

size_t EthernetServer::write(uint8_t b);

size_t EthernetServer::write(const uint8_t *buffer, size_t size)

引数

b書き込むデータ。
bufferデータを格納した配列。
size書き込むデータの大きさ。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
#include <SPI.h>
#include <Ethernet.h>

// network configuration.  gateway and subnet are optional.

 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 10, 0, 0, 177 };    
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();
}

void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client == true) {
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
  }
}
server.print()

名称

EthernetServer::print()

説明

サーバに接続しているすべてのクライアントにデータを書く。数値は連続したアスキー文字として書く。例えば、数値の123は、‘1’、‘2’、‘3’という三つの文字として送信される。

書式

size_t Print::print(const __FlashStringHelper *ifsh);

size_t Print::print(const String &s);

size_t Print::print(const char str[]);

size_t Print::print(char c);

size_t Print::print(unsigned char b, int base = DEC);

size_t Print::print(int n, int base = DEC);

size_t Print::print(unsigned int n, int base = DEC);

size_t Print::print(long n, int base = DEC);

size_t Print::print(unsigned long n, int base = DEC);

size_t Print::print(double n, int digits = 2);

引数

s, str, c, b, n書き込む文字/数値。
base書きこむ値の底。BIN:2進数、DEC:10進数、OCT:8進数、HEX:16進数。省略時は、DEC。
digits小数点以下の表示桁数。省略時は2。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

server.println()

名称

EthernetServer::println()

説明

サーバに接続しているすべてのクライアントにデータを書く。数値は連続したアスキー文字として書く。例えば、数値の123は、‘1’、‘2’、‘3’という三つの文字として送信される。

書式

size_t Print::println(const __FlashStringHelper *ifsh);

size_t Print::println(const String &s);

size_t Print::println(const char str[]);

size_t Print::println(char c);

size_t Print::println(unsigned char b, int base = DEC);

size_t Print::println(int n, int base = DEC);

size_t Print::println(unsigned int n, int base = DEC);

size_t Print::println(long n, int base = DEC);

size_t Print::println(unsigned long n, int base = DEC);

size_t Print::println(double n, int digits = 2);

引数

s, str, c, b, n書き込む文字/数値。
base書きこむ値の底。BIN:2進数、DEC:10進数、OCT:8進数、HEX:16進数。省略時は、DEC。
digits小数点以下の表示桁数。省略時は2。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

Client

名称

Client

説明

Clientは、全てのEthernetクライアントの呼び出しに対するベースクラスである。直接呼ばれることはないが、この機能に依存する関数を利用すると呼び出される。

EthernetClient()

名称

EthernetClient()

説明

指定したIPアドレスのポート(client.connect()関数で指定する)に接続できるクライアントを生成する。

書式

EthernetClient::EthernetClient();

引数

なし。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
43
#include <Ethernet.h>
#include <SPI.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
if(EthernetClient)

名称

if(EthernetClient)

説明

指定したイーサネットクライアントが利用可能かを示す。

書式

if(EthernetClient);

引数

なし。

戻り値

論理型(boolean型)。指定したクライアントが利用可能であれば、trueを返す。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
43
44
45
#include <Ethernet.h>
#include <SPI.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");
  while(!client){
    ; // wait until there is a client connected to proceed
  }
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
client.connected()

名称

EthernetClient::connected()

説明

クライアントが接続しているかを調べる。コネクションが切断されていても未読のデータがある場合には、クライアントは接続しているとみなす。

書式

uint8_t EthernetClient::connected();

引数

なし。

戻り値

クライアントが接続していればtrue、接続していなければfalseを返す。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  client.connect(server, 80);
  delay(1000);

  Serial.println("connecting...");

  if (client.connected()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
client.connect()

名称

EthernetClient::connect()

説明

指定したIPアドレスのポートに接続する。戻り値は成功か失敗かを表す。ドメイン名を使ったときにはDNS検索もサポートする。

書式

int EthernetClient::connect(const char * host, uint16_t port);

int EthernetClient::connect(IPAddress ip, uint16_t port);

引数

hostクライアントが接続するドメイン名(文字列。例えば、“arduino.cc”)。
ipクライアントが接続するIPアドレス(4バイトの配列)。
portクライアントが接続するポート。

戻り値

接続が成功すれば1、失敗の場合は、0(1以外)を返す。

(訳者註)リファレンスによると、失敗の場合は、TIMED_OUT(-1)、INVALID_SERVER(-2)、TRUNCATED(-3)、INVALID_RESPONSE(-4)を返すとありますが、これは、おそらく、ホスト名指定で名前解決ができなかった場合の内部の戻り値と思います(すべての場合を調査したわけではありませんが)。connect()は、これらの値をまとめて、0を返しているようです。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
client.localPort()

名称

EthernetClient::localPort()

説明

クライアントが接続しているローカルポート番号を返却する。

書式

uint16_t EthernetClient::localPort();

引数

なし。

戻り値

クライアントが接続しているローカルポート番号。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // initialize the Ethernet device
  Ethernet.begin(mac, ip);

  // start listening for clients
  server.begin();
}

void loop() {
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {
    Serial.print("Client is connected on port: ");
    Serial.println(client.localPort());
    client.stop();
  }
}
remoteIP()

名称

EthernetClient::remoteIP()

説明

クライアントのIPアドレスを返却する。

書式

uint16_t EthernetClient::localPort();

引数

なし。

戻り値

クライアントのIPアドレス。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // initialize the Ethernet device
  Ethernet.begin(mac, ip);

  // start listening for clients
  server.begin();
}

void loop() {
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {
    Serial.print("Remote IP address: ");
    Serial.println(client.remoteIP());
    client.stop();
  }
}
client.remotePort()

名称

EthernetClient::remotePort()

説明

現在の着信パケットを送信したホストのポート番号を返却する。

書式

uint16_t EthernetClient::remotePort();

引数

なし。

戻り値

現在の着信パケットを送信したホストのポート番号。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // initialize the Ethernet device
  Ethernet.begin(mac, ip);

  // start listening for clients
  server.begin();
}

void loop() {
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {
    Serial.print("Remote port: ");
    Serial.println(client.remotePort());
    client.stop();
  }
}
client.setConnectionTimeout()

名称

EthernetClient::setConnectionTimeout()

説明

EthernetClient::connect()とEthernetClient::stop()のタイムアウトを設定する。初期値は1000msである。通信の状態が悪いときに、反応を早くするためには、小さい値を設定する。

書式

void EthernetClient::setConnectionTimeout(uint16_t timeout);

引数

timeoutEthernetClient::connect()とEthernetClient::stop()のタイムアウト値(ミリ秒単位)。

戻り値

なし。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

// telnet defaults to port 23
EthernetServer server = EthernetServer(23);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // initialize the Ethernet device
  Ethernet.begin(mac, ip);

  // start listening for clients
  server.begin();
}

void loop() {
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {
    client.setConnectionTimeout(100);  // set the timeout duration for client.connect() and client.stop()
  }
}
client.write()

名称

EthernetClient::write()

説明

クライアントが接続しているサーバにデータを書き込む。このデータはバイトもしくはバイト列として送信される。

書式

size_t EthernetClient::write(uint8_t b);

size_t EthernetClient::write(const uint8_t *buf, size_t size);

引数

b書き込むデータ。
bufデータを格納した配列。
sizeデータ長

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

client.print()

名称

EthernetClient::print()

説明

クライアントが接続しているサーバにデータを書く。数値は連続したアスキー文字として書く。例えば、数値の123は、‘1’、‘2’、‘3’という三つの文字として送信される。

書式

size_t Print::print(const __FlashStringHelper *ifsh);

size_t Print::print(const String &s);

size_t Print::print(const char str[]);

size_t Print::print(char c);

size_t Print::print(unsigned char b, int base = DEC);

size_t Print::print(int n, int base = DEC);

size_t Print::print(unsigned int n, int base = DEC);

size_t Print::print(long n, int base = DEC);

size_t Print::print(unsigned long n, int base = DEC);

size_t Print::print(double n, int digits = 2);

引数

s, str, c, b, n書き込む文字/数値。
base書きこむ値の底。BIN:2進数、DEC:10進数、OCT:8進数、HEX:16進数。省略時は、DEC。
digits小数点以下の表示桁数。省略時は2。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

client.println()

名称

EthernetClient::println()

説明

クライアントが接続しているサーバにデータと改行コードを書く。数値は連続したアスキー文字として書く。例えば、数値の123は、‘1’、‘2’、‘3’という三つの文字として送信される。

書式

size_t Print::println(const __FlashStringHelper *ifsh);

size_t Print::println(const String &s);

size_t Print::println(const char str[]);

size_t Print::println(char c);

size_t Print::println(unsigned char b, int base = DEC);

size_t Print::println(int n, int base = DEC);

size_t Print::println(unsigned int n, int base = DEC);

size_t Print::println(long n, int base = DEC);

size_t Print::println(unsigned long n, int base = DEC);

size_t Print::println(double n, int digits = 2);

引数

s, str, c, b, n書き込む文字/数値。
base書き込む値の底。BIN:2進数、DEC:10進数、OCT:8進数、HEX:16進数。省略時は、DEC。
digits小数点以下の表示桁数。省略時は2。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

client.available()

名称

EthernetClient::available()

説明

読み込み可能なバイト数を返却する。すなわち、接続しているサーバによって、クライアントに対して書き込まれたデータの総数である。

available()はStreamクラスを継承する。

書式

int EthernetClient::availableForWrite(void);

引数

なし。

戻り値

読み込み可能なバイト数。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; // Google

EthernetClient client;

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}
client.read()

名称

EthernetClient::read()

説明

クライアントが接続しているサーバから(最後にread()を呼んだ)次のバイトを読み取る。

read()はStreamクラスを継承する。

書式

int EthernetClient::read();

int EthernetClient::read(uint8_t *buf, size_t size);

訳者註: オリジナルのリファレンスには、前者の形式だけが記載されています。

引数

bufデータを格納するバッファ。
sizeバッファサイズ。

戻り値

書き込んだデータのバイト数。これを読みだす必要は必ずしもない。

client.flush()

名称

EthernetClient::flush()

説明

全ての送信データがバッファから送信されるまで待つ。

flush()はStreamクラスを継承する。

書式

void EthernetClient::flush();

引数

なし。

戻り値

なし。

client.stop()

名称

EthernetClient::stop()

説明

サーバから切断する。

書式

void EthernetClient::stop();

引数

なし。

戻り値

なし。

EthernetUDP.begin()

名称

EthernetUDP.begin()

説明

指定したポートへの接続を待ち受けるサーバを作成する。

書式

uint8_t EthernetUDP::begin(uint16_t port);

引数

port
待ち受けるポート番号。

戻り値

成功したら1、利用可能なソケットがなければ0.

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {

}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.begin/

EthernetUDP.read()

名称

EthernetUDP.read()

説明

指定したバッファにUDPデータを読み込む。引数を与えないときは、バッファの次の文字を返す。

この関数はEthernetUDP.parsePacket()を呼んだ後にだけ成功する。

訳者註: オリジナルの説明は、「Reads UDP data from the specified buffer.」となっているが、指定するのは読む込んだデータを格納するデータと思われる。

書式

int EthernetUDP::read();

int EthernetUDP::read(unsigned char *buffer, size_t len);

引数

bufデータを格納するバッファ。
sizeバッファサイズ。

戻り値

引数なしで呼んだとき:(ライブラリの中の)バッファ内の文字(読み取った文字)。

引数ありで呼んだとき:読み取った文字数(読み取った文字は、指定したバッファに入れられる)。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
  }
}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.read/

EthernetUDP.write()

名称

EthernetUDP.write()

説明

リモート接続先にUDPデータを書き出す。

EthernetUDP.beginPacket()EthernetUDP.endPacket()の間に配置する必要がある。EthernetUDP.beginPacket()はデータのパケットを初期化し、aref/EthernetUDP.endPacket()が呼ばれるまでデータは送信されない。

書式

size_t EthernetUDP::write(uint8_t byte);

size_t EthernetUDP::write(const uint8_t *buffer, size_t size);

引数

byte送信するメッセージ。
buffer送信メッセージを保持するバッファ。
size送信メッセージ長。

戻り値

送信したデータのバイト数。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {
  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  Udp.write("hello");
  Udp.endPacket();
}
 

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.write/

EthernetUDP.beginPacket()

名称

EthernetUDP.beginPacket()

説明

リモートコネクションに対してUDPデータの書き込みを開始する。

書式

int EthernetUDP::beginPacket(const char *host, uint16_t port);

int EthernetUDP::beginPacket(IPAddress ip, uint16_t port);

引数

hostリモートコネクションのホスト名。
ipリモートコネクションのIPアドレス(4バイト)。
portリモートコネクションのポート。

戻り値

成功した場合は1、ホスト名かポートの解決に問題があった場合は0。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

}

void loop() {

  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("hello");
    Udp.endPacket();

}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.beginpacket/

EthernetUDP.endPacket()

名称

EthernetUDP.endPacket()

説明

リモートコネクションに対してUDPデータを書き込んだ後に呼ぶ。

書式

int EthernetUDP::endPacket();

引数

なし。

戻り値

パケットの送信に成功したら1、失敗したら0。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

}

void loop() {

  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write("hello");
    Udp.endPacket();

}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.endpacket/

parsePacket()

名称

EthernetUDP.parsePacket()

説明

UDPパケットの存在を確認しサイズを取得する。

parsePacket()はEthernetUDP.read()でバッファを読む前に呼ぶ必要がある。

書式

int EthernetUDP::parsePacket();

引数

なし。

戻り値

受信したUDPパケットのサイズ。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
  }
  delay(10);
}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.parsepacket/

EthernetUDP.available()

名称

EthernetUDP.available()

説明

バッファから読み込み可能なデータのバイト数を返す。これはすでに到着したデータである。

この関数はEthernetUDP.parsePacket()を呼んだ後にだけ成功する。

available()はStreamクラスを継承する。

書式

int EthernetUDP::available();

引数

なし。

戻り値

送信したデータのバイト数。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

}

void loop() {

  int packetSize = Udp.parsePacket();
  if(Udp.available())
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
 }
}

オリジナルのページ

https://www.arduino.cc/reference/en/libraries/ethernet/ethernetudp.available/

オリジナルのページ

https://docs.arduino.cc/libraries/ethernet/

inserted by FC2 system