root / arduino-1.0 / libraries / Ethernet / examples / WebServer / WebServer.ino @ 58d82c77
History | View | Annotate | Download (2.25 KB)
1 |
/* |
---|---|
2 |
Web Server |
3 |
|
4 |
A simple web server that shows the value of the analog input pins. |
5 |
using an Arduino Wiznet Ethernet shield. |
6 |
|
7 |
Circuit: |
8 |
* Ethernet shield attached to pins 10, 11, 12, 13 |
9 |
* Analog inputs attached to pins A0 through A5 (optional) |
10 |
|
11 |
created 18 Dec 2009 |
12 |
by David A. Mellis |
13 |
modified 4 Sep 2010 |
14 |
by Tom Igoe |
15 |
|
16 |
*/ |
17 |
|
18 |
#include <SPI.h> |
19 |
#include <Ethernet.h> |
20 |
|
21 |
// Enter a MAC address and IP address for your controller below. |
22 |
// The IP address will be dependent on your local network: |
23 |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
24 |
IPAddress ip(192,168,1, 177); |
25 |
|
26 |
// Initialize the Ethernet server library |
27 |
// with the IP address and port you want to use |
28 |
// (port 80 is default for HTTP): |
29 |
EthernetServer server(80); |
30 |
|
31 |
void setup() |
32 |
{ |
33 |
// start the Ethernet connection and the server: |
34 |
Ethernet.begin(mac, ip); |
35 |
server.begin(); |
36 |
} |
37 |
|
38 |
void loop() |
39 |
{ |
40 |
// listen for incoming clients |
41 |
EthernetClient client = server.available(); |
42 |
if (client) { |
43 |
// an http request ends with a blank line |
44 |
boolean currentLineIsBlank = true; |
45 |
while (client.connected()) { |
46 |
if (client.available()) { |
47 |
char c = client.read(); |
48 |
// if you've gotten to the end of the line (received a newline |
49 |
// character) and the line is blank, the http request has ended, |
50 |
// so you can send a reply |
51 |
if (c == '\n' && currentLineIsBlank) { |
52 |
// send a standard http response header |
53 |
client.println("HTTP/1.1 200 OK"); |
54 |
client.println("Content-Type: text/html"); |
55 |
client.println(); |
56 |
|
57 |
// output the value of each analog input pin |
58 |
for (int analogChannel = 0; analogChannel < 6; analogChannel++) { |
59 |
client.print("analog input "); |
60 |
client.print(analogChannel); |
61 |
client.print(" is "); |
62 |
client.print(analogRead(analogChannel)); |
63 |
client.println("<br />"); |
64 |
} |
65 |
break; |
66 |
} |
67 |
if (c == '\n') { |
68 |
// you're starting a new line |
69 |
currentLineIsBlank = true; |
70 |
} |
71 |
else if (c != '\r') { |
72 |
// you've gotten a character on the current line |
73 |
currentLineIsBlank = false; |
74 |
} |
75 |
} |
76 |
} |
77 |
// give the web browser time to receive the data |
78 |
delay(1); |
79 |
// close the connection: |
80 |
client.stop(); |
81 |
} |
82 |
} |