Advertisement
MrHohenheim

sdweb

Mar 8th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4.  
  5. // MAC address from Ethernet shield sticker under board
  6. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  7. IPAddress ip(192, 168, 1, 104); // IP address, may need to change depending on network
  8. EthernetServer server(8123);  // create a server at port 80
  9.  
  10. File webFile;
  11.  
  12. void setup()
  13. {
  14.     Ethernet.begin(mac, ip);  // initialize Ethernet device
  15.     server.begin();           // start to listen for clients
  16.     Serial.begin(9600);       // for debugging
  17.    
  18.     // initialize SD card
  19.     Serial.println("Initializing SD card...");
  20.     if (!SD.begin(4)) {
  21.         Serial.println("ERROR - SD card initialization failed!");
  22.         return;    // init failed
  23.     }
  24.     Serial.println("SUCCESS - SD card initialized.");
  25.     // check for index.htm file
  26.     if (!SD.exists("index.htm")) {
  27.         Serial.println("ERROR - Can't find index.htm file!");
  28.         return;  // can't find index file
  29.     }
  30.     Serial.println("SUCCESS - Found index.htm file.");
  31. }
  32.  
  33. void loop()
  34. {
  35.     EthernetClient client = server.available();  // try to get client
  36.  
  37.     if (client) {  // got client?
  38.         boolean currentLineIsBlank = true;
  39.         while (client.connected()) {
  40.             if (client.available()) {   // client data available to read
  41.                 char c = client.read(); // read 1 byte (character) from client
  42.                 // last line of client request is blank and ends with \n
  43.                 // respond to client only after last line received
  44.                 if (c == '\n' && currentLineIsBlank) {
  45.                     // send a standard http response header
  46.                     client.println("HTTP/1.1 200 OK");
  47.                     client.println("Content-Type: text/html");
  48.                     client.println("Connection: close");
  49.                     client.println();
  50.                     // send web page
  51.                     webFile = SD.open("index.htm");        // open web page file
  52.                     if (webFile) {
  53.                         while(webFile.available()) {
  54.                             client.write(webFile.read()); // send web page to client
  55.                         }
  56.                         webFile.close();
  57.                     }
  58.                     break;
  59.                 }
  60.                 // every line of text received from the client ends with \r\n
  61.                 if (c == '\n') {
  62.                     // last character on line of received text
  63.                     // starting new line with next character read
  64.                     currentLineIsBlank = true;
  65.                 }
  66.                 else if (c != '\r') {
  67.                     // a text character was received from client
  68.                     currentLineIsBlank = false;
  69.                 }
  70.             } // end if (client.available())
  71.         } // end while (client.connected())
  72.         delay(1);      // give the web browser time to receive the data
  73.         client.stop(); // close the connection
  74.     } // end if (client)
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement