Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Advanced Server Prototype by Paul Bartolemea
- Programmed in C++
- v1.0-Initial Release-Needs bugfix.
- It is reccommended to use DCHP Reserevation
- Licensed on Creative Commons 3.0 BY-NC-SA License
- */
- #include <SPI.h> //The SPI Bus
- #include <Ethernet.h>//Ethernet Chip stuff
- #include <SD.h> //SD Card
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Unique identifier that...I can pick!
- IPAddress ip(192,168,1,130); //IP Adress that...I picked!
- EthernetServer server(80); //It's important.
- char current = 'x'; //Current Parsing Char //X is a placeholder
- char FileName[8]; //You'll see
- void setup()
- {
- Serial.begin(57600); //Starts up the Serial at a baud rate of 9600
- Ethernet.begin(mac, ip); //Starts the Ethernet connection
- server.begin(); //Boot! //Begins the server
- SD.begin(4); //Start reading from that SD Card there
- pinMode(4, OUTPUT); //Important for SD sheild
- Serial.print("IP is at: "); //Prints that text
- Serial.println(ip); //Prints the IP
- }
- void loop() {
- EthernetClient client = server.available(); //No idea what this does, but it's neccessary! (I think it checks to see if theirs a client)
- if(client) { //Anyone Home?
- Serial.println("\nRequest Recieved!"); //serial stuff
- while (client.connected()) {
- char Packet[22]; //<--21 long!
- if (client.available()) { //Anyone there>?
- char c = client.peek(); //Peeking is useful. It doesn't advance the characters, unlike client.read
- for(int x = 0; c != '/n' && x<22; x++) { //The c != /n makes sure request is recieved
- c = client.read(); //Starts loading some chars
- 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()
- Serial.print("RequestChar: ");
- Serial.write(c);
- Serial.print(' ');
- }
- if(Packet[0] == 'G' && Packet[1] == 'E' && Packet[2] == 'T') { //Is it a GET request?
- for(int x=5; current != ' '; x++) //Checks to see if your not at the end of the packet
- {
- FileName[x-5] = Packet[x]; //Writes 5th digit of reqeust to first char in packet array, skipping GET
- current = Packet[x+1]; //Advances the current working digit.
- }
- }
- Serial.println(); //Quick space here.
- Serial.println("File name is: "); //Serial Debugging Filename Print
- Serial.println(FileName);
- //Rare Occurence that a forgetful person uses the site
- if(FileName == " HTTP/1. ") {
- File myFile=SD.open("hom.htm"); //Remember
- if(myFile) { //Print's a standard HTML Header
- client.println("HTTP/1.1 200 OK");
- client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
- client.println("<!DOCTYPE HTML>");
- while(myFile.available()) {
- client.write(myFile.read());
- }
- myFile.close();
- }
- }
- //////////////////////////
- File myFile=SD.open(FileName);
- if(myFile) {
- client.print("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n");
- client.println("<!DOCTYPE HTML>");
- while(myFile.available()) { //ISSUE HAPPENS HERE-Somehow it doesn't work...^must remember memorary types^
- client.print(myFile.read()); //ISSUE LINE 2...supposed to send the char to the client...
- Serial.print("ReplyChar: ");
- Serial.write(myFile.read());
- Serial.println(' ');
- }
- myFile.close();
- }
- else {
- Serial.println("Oh Fiddlesticks!");
- Serial.println("Something is broken:");
- Serial.println();
- Serial.println();
- if(!SD.exists(FileName)) {
- Serial.println("Oops! It seems that that file doesn't exist!");
- Serial.println();
- Serial.println();
- client.println("HTTP/1.1 404 NOT FOUND");
- client.println("Connection: close");
- }
- }
- }
- }
- }
- delay(5000); //Stop spammy broswers from spamming the poor duino.
- }
Advertisement
Add Comment
Please, Sign In to add comment