root / arduino-1.0 / libraries / Ethernet / examples / DnsWebClient / DnsWebClient.ino @ 58d82c77
History | View | Annotate | Download (1.85 KB)
1 | 58d82c77 | Tom Mullins | /* |
---|---|---|---|
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 | EthernetClient 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 | } |