[136] | 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 9 Apr 2012
|
---|
| 14 | by Tom Igoe
|
---|
| 15 |
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #include <SPI.h>
|
---|
| 19 | #include <Ethernet2.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[] = {
|
---|
| 24 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
---|
| 25 | };
|
---|
| 26 | IPAddress ip(192, 168, 1, 177);
|
---|
| 27 |
|
---|
| 28 | // Initialize the Ethernet server library
|
---|
| 29 | // with the IP address and port you want to use
|
---|
| 30 | // (port 80 is default for HTTP):
|
---|
| 31 | EthernetServer server(80);
|
---|
| 32 |
|
---|
| 33 | void setup() {
|
---|
| 34 | // Open serial communications and wait for port to open:
|
---|
| 35 | Serial.begin(9600);
|
---|
| 36 | while (!Serial) {
|
---|
| 37 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | // start the Ethernet connection and the server:
|
---|
| 42 | Ethernet.begin(mac, ip);
|
---|
| 43 | server.begin();
|
---|
| 44 | Serial.print("server is at ");
|
---|
| 45 | Serial.println(Ethernet.localIP());
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | void loop() {
|
---|
| 50 | // listen for incoming clients
|
---|
| 51 | EthernetClient client = server.available();
|
---|
| 52 | if (client) {
|
---|
| 53 | Serial.println("new client");
|
---|
| 54 | // an http request ends with a blank line
|
---|
| 55 | boolean currentLineIsBlank = true;
|
---|
| 56 | while (client.connected()) {
|
---|
| 57 | if (client.available()) {
|
---|
| 58 | char c = client.read();
|
---|
| 59 | Serial.write(c);
|
---|
| 60 | // if you've gotten to the end of the line (received a newline
|
---|
| 61 | // character) and the line is blank, the http request has ended,
|
---|
| 62 | // so you can send a reply
|
---|
| 63 | if (c == '\n' && currentLineIsBlank) {
|
---|
| 64 | // send a standard http response header
|
---|
| 65 | client.println("HTTP/1.1 200 OK");
|
---|
| 66 | client.println("Content-Type: text/html");
|
---|
| 67 | client.println("Connection: close"); // the connection will be closed after completion of the response
|
---|
| 68 | client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
---|
| 69 | client.println();
|
---|
| 70 | client.println("<!DOCTYPE HTML>");
|
---|
| 71 | client.println("<html>");
|
---|
| 72 | // output the value of each analog input pin
|
---|
| 73 | for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
|
---|
| 74 | int sensorReading = analogRead(analogChannel);
|
---|
| 75 | client.print("analog input ");
|
---|
| 76 | client.print(analogChannel);
|
---|
| 77 | client.print(" is ");
|
---|
| 78 | client.print(sensorReading);
|
---|
| 79 | client.println("<br />");
|
---|
| 80 | }
|
---|
| 81 | client.println("</html>");
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 | if (c == '\n') {
|
---|
| 85 | // you're starting a new line
|
---|
| 86 | currentLineIsBlank = true;
|
---|
| 87 | }
|
---|
| 88 | else if (c != '\r') {
|
---|
| 89 | // you've gotten a character on the current line
|
---|
| 90 | currentLineIsBlank = false;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | // give the web browser time to receive the data
|
---|
| 95 | delay(1);
|
---|
| 96 | // close the connection:
|
---|
| 97 | client.stop();
|
---|
| 98 | Serial.println("client disconnected");
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|