Advertisement
Guest User

WebServer

a guest
Jan 9th, 2014
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. //#include <SPI.h> //Same result when keeping it uncommented
  2. #include <UIPEthernet.h>
  3.  
  4. // Enter a MAC address and IP address for your controller below.
  5. // The IP address will be dependent on your local network:
  6. byte mac[] = {
  7.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  8. IPAddress ip(192,168,1,140);
  9. //IPAddress gateway(192,168,1,1);  
  10. //IPAddress subnet(255, 255, 255, 0);
  11.  
  12. EthernetServer server(80);
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.  
  17.   // start the Ethernet connection and the server:
  18.   Ethernet.begin(mac, ip /*, gateway, subnet*/);
  19.   server.begin();
  20.   Serial.print("server is at ");
  21.   Serial.println(Ethernet.localIP());
  22. }
  23.  
  24.  
  25. void loop() {
  26.   // listen for incoming clients
  27.   EthernetClient client = server.available();
  28.   if (client) {
  29.     Serial.println("new client");
  30.     // an http request ends with a blank line
  31.     boolean currentLineIsBlank = true;
  32.     while (client.connected()) {
  33.       if (client.available()) {
  34.         char c = client.read();
  35.         Serial.write(c);
  36.         // if you've gotten to the end of the line (received a newline
  37.         // character) and the line is blank, the http request has ended,
  38.         // so you can send a reply
  39.         if (c == '\n' && currentLineIsBlank) {
  40.           // send a standard http response header
  41.           client.println("HTTP/1.1 200 OK");
  42.           client.println("Content-Type: text/html");
  43.           client.println("Connection: close");  // the connection will be closed after completion of the response
  44.       client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  45.           client.println();
  46.           client.println("<!DOCTYPE HTML>");
  47.           client.println("<html>");
  48.           // output the value of each analog input pin
  49.           for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  50.             int sensorReading = analogRead(analogChannel);
  51.             client.print("analog input ");
  52.             client.print(analogChannel);
  53.             client.print(" is ");
  54.             client.print(sensorReading);
  55.             client.println("<br />");      
  56.           }
  57.           client.println("</html>");
  58.           break;
  59.         }
  60.         if (c == '\n') {
  61.           // you're starting a new line
  62.           currentLineIsBlank = true;
  63.         }
  64.         else if (c != '\r') {
  65.           // you've gotten a character on the current line
  66.           currentLineIsBlank = false;
  67.         }
  68.       }
  69.     }
  70.     // give the web browser time to receive the data
  71.     delay(1);
  72.     // close the connection:
  73.     client.stop();
  74.     Serial.println("client disonnected");
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement