Advertisement
Alex-Digital

Webserver Arduino

Apr 4th, 2013
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.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,0,63);
  9.  
  10. // Initialize the Ethernet server library
  11. // with the IP address and port you want to use
  12. // (port 80 is default for HTTP):
  13. EthernetServer server(80);
  14.  
  15. void setup() {
  16.  
  17.   // start the Ethernet connection and the server:
  18.   Ethernet.begin(mac, ip);
  19.   server.begin();
  20.  
  21. }
  22.  
  23.  
  24. void loop() {
  25.   // listen for incoming clients
  26.   EthernetClient client = server.available();
  27.   if (client) {
  28.  
  29.     // an http request ends with a blank line
  30.     boolean currentLineIsBlank = true;
  31.     while (client.connected()) {
  32.       if (client.available()) {
  33.         char c = client.read();
  34.  
  35.         // if you've gotten to the end of the line (received a newline
  36.         // character) and the line is blank, the http request has ended,
  37.         // so you can send a reply
  38.         if (c == '\n' && currentLineIsBlank) {
  39.           // send a standard http response header
  40.           client.println("HTTP/1.1 200 OK");
  41.           client.println("Content-Type: text/html");
  42.           client.println("Connnection: close");
  43.           client.println();
  44.           client.println("<!DOCTYPE HTML>");
  45.           client.println("<html>");
  46.           client.println("<b>Es funktioniert!</b>");
  47.           client.println("</html>");
  48.          
  49.         }
  50.         if (c == '\n') {
  51.           // you're starting a new line
  52.           currentLineIsBlank = true;
  53.         }
  54.         else if (c != '\r') {
  55.           // you've gotten a character on the current line
  56.           currentLineIsBlank = false;
  57.         }
  58.       }
  59.     }
  60.     // give the web browser time to receive the data
  61.     delay(1);
  62.     // close the connection:
  63.     client.stop();
  64.  
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement