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

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

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

File size: 2.9 KB
Line 
1/*
2 Repeating Web client
3
4 This sketch connects to a a web server and makes a request
5 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6 the Adafruit Ethernet shield, either one will work, as long as it's got
7 a Wiznet Ethernet module on board.
8
9 This example uses DNS, by assigning the Ethernet client with a MAC address,
10 IP address, and DNS address.
11
12 Circuit:
13 * Ethernet shield attached to pins 10, 11, 12, 13
14
15 created 19 Apr 2012
16 by Tom Igoe
17 modified 21 Jan 2014
18 by Federico Vanzati
19
20 http://arduino.cc/en/Tutorial/WebClientRepeating
21 This code is in the public domain.
22
23 */
24
25#include <SPI.h>
26#include <Ethernet2.h>
27
28// assign a MAC address for the ethernet controller.
29// fill in your address here:
30byte mac[] = {
31 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
32};
33// fill in an available IP address on your network here,
34// for manual configuration:
35IPAddress ip(192, 168, 1, 177);
36
37// fill in your Domain Name Server address here:
38IPAddress myDns(1, 1, 1, 1);
39
40// initialize the library instance:
41EthernetClient client;
42
43char server[] = "www.arduino.cc";
44//IPAddress server(64,131,82,241);
45
46unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
47const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
48// the "L" is needed to use long type numbers
49
50void setup() {
51 // start serial port:
52 Serial.begin(9600);
53 while (!Serial) {
54 ; // wait for serial port to connect. Needed for Leonardo only
55 }
56
57 // give the ethernet module time to boot up:
58 delay(1000);
59 // start the Ethernet connection using a fixed IP address and DNS server:
60 Ethernet.begin(mac, ip, myDns);
61 // print the Ethernet board/shield's IP address:
62 Serial.print("My IP address: ");
63 Serial.println(Ethernet.localIP());
64}
65
66void loop() {
67 // if there's incoming data from the net connection.
68 // send it out the serial port. This is for debugging
69 // purposes only:
70 if (client.available()) {
71 char c = client.read();
72 Serial.write(c);
73 }
74
75 // if ten seconds have passed since your last connection,
76 // then connect again and send data:
77 if (millis() - lastConnectionTime > postingInterval) {
78 httpRequest();
79 }
80
81}
82
83// this method makes a HTTP connection to the server:
84void httpRequest() {
85 // close any connection before send a new request.
86 // This will free the socket on the WiFi shield
87 client.stop();
88
89 // if there's a successful connection:
90 if (client.connect(server, 80)) {
91 Serial.println("connecting...");
92 // send the HTTP PUT request:
93 client.println("GET /latest.txt HTTP/1.1");
94 client.println("Host: www.arduino.cc");
95 client.println("User-Agent: arduino-ethernet");
96 client.println("Connection: close");
97 client.println();
98
99 // note the time that the connection was made:
100 lastConnectionTime = millis();
101 }
102 else {
103 // if you couldn't make a connection:
104 Serial.println("connection failed");
105 }
106}
107
108
Note: See TracBrowser for help on using the repository browser.