waterlubber

Webserver Code-WIP

Oct 22nd, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. /*Advanced Server Prototype by Paul Bartolemea
  2. Programmed in C++
  3.  
  4. v1.0-Initial Release-Needs bugfix.
  5.  
  6. It is reccommended to use DCHP Reserevation
  7. Licensed on Creative Commons 3.0 BY-NC-SA License
  8. */
  9.  
  10. #include <SPI.h>     //The SPI Bus
  11. #include <Ethernet.h>//Ethernet Chip stuff
  12. #include <SD.h>      //SD Card
  13. byte mac[] = {
  14.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Unique identifier that...I can pick!
  15. IPAddress ip(192,168,1,130); //IP Adress that...I picked!
  16. EthernetServer server(80); //It's important.
  17. char current = 'x'; //Current Parsing Char //X is a placeholder
  18. char FileName[8]; //You'll see
  19.  
  20. void setup()
  21. {
  22.   Serial.begin(57600); //Starts up the Serial at a baud rate of 9600
  23.   Ethernet.begin(mac, ip); //Starts the Ethernet connection
  24.   server.begin(); //Boot! //Begins the server
  25.   SD.begin(4); //Start reading from that SD Card there
  26.   pinMode(4, OUTPUT); //Important for SD sheild
  27.   Serial.print("IP is at: "); //Prints that text
  28.   Serial.println(ip); //Prints the IP
  29. }
  30. void loop() {
  31.   EthernetClient client = server.available(); //No idea what this does, but it's neccessary! (I think it checks to see if theirs a client)
  32.   if(client) { //Anyone Home?
  33.     Serial.println("\nRequest Recieved!"); //serial stuff
  34.     while (client.connected()) {
  35.       char Packet[22]; //<--21 long!
  36.       if (client.available()) { //Anyone there>?
  37.       char c = client.peek(); //Peeking is useful. It doesn't advance the characters, unlike client.read
  38.         for(int x = 0; c != '/n' && x<22; x++) { //The c != /n makes sure request is recieved
  39.           c = client.read(); //Starts loading some chars
  40.           Packet[x] = c; //Adds the char to the packed in the step in the for loop (i.e, step 4 pu in array slot 3()
  41.           Serial.print("RequestChar: ");
  42.           Serial.write(c);
  43.           Serial.print(' ');
  44.         }
  45.         if(Packet[0] == 'G' && Packet[1] == 'E' && Packet[2] == 'T') { //Is it a GET request?
  46.           for(int x=5; current != ' '; x++) //Checks to see if your not at the end of the packet
  47.           {
  48.             FileName[x-5] = Packet[x]; //Writes 5th digit of reqeust to first char in packet array, skipping GET
  49.             current = Packet[x+1]; //Advances the current working digit.
  50.           }
  51.         }
  52.         Serial.println(); //Quick space here.
  53.         Serial.println("File name is: "); //Serial Debugging Filename Print
  54.         Serial.println(FileName);
  55.         //Rare Occurence that a forgetful person uses the site
  56.         if(FileName == " HTTP/1. ") {
  57.           File myFile=SD.open("hom.htm"); //Remember
  58.           if(myFile) { //Print's a standard HTML Header
  59.             client.println("HTTP/1.1 200 OK");
  60.             client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
  61.             client.println("<!DOCTYPE HTML>");
  62.             while(myFile.available()) {
  63.               client.write(myFile.read());
  64.             }
  65.             myFile.close();
  66.           }
  67.         }
  68.         //////////////////////////
  69.         File myFile=SD.open(FileName);
  70.         if(myFile) {
  71.           client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
  72.           client.println("<!DOCTYPE HTML>");
  73.           while(myFile.available()) { //ISSUE HAPPENS HERE-Somehow it doesn't work...^must remember memorary types^
  74.             client.print(myFile.read()); //ISSUE LINE 2...supposed to send the char to the client...
  75.             Serial.print("ReplyChar: ");
  76.             Serial.write(myFile.read());
  77.             Serial.println(' ');
  78.           }
  79.           myFile.close();
  80.         }
  81.         else {
  82.           Serial.println("Oh Fiddlesticks!");
  83.           Serial.println("Something is broken:");
  84.           Serial.println();
  85.           Serial.println();
  86.           if(!SD.exists(FileName)) {
  87.             Serial.println("Oops! It seems that that file doesn't exist!");
  88.             Serial.println();
  89.             Serial.println();
  90.             client.println("HTTP/1.1 404 NOT FOUND");
  91.             client.println("Connection: close");
  92.           }
  93.         }
  94.       }
  95.     }
  96.   }
  97.   delay(5000); //Stop spammy broswers from spamming the poor duino.
  98. }
Advertisement
Add Comment
Please, Sign In to add comment