Guest User

Untitled

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