Ethernet

Ethernetの初期化用ライブラリです。初期化はクライアント、サーバーで共通で必要になります。使用するためには、#include <Ethernet.h>を記述してください。

begin

概要
イーサネットライブラリを開始します。パラメータとして指定しないIPアドレス、DNS、デフォルトゲートウェイ、サブネットマスクはDHCPで取得されます。
文法
int Ethernet.begin(mac)
Ethernet.begin(mac, ip)
Ethernet.begin(mac, ip, dns)
Ethernet.begin(mac, ip, dns, gateway)
Ethernet.begin(mac, ip, dns, gateway, subnet)
パラメータ
mac: MACアドレス(6バイトの配列)
ip: IPアドレス(4バイトの配列)
dns: ネームサーバーアドレス(4バイトの配列)
gateway: デフォルトゲートウェイアドレス(4バイトの配列)
subnet: サブネットマスク(4バイトの配列)
戻り値
Ethernet.begin(mac)はDHCPが成功したら1、失敗したら0を返す。それ以外はなし。

localIP

概要
自分のIPアドレスを受け取る。
文法
IPAddress Ethernet.localIP()
パラメータ
なし
戻り値
自分のIPアドレス

maintain

概要
DHCPでアドレスを更新する。
文法
Ethernet.maintain()
パラメータ
なし
戻り値
0: 何もなし
1: 更新失敗
2: 更新成功
3: 再接続失敗
4: 再接続成功

サンプルプログラム

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


#include <Arduino.h>
#include <Ethernet.h>
 
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()
{
    Serial.begin(9600);
    while(!Serial.available()); // wait to press key.
     
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        while(1);
    }
    // print your local IP address:
    Serial.println(Ethernet.localIP());
}
 
void loop() {
}