1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/libraries/Ethernet/examples/WebClient/WebClient.pde

76 lines
1.8 KiB
Plaintext
Raw Normal View History

2010-07-25 18:18:52 +02:00
/*
2010-07-25 18:28:30 +02:00
Web client
2010-07-25 18:18:52 +02:00
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
*/
#include <SPI.h>
2008-07-30 16:47:36 +02:00
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
2010-07-25 18:28:30 +02:00
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(173,194,33,104); // Google
2008-07-30 16:47:36 +02:00
2010-07-25 18:18:52 +02:00
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
2008-07-30 16:47:36 +02:00
Client client(server, 80);
2010-07-25 18:18:52 +02:00
void setup() {
// start the serial library:
2008-07-30 16:47:36 +02:00
Serial.begin(9600);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
2010-07-25 18:18:52 +02:00
// give the Ethernet shield a second to initialize:
2008-07-30 16:47:36 +02:00
delay(1000);
Serial.println("connecting...");
2010-07-25 18:18:52 +02:00
// if you get a connection, report back via serial:
2008-07-30 16:47:36 +02:00
if (client.connect()) {
Serial.println("connected");
2010-07-25 18:18:52 +02:00
// Make a HTTP request:
2008-07-30 16:47:36 +02:00
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
2010-07-25 18:18:52 +02:00
}
else {
// kf you didn't get a connection to the server:
2008-07-30 16:47:36 +02:00
Serial.println("connection failed");
}
}
void loop()
{
2010-07-25 18:18:52 +02:00
// if there are incoming bytes available
// from the server, read them and print them:
2008-07-30 16:47:36 +02:00
if (client.available()) {
char c = client.read();
Serial.print(c);
}
2010-07-25 18:18:52 +02:00
// if the server's disconnected, stop the client:
2008-07-30 16:47:36 +02:00
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
2010-07-25 18:18:52 +02:00
// do nothing forevermore:
2008-07-30 16:47:36 +02:00
for(;;)
;
}
}
2010-07-25 18:18:52 +02:00