This example connects to a named server and makes a request using an Ethernet shield. The sketch in the example illustrates how to connect using DHCP and DNS.
Shield-compatible Arduino board (Arduino UNO and Mega)
Ethernet Cable
The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino via the SPI bus. It uses pins 10, 11, 12, and 13 for the SPI connection to the WizNet. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the chip select pin on the SD card.
The shield should be connected to a network with an ethernet cable. You will need to change the network settings in the program to correspond to your network. Mount the Ethernet Shield to your Arduino Board.
Now we will get to the programming part of the tutorial.
We need to install the libraries needed. Simply go to Tools > Manage libraries... and search for Ethernet Library and install it.
Upload the sketch to the board.
Before we start, lets take a look at some core functions of the program:
Client
- 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.EthernetClient()
- Creates a client which can connect to a specified internet IP address and port (defined in the client.connect()
function).client.connected()
- Whether or not the client is connected. Note that a client is considered connected if the connection has been closed but there is still unread data.client.connect(servername,port)
is called. servername
is a URL string, like "www.arduino.cc"
.Upload the sketch below to your board:
1/*2
3 DNS and DHCP-based Web client4
5 This sketch connects to a website (http://www.google.com)6
7 using an Arduino Wiznet Ethernet shield.8
9 Circuit:10
11 * Ethernet shield attached to pins 10, 11, 12, 1312
13 created 18 Dec 200914
15 by David A. Mellis16
17 modified 9 Apr 201218
19 by Tom Igoe, based on work by Adrian McEwen20
21 */22
23#include <SPI.h>24#include <Ethernet.h>25
26// Enter a MAC address for your controller below.27// Newer Ethernet shields have a MAC address printed on a sticker on the shield28byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };29char serverName[] = "www.google.com";30
31// Initialize the Ethernet client library32// with the IP address and port of the server33// that you want to connect to (port 80 is default for HTTP):34
35EthernetClient client;36
37void setup() {38
39 // Open serial communications and wait for port to open:40
41 Serial.begin(9600);42
43 while (!Serial) {44
45 ; // wait for serial port to connect. Needed for Leonardo only46
47 }48
49 // start the Ethernet connection:50
51 if (Ethernet.begin(mac) == 0) {52
53 Serial.println("Failed to configure Ethernet using DHCP");54
55 // no point in carrying on, so do nothing forevermore:56
57 while(true);58
59 }60
61 // give the Ethernet shield a second to initialize:62
63 delay(1000);64
65 Serial.println("connecting...");66
67 // if you get a connection, report back via serial:68
69 if (client.connect(serverName, 80)) {70
71 Serial.println("connected");72
73 // Make a HTTP request:74
75 client.println("GET /search?q=arduino HTTP/1.0");76
77 client.println();78
79 }80
81 else {82
83 // if you didn't get a connection to the server:84
85 Serial.println("connection failed");86
87 }88}89
90void loop()91{92
93 // if there are incoming bytes available94
95 // from the server, read them and print them:96
97 if (client.available()) {98
99 char c = client.read();100
101 Serial.print(c);102
103 }104
105 // if the server's disconnected, stop the client:106
107 if (!client.connected()) {108
109 Serial.println();110
111 Serial.println("disconnecting.");112
113 client.stop();114
115 // do nothing forevermore:116
117 while(true);118
119 }120}
After you have uploaded the code to your board, open the Serial Monitor in your IDE. You should see this:
In the Serial Monitor we print the incoming bytes from the
client.println("GET /search?q=arduino HTTP/1.0");
function where we search for arduino
on Google. We defined char c
as client.read();
and then print it. If the code is not working, there are some common issues we can troubleshoot:
In this tutorial we have used the Arduino Ethernet Shield to connect to Google and make a search request. The data from the search request is then printed to the Serial Monitor in the Arduino IDE.