2008-09-08 22:05:31 +02:00
|
|
|
/*
|
2010-07-25 18:27:38 +02:00
|
|
|
Web Server
|
|
|
|
|
|
|
|
A simple web server that shows the value of the analog input pins.
|
|
|
|
using an Arduino Wiznet Ethernet shield.
|
|
|
|
|
|
|
|
Circuit:
|
|
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
|
|
|
* Analog inputs attached to pins A0 through A5 (optional)
|
|
|
|
|
|
|
|
created 18 Dec 2009
|
|
|
|
by David A. Mellis
|
|
|
|
|
2008-09-08 22:05:31 +02:00
|
|
|
*/
|
|
|
|
|
2010-08-02 20:59:44 +02:00
|
|
|
#include <SPI.h>
|
2008-09-08 22:05:31 +02:00
|
|
|
#include <Ethernet.h>
|
|
|
|
|
2010-07-25 18:27:38 +02:00
|
|
|
// Enter a MAC address and IP address for your controller below.
|
|
|
|
// The IP address will be dependent on your local network:
|
2008-09-08 22:05:31 +02:00
|
|
|
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
2010-07-25 18:27:38 +02:00
|
|
|
byte ip[] = { 192,168,1, 177 };
|
2008-09-08 22:05:31 +02:00
|
|
|
|
2010-07-25 18:27:38 +02:00
|
|
|
// Initialize the Ethernet server library
|
|
|
|
// with the IP address and port you want to use
|
|
|
|
// (port 80 is default for HTTP):
|
2008-09-08 22:05:31 +02:00
|
|
|
Server server(80);
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2010-07-25 18:27:38 +02:00
|
|
|
// start the Ethernet connection and the server:
|
2008-09-08 22:05:31 +02:00
|
|
|
Ethernet.begin(mac, ip);
|
|
|
|
server.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2010-07-25 18:27:38 +02:00
|
|
|
// listen for incoming clients
|
2008-09-08 22:05:31 +02:00
|
|
|
Client client = server.available();
|
|
|
|
if (client) {
|
|
|
|
// an http request ends with a blank line
|
2010-07-25 18:27:38 +02:00
|
|
|
boolean currentLineIsBlank = true;
|
2008-09-08 22:05:31 +02:00
|
|
|
while (client.connected()) {
|
|
|
|
if (client.available()) {
|
|
|
|
char c = client.read();
|
2010-07-25 18:27:38 +02:00
|
|
|
// if you've gotten to the end of the line (received a newline
|
2008-09-08 22:05:31 +02:00
|
|
|
// character) and the line is blank, the http request has ended,
|
2010-07-25 18:27:38 +02:00
|
|
|
// so you can send a reply
|
|
|
|
if (c == '\n' && currentLineIsBlank) {
|
2008-09-08 22:05:31 +02:00
|
|
|
// send a standard http response header
|
|
|
|
client.println("HTTP/1.1 200 OK");
|
|
|
|
client.println("Content-Type: text/html");
|
|
|
|
client.println();
|
2010-07-25 18:27:38 +02:00
|
|
|
|
2008-09-08 22:05:31 +02:00
|
|
|
// output the value of each analog input pin
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
client.print("analog input ");
|
|
|
|
client.print(i);
|
|
|
|
client.print(" is ");
|
|
|
|
client.print(analogRead(i));
|
|
|
|
client.println("<br />");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
2010-07-25 18:27:38 +02:00
|
|
|
// you're starting a new line
|
|
|
|
currentLineIsBlank = true;
|
|
|
|
}
|
|
|
|
else if (c != '\r') {
|
|
|
|
// you've gotten a character on the current line
|
|
|
|
currentLineIsBlank = false;
|
2008-09-08 22:05:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-14 17:55:23 +01:00
|
|
|
// give the web browser time to receive the data
|
|
|
|
delay(1);
|
2010-07-25 18:27:38 +02:00
|
|
|
// close the connection:
|
2008-09-08 22:05:31 +02:00
|
|
|
client.stop();
|
|
|
|
}
|
2010-07-25 18:27:38 +02:00
|
|
|
}
|