1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-13 23:48:46 +01:00
Arduino/libraries/Ethernet/examples/WebServer/WebServer.ino

101 lines
2.9 KiB
Arduino
Raw Normal View History

2008-09-08 22:05:31 +02:00
/*
Web Server
2010-07-25 18:27:38 +02:00
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
2010-07-25 18:27:38 +02:00
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
2010-07-25 18:27:38 +02:00
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
2008-09-08 22:05:31 +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:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress 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
2010-07-25 18:27:38 +02:00
// (port 80 is default for HTTP):
EthernetServer server(80);
2008-09-08 22:05:31 +02:00
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
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();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
2008-09-08 22:05:31 +02:00
}
void loop() {
2010-07-25 18:27:38 +02:00
// listen for incoming clients
EthernetClient client = server.available();
2008-09-08 22:05:31 +02:00
if (client) {
Serial.println("new client");
2008-09-08 22:05:31 +02:00
// 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();
Serial.write(c);
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");
2013-04-17 11:37:31 +02:00
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
2008-09-08 22:05:31 +02:00
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
2008-09-08 22:05:31 +02:00
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
2008-09-08 22:05:31 +02:00
client.print("analog input ");
client.print(analogChannel);
2008-09-08 22:05:31 +02:00
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
2008-09-08 22:05:31 +02:00
}
client.println("</html>");
2008-09-08 22:05:31 +02:00
break;
}
if (c == '\n') {
2010-07-25 18:27:38 +02:00
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
2010-07-25 18:27:38 +02:00
// you've gotten a character on the current line
currentLineIsBlank = false;
2008-09-08 22:05:31 +02: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();
2013-07-29 19:40:51 +02:00
Serial.println("client disconnected");
2008-09-08 22:05:31 +02:00
}
}