2008-09-08 22:05:31 +02:00
|
|
|
/*
|
2011-08-29 23:36:28 +02:00
|
|
|
Web Server
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-07-25 18:27:38 +02:00
|
|
|
A simple web server that shows the value of the analog input pins.
|
2013-10-21 09:58:40 +02:00
|
|
|
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)
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-07-25 18:27:38 +02:00
|
|
|
created 18 Dec 2009
|
|
|
|
by David A. Mellis
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2010-09-17 15:22:32 +02:00
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
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:
|
2013-10-21 09:58:40 +02:00
|
|
|
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
|
2013-10-21 09:58:40 +02:00
|
|
|
// with the IP address and port you want to use
|
2010-07-25 18:27:38 +02:00
|
|
|
// (port 80 is default for HTTP):
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetServer server(80);
|
2008-09-08 22:05:31 +02:00
|
|
|
|
2012-03-20 15:37:15 +01:00
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2012-03-20 15:37:15 +01:00
|
|
|
Serial.begin(9600);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial) {
|
2012-04-09 15:51:35 +02:00
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
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();
|
2012-03-20 15:37:15 +01:00
|
|
|
Serial.print("server is at ");
|
|
|
|
Serial.println(Ethernet.localIP());
|
2008-09-08 22:05:31 +02:00
|
|
|
}
|
|
|
|
|
2012-03-20 15:37:15 +01:00
|
|
|
|
|
|
|
void loop() {
|
2010-07-25 18:27:38 +02:00
|
|
|
// listen for incoming clients
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client = server.available();
|
2008-09-08 22:05:31 +02:00
|
|
|
if (client) {
|
2012-03-20 15:37:15 +01:00
|
|
|
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();
|
2012-03-20 15:37:15 +01:00
|
|
|
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
|
2013-10-21 09:58:40 +02:00
|
|
|
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
2008-09-08 22:05:31 +02:00
|
|
|
client.println();
|
2012-03-20 15:37:15 +01:00
|
|
|
client.println("<!DOCTYPE HTML>");
|
|
|
|
client.println("<html>");
|
2008-09-08 22:05:31 +02:00
|
|
|
// output the value of each analog input pin
|
2010-09-17 15:22:32 +02:00
|
|
|
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
|
2012-03-20 15:37:15 +01:00
|
|
|
int sensorReading = analogRead(analogChannel);
|
2008-09-08 22:05:31 +02:00
|
|
|
client.print("analog input ");
|
2010-09-17 15:22:32 +02:00
|
|
|
client.print(analogChannel);
|
2008-09-08 22:05:31 +02:00
|
|
|
client.print(" is ");
|
2012-03-20 15:37:15 +01:00
|
|
|
client.print(sensorReading);
|
2013-10-21 09:58:40 +02:00
|
|
|
client.println("<br />");
|
2008-09-08 22:05:31 +02:00
|
|
|
}
|
2012-03-20 15:37:15 +01: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;
|
2015-07-06 15:18:33 +02:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
2013-07-29 19:40:51 +02:00
|
|
|
Serial.println("client disconnected");
|
2008-09-08 22:05:31 +02:00
|
|
|
}
|
2011-01-16 21:11:50 +01:00
|
|
|
}
|
2012-03-20 15:37:15 +01:00
|
|
|
|