How to use the Arduino Ethernet Shield

Posted on by
Arduino Ethernet Shield

Arduino Ethernet Shield

Among the different ways to communicate with an Arduino, one is to use an Ethernet Shield will allow us to our Arduino to behave as a server or a client. Here we explain it up and running in a basic way.

Setting up the MAC and the IP

First we set the MAC in the code of our Arduino, depending on the version of the Shield Ethernet may come a sticker with the MAC, otherwise we can put anyone you want, carefully as there can not be the same MAC address several times on the same network. On the other hand it is advisable that you set your router to assign a fixed IP instead of automatically assigned using DHCP, associated with the MAC that we have previously chosen. For the examples will make the MAC is AA: AA: AA: AA: AA: AA and IP 192.168.0.122.

byte mac[] = { 
 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
IPAddress ip(192,168,0,122);

Basic example of Arduino Server

This is a basic example for an Arduino as a server, when it receives a request will return an HTML site with a Hello World!. To prove just have to enter http://192.168.0.122/ and see the text “Hello World!” in your browser.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 
 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
IPAddress ip(192,168,0,122);

EthernetServer server(80);

void setup() {
 // Ethernet and server:
 Ethernet.begin(mac, ip);
 server.begin();
}


void loop() {
 EthernetClient client = server.available();
 if (client) {
 boolean currentLineIsBlank = true;
 while (client.connected()) {
 if (client.available()) {
 char c = client.read();
 if (c == '\n' && currentLineIsBlank) {
 // HTTP Header
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println("Connection: close");
 client.println("Refresh: 5");
 client.println();
 client.println("<!DOCTYPE HTML>");
 client.println("<html>");
 client.println("Hello World!");
 client.println("</html>");
 break;
 }
 if (c == '\n') {
 currentLineIsBlank = true;
 } 
 else if (c != '\r') {
 currentLineIsBlank = false;
 }
 }
 }
 delay(1);
 client.stop();
 }
}

Basic example of Arduino Client

If we want our Arduino connects to a website, either to extract information or send data have to use it as a client. In the following example we will see how Arduino connects to scidle.com printing on the serial port the server’s response, which will be the HTML of the homepage.

#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
IPAddress ip(192,168,0,122);

char server[] = "scidle.com";

EthernetClient client;

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

 // Ethernet:
 if (Ethernet.begin(mac) == 0) {
 Serial.println("Failed to configure Ethernet using DHCP");
 Ethernet.begin(mac, ip);
 }
 
 delay(1000);
 Serial.println("connecting...");

 if (client.connect(server, 80)) {
 Serial.println("connected");
 client.println("GET /search?q=arduino HTTP/1.1");
 client.println("Host: scidle.com");
 client.println("Connection: close");
 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();

 while(true);
 }
}
Comments are disabled