1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-04 15:24:12 +01:00
Arduino/hardware/arduino/avr/libraries/Ethernet/examples/WebServer/WebServer.ino

102 lines
3.0 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.
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
modified 9 Apr 2012
by Tom Igoe
2010-07-25 18:27:38 +02:00
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
// (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");
client.println("Connection: close");
2008-09-08 22:05:31 +02:00
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
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') {
// 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();
Serial.println("client disonnected");
2008-09-08 22:05:31 +02:00
}
}