source: rtos_arduino/trunk/arduino_lib/libraries/Ethernet2/examples/WebClient/WebClient.ino@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 2.4 KB
Line 
1/*
2 Web client
3
4 This sketch connects to a website (http://www.google.com)
5 using an Arduino Wiznet Ethernet shield.
6
7 Circuit:
8 * Ethernet shield attached to pins 10, 11, 12, 13
9
10 created 18 Dec 2009
11 by David A. Mellis
12 modified 9 Apr 2012
13 by Tom Igoe, based on work by Adrian McEwen
14
15 */
16
17#include <SPI.h>
18#include <Ethernet2.h>
19
20// Enter a MAC address for your controller below.
21// Newer Ethernet shields have a MAC address printed on a sticker on the shield
22byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
23// if you don't want to use DNS (and reduce your sketch size)
24// use the numeric IP instead of the name for the server:
25//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
26char server[] = "www.google.com"; // name address for Google (using DNS)
27
28// Set the static IP address to use if the DHCP fails to assign
29IPAddress ip(192, 168, 0, 177);
30
31// Initialize the Ethernet client library
32// with the IP address and port of the server
33// that you want to connect to (port 80 is default for HTTP):
34EthernetClient client;
35
36void setup() {
37 // Open serial communications and wait for port to open:
38 Serial.begin(9600);
39 while (!Serial) {
40 ; // wait for serial port to connect. Needed for Leonardo only
41 }
42
43 // start the Ethernet connection:
44 if (Ethernet.begin(mac) == 0) {
45 Serial.println("Failed to configure Ethernet using DHCP");
46 // no point in carrying on, so do nothing forevermore:
47 // try to congifure using IP address instead of DHCP:
48 Ethernet.begin(mac, ip);
49 }
50 // give the Ethernet shield a second to initialize:
51 delay(1000);
52 Serial.println("connecting...");
53
54 // if you get a connection, report back via serial:
55 if (client.connect(server, 80)) {
56 Serial.println("connected");
57 // Make a HTTP request:
58 client.println("GET /search?q=arduino HTTP/1.1");
59 client.println("Host: www.google.com");
60 client.println("Connection: close");
61 client.println();
62 }
63 else {
64 // kf you didn't get a connection to the server:
65 Serial.println("connection failed");
66 }
67}
68
69void loop()
70{
71 // if there are incoming bytes available
72 // from the server, read them and print them:
73 if (client.available()) {
74 char c = client.read();
75 Serial.print(c);
76 }
77
78 // if the server's disconnected, stop the client:
79 if (!client.connected()) {
80 Serial.println();
81 Serial.println("disconnecting.");
82 client.stop();
83
84 // do nothing forevermore:
85 while (true);
86 }
87}
88
Note: See TracBrowser for help on using the repository browser.