1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-12 23:08:52 +01:00
Arduino/libraries/Ethernet/examples/WebClient/WebClient.ino

89 lines
2.4 KiB
Arduino
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.
2010-07-25 18:18:52 +02:00
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
2010-07-25 18:18:52 +02:00
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
*/
#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
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
2010-07-25 18:18:52 +02:00
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
2008-07-30 16:47:36 +02:00
2010-07-25 18:18:52 +02:00
void setup() {
// Open serial communications and wait for port to open:
2008-07-30 16:47:36 +02:00
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// 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);
}
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:
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()
{
// if there are incoming bytes available
2010-07-25 18:18:52 +02:00
// 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:
while (true);
2008-07-30 16:47:36 +02:00
}
}
2010-07-25 18:18:52 +02:00