root / arduino-1.0 / libraries / Ethernet / examples / WebClient / WebClient.ino @ 58d82c77
History | View | Annotate | Download (1.77 KB)
1 |
/* |
---|---|
2 |
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 |
|
13 |
*/ |
14 |
|
15 |
#include <SPI.h> |
16 |
#include <Ethernet.h> |
17 |
|
18 |
// Enter a MAC address for your controller below. |
19 |
// Newer Ethernet shields have a MAC address printed on a sticker on the shield |
20 |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
21 |
IPAddress server(173,194,33,104); // Google |
22 |
|
23 |
// Initialize the Ethernet client library |
24 |
// with the IP address and port of the server |
25 |
// that you want to connect to (port 80 is default for HTTP): |
26 |
EthernetClient client; |
27 |
|
28 |
void setup() { |
29 |
// start the serial library: |
30 |
Serial.begin(9600); |
31 |
// start the Ethernet connection: |
32 |
if (Ethernet.begin(mac) == 0) { |
33 |
Serial.println("Failed to configure Ethernet using DHCP"); |
34 |
// no point in carrying on, so do nothing forevermore: |
35 |
for(;;) |
36 |
; |
37 |
} |
38 |
// give the Ethernet shield a second to initialize: |
39 |
delay(1000); |
40 |
Serial.println("connecting..."); |
41 |
|
42 |
// if you get a connection, report back via serial: |
43 |
if (client.connect(server, 80)) { |
44 |
Serial.println("connected"); |
45 |
// Make a HTTP request: |
46 |
client.println("GET /search?q=arduino HTTP/1.0"); |
47 |
client.println(); |
48 |
} |
49 |
else { |
50 |
// kf you didn't get a connection to the server: |
51 |
Serial.println("connection failed"); |
52 |
} |
53 |
} |
54 |
|
55 |
void loop() |
56 |
{ |
57 |
// if there are incoming bytes available |
58 |
// from the server, read them and print them: |
59 |
if (client.available()) { |
60 |
char c = client.read(); |
61 |
Serial.print(c); |
62 |
} |
63 |
|
64 |
// if the server's disconnected, stop the client: |
65 |
if (!client.connected()) { |
66 |
Serial.println(); |
67 |
Serial.println("disconnecting."); |
68 |
client.stop(); |
69 |
|
70 |
// do nothing forevermore: |
71 |
for(;;) |
72 |
; |
73 |
} |
74 |
} |
75 |
|