Skip to content

Commit abfe299

Browse files
committed
Added DHCP address printer and DNS-based web client, based on Adrian McEwen's additions to the Ethernet library
1 parent b50daa1 commit abfe299

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
DHCP-based IP printer
3+
4+
This sketch uses the DHCP extensions to the Ethernet library
5+
to get an IP address via DHCP and print the address obtained.
6+
using an Arduino Wiznet Ethernet shield.
7+
8+
Circuit:
9+
* Ethernet shield attached to pins 10, 11, 12, 13
10+
11+
created 12 April 2011
12+
by Tom Igoe
13+
14+
*/
15+
16+
#include <SPI.h>
17+
#include <Ethernet.h>
18+
19+
// Enter a MAC address for your controller below.
20+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
21+
byte mac[] = {
22+
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
23+
24+
// Initialize the Ethernet client library
25+
// with the IP address and port of the server
26+
// that you want to connect to (port 80 is default for HTTP):
27+
Client client;
28+
29+
void setup() {
30+
// start the serial library:
31+
Serial.begin(9600);
32+
// start the Ethernet connection:
33+
if (Ethernet.begin(mac) == 0) {
34+
Serial.println("Failed to configure Ethernet using DHCP");
35+
// no point in carrying on, so do nothing forevermore:
36+
for(;;)
37+
;
38+
}
39+
// print your local IP address:
40+
Serial.print("My IP address: ");
41+
for (byte thisByte = 0; thisByte < 4; thisByte++) {
42+
// print the value of each byte of the IP address:
43+
Serial.print(Ethernet.localIP()[thisByte], DEC);
44+
Serial.print(".");
45+
}
46+
Serial.println();
47+
}
48+
49+
void loop() {
50+
51+
}
52+
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
DNS and DHCP-based Web client
3+
4+
This sketch connects to a website (http://www.google.com)
5+
using an Arduino Wiznet Ethernet shield.
6+
7+
Circuit:
8+
* Ethernet shield attached to pins 10, 11, 12, 13
9+
10+
created 18 Dec 2009
11+
by David A. Mellis
12+
modified 12 April 2011
13+
by Tom Igoe, based on work by Adrian McEwen
14+
15+
*/
16+
17+
#include <SPI.h>
18+
#include <Ethernet.h>
19+
20+
// Enter a MAC address for your controller below.
21+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
22+
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
23+
char serverName[] = "www.google.com";
24+
25+
// Initialize the Ethernet client library
26+
// with the IP address and port of the server
27+
// that you want to connect to (port 80 is default for HTTP):
28+
Client client;
29+
30+
void setup() {
31+
// start the serial library:
32+
Serial.begin(9600);
33+
// start the Ethernet connection:
34+
if (Ethernet.begin(mac) == 0) {
35+
Serial.println("Failed to configure Ethernet using DHCP");
36+
// no point in carrying on, so do nothing forevermore:
37+
while(true);
38+
}
39+
// give the Ethernet shield a second to initialize:
40+
delay(1000);
41+
Serial.println("connecting...");
42+
43+
// if you get a connection, report back via serial:
44+
45+
if (client.connect(serverName, 80)) {
46+
Serial.println("connected");
47+
// Make a HTTP request:
48+
client.println("GET /search?q=arduino HTTP/1.0");
49+
client.println();
50+
}
51+
else {
52+
// kf you didn't get a connection to the server:
53+
Serial.println("connection failed");
54+
}
55+
}
56+
57+
void loop()
58+
{
59+
// if there are incoming bytes available
60+
// from the server, read them and print them:
61+
if (client.available()) {
62+
char c = client.read();
63+
Serial.print(c);
64+
}
65+
66+
// if the server's disconnected, stop the client:
67+
if (!client.connected()) {
68+
Serial.println();
69+
Serial.println("disconnecting.");
70+
client.stop();
71+
72+
// do nothing forevermore:
73+
while(true);
74+
}
75+
}
76+

0 commit comments

Comments
 (0)