Ethernet

Ethernetの初期化用ライブラリです。初期化はクライアント、サーバーで共通で必要になります。

WIZnetのWIZ550ioイアント、サーバーで共通で必要になります。WIZnetのWIZ550ioioShieldと接続し、SPIによって制御を行います。使用するためには、#include <SPI.h>#include <Ethernet.h>を記述してください。

begin

概要

イーサネットライブラリを開始します。パラメータとして指定しないIPアドレス、DNS、デフォルトゲートウェイ、サブネットマスクはDHCPで取得されます。

文法

int Ethernet.begin()
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()はDHCPが成功したら1、失敗したら0を返す。それ以外はなし。

localIP

概要

自分のIPアドレスを受け取る。

文法

IPAddress Ethernet.localIP()

パラメータ

なし

戻り値

自分のIPアドレス

maintain

概要

DHCPでアドレスを更新する。

文法

Ethernet.maintain()

パラメータ

なし

戻り値

0: 何もなし
1: 更新失敗
2: 更新成功
3: 再接続失敗
4: 再接続成功


サンプルプログラム

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


                #include <Arduino.h>
                #include <SPI.h>
                #include <Ethernet.h>
                    
                void setup()
                {
                    Serial.begin(9600);
                        
                    // start the Ethernet connection:
                    if (Ethernet.begin() == 0) {
                        Serial.println("Failed to configure Ethernet using DHCP");
                        while(1);
                    }
                    // print your local IP address:
                    Serial.println(Ethernet.localIP());
                }
                    
                void loop() {
                }