Ethernet
This library is used for initiarizing Ethernet. It is necessary to use Ethernet for both cliant and server. Specify #include <Ethernet.h> for use.
begin
Description | Initializes the Ethernet library and network settings. The DHCP automatically obtains an IP address, DNS, default gateway, and subnet mask. |
---|---|
Syntax | 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) |
Parameters | mac: MAC address (array of 6 bytes)
ip: IP address (array of 4 bytes) dns: name server address (array of 4 bytes) gateway: default gateway address (array of 4 bytes) subnet: subnet mask (array of 4 bytes) |
Returns | If Ethernet.begin(mac) is successful = 1, if failure = 0. Others are none. |
localIP
Description | Obtains the IP address of the board. |
---|---|
Syntax | IPAddress Ethernet.localIP() |
Parameters | None |
Returns | the IP address |
maintain
Description | Allows for the renewal of DHCP leases. |
---|---|
Syntax | Ethernet.maintain() |
Parameters | None |
Returns | 0: nothing happened
1: renew failed 2: renew success 3: rebind fail 4: rebind success |
Sample Program
Connect with DHCP, and then display the IP address.
#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() { }