Arduino Ethernet Shield
Arduino Ethernet Shield
Arduino Ethernet Shield
Operating voltage 5V
(supplied from the
Arduino Board)
Ethernet Controller:
W5500 with internal
32K buffer
Connection speed:
10/100Mb
Connection with
Arduino on SPI port
Ethernet / Ethernet 2 library
Ethernet / Ethernet 2 library
These libraries are designed to work with the
Arduino Ethernet Shield (Ethernet.h) or the
Arduino Ethernet Shield 2 and Leonardo
Ethernet (Ethernet2.h).
The libraries are allow an Arduino board to
connect to the internet.
The board can serve as either a server accepting
incoming connections or a client making
outgoing ones.
Ethernet library (Ethernet.h) manages the
W5100 chip, while Ethernet2 library
(Ethernet2.h) manages the W5500 chip; all the
functions remain the same.
Arduino - Ethernet
Arduino
communicates
with the shield
using the SPI bus.
This is on digital
pins 11, 12, and
13 on the Uno .
On UNO board,
pin 10 is used as
SS.
ETHERNET CLASS
Ethernet class
SYNTAX
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: the MAC (Media access control) address for the
device (array of 6 bytes). this is the Ethernet hardware
address of your shield. Newer Arduino Ethernet Shields
include a sticker with the device's MAC address. For
older shields, choose your own.
begin()
ip: the IP address of the device (array of 4 bytes)
dns: the IP address of the DNS server (array of 4 bytes).
optional: defaults to the device IP address with the last
octet set to 1
gateway: the IP address of the network gateway (array
of 4 bytes). optional: defaults to the device IP address
with the last octet set to 1
subnet: the subnet mask of the network (array of 4
bytes). optional: defaults to 255.255.255.0
RETURNS
The DHCP version of this function, Ethernet.begin(mac),
returns an int: 1 on a successful DHCP connection, 0 on
failure. The other versions don't return anything.
Example
#include <SPI.h>
#include <Ethernet.h>
void setup()
{
Ethernet.begin(mac, ip);
}
void loop () {}
localIP()
#include <SPI.h>
#include <Ethernet.h>
SYNTAX
Ethernet.maintain();
Parameters
none
Returns
byte:
0: nothing happened
1: renew failed
2: renew success
3: rebind fail
4: rebind success
IP ADDRESS CLASS
IP Address class
IPAddress()
IPAddress()
Parameters
data (optional): the data to print (char, byte, int,
long, or string)
BASE (optional): the base in which to print
numbers: BIN for binary (base 2), DEC for
decimal (base 10), OCT for octal (base 8), HEX for
hexadecimal (base 16).
Returns
byte
println() will return the number of bytes written,
though reading that number is optional
Example
#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 };
// 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());
}
}
CLIENT CLASS
Client class
Client is the base class for all Ethernet client based calls.
It is not called directly, but invoked whenever you use a
function that relies on it.
Functions
EthernetClient()
connected()
connect()
write()
print()
println()
available()
read()
flush()
stop()
EthernetClient()
Parameters
data (optional): the data to print (char,
byte, int, long, or string)
BASE (optional): the base in which to print
numbers: DEC for decimal (base 10), OCT
for octal (base 8), HEX for hexadecimal
(base 16).
Returns
byte: return the number of bytes written,
though reading that number is optional
available()
#include <Ethernet.h>
#include <SPI.h>
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
client.connect(server, 80);
delay(1000);
Cont..
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");
}
}
Cont..
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
ETHERNETUDP CLASS
EthernetUDP class
Parameters
localPort: the local port to listen on (int)
Returns
1 if successful, 0 if there are no sockets available
to use
UDP.read()
Parameters
remoteIP: the IP address of the remote connection (4
bytes)
remotePort: the port of the remote connection (int)
Returns
Returns an int: 1 if successful, 0 if there was a problem
resolving the hostname or port.
UDP.endPacket()
Parameters
None
Returns
Returns an int: 1 if the packet was sent
successfully, 0 if there was an error
UDP.parsePacket()
Checks for the presence of a UDP packet, and
reports the size. parsePacket() must be called
before reading the buffer with UDP.read().
Syntax
UDP.parsePacket();
Parameters
None
Returns
int: the size of a received UDP packet
UDP.available()
Parameters
None
Returns
4 bytes : the IP address of the remote connection
UDP.remotePort()
Parameters
None
Returns
int : the port of the UDP connection to a remote
host
Example
#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
Example
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
void loop() {