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

72 lines
1.6 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>
2010-07-25 18:18:52 +02:00
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
2010-07-25 18:28:30 +02:00
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte 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 Ethernet connection:
2008-07-30 16:47:36 +02:00
Ethernet.begin(mac, ip);
2010-07-25 18:18:52 +02:00
// start the serial library:
2008-07-30 16:47:36 +02:00
Serial.begin(9600);
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