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
|
2013-04-11 17:36:15 +02:00
|
|
|
modified 9 Apr 2012
|
|
|
|
by Tom Igoe, based on work by Adrian McEwen
|
2010-07-25 18:18:52 +02:00
|
|
|
|
|
|
|
*/
|
2010-08-02 20:59:44 +02:00
|
|
|
|
|
|
|
#include <SPI.h>
|
2008-07-30 16:47:36 +02:00
|
|
|
#include <Ethernet.h>
|
|
|
|
|
2011-01-16 21:11:50 +01:00
|
|
|
// Enter a MAC address for your controller below.
|
|
|
|
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
2013-04-11 17:36:15 +02:00
|
|
|
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
|
|
|
// if you don't want to use DNS (and reduce your sketch size)
|
|
|
|
// use the numeric IP instead of the name for the server:
|
|
|
|
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
|
|
|
|
char server[] = "www.google.com"; // name address for Google (using DNS)
|
|
|
|
|
|
|
|
// Set the static IP address to use if the DHCP fails to assign
|
|
|
|
IPAddress ip(192,168,0,177);
|
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):
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client;
|
2008-07-30 16:47:36 +02:00
|
|
|
|
2010-07-25 18:18:52 +02:00
|
|
|
void setup() {
|
2012-04-02 15:07:58 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2008-07-30 16:47:36 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-09 15:51:35 +02:00
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2011-01-16 21:11:50 +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:
|
2013-04-11 17:36:15 +02:00
|
|
|
// try to congifure using IP address instead of DHCP:
|
|
|
|
Ethernet.begin(mac, ip);
|
2011-01-16 21:11:50 +01:00
|
|
|
}
|
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:
|
2011-01-25 17:29:38 +01:00
|
|
|
if (client.connect(server, 80)) {
|
2008-07-30 16:47:36 +02:00
|
|
|
Serial.println("connected");
|
2010-07-25 18:18:52 +02:00
|
|
|
// Make a HTTP request:
|
2013-04-11 17:36:15 +02:00
|
|
|
client.println("GET /search?q=arduino HTTP/1.1");
|
|
|
|
client.println("Host: www.google.com");
|
|
|
|
client.println("Connection: close");
|
2008-07-30 16:47:36 +02:00
|
|
|
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:
|
2013-04-11 17:36:15 +02:00
|
|
|
while(true);
|
2008-07-30 16:47:36 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-25 18:18:52 +02:00
|
|
|
|