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
|
|
|
|
|
|
|
|
*/
|
2010-08-02 20:59:44 +02:00
|
|
|
|
|
|
|
#include <SPI.h>
|
2008-07-30 16:47:36 +02:00
|
|
|
#include <Ethernet.h>
|
|
|
|
|
2011-03-24 04:28:33 +01:00
|
|
|
// 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 };
|
2011-03-24 04:28:33 +01:00
|
|
|
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);
|
2011-03-24 04:28:33 +01:00
|
|
|
// 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
|
|
|
|