Ethernet Shield DnsWebClient

This example connects to a named server using an Ethernet shield.

DNS Web Client

This example connects to a named server using an Ethernet shield. The sketch illustrates how to connect using DHCP and DNS. When calling Ethernet.begin(mac), the Etehrnet library attempts to obtain an IP address using DHCP. Using DHCP significantly adds to the sketch size; be sure there is enough space to run the program.

DNS lookup happens when client.connect(servername,port) is called. servername is a URL string, like

"www.arduino.cc"
.

Hardware Required

  • Arduino Ethernet Shield

  • Shield-compatible Arduino board

Circuit

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 slave 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.

The circuit for this example.
The circuit for this example.

In the above image, your Arduino would be stacked below the Ethernet shield.

Code

1/*
2
3 DNS and DHCP-based Web client
4
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, 13
12
13 created 18 Dec 2009
14
15 by David A. Mellis
16
17 modified 9 Apr 2012
18
19 by Tom Igoe, based on work by Adrian McEwen
20
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 shield
28byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
29char serverName[] = "www.google.com";
30
31// Initialize the Ethernet client library
32// with the IP address and port of the server
33// 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 only
46
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 // kf 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 available
94
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}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.