Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.39 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3. #include <EthernetV2_0.h>
  4. #include <avr/pgmspace.h>
  5. #include <MemoryFree.h>
  6. #include <avr/wdt.h>
  7.  
  8. #define W5200_CS  10
  9. #define SDCARD_CS 4
  10. #define REQ_BUF_SZ 20
  11. #define SERIAL_BUF_SZ 64
  12.  
  13. File indexFile;
  14. boolean outputFile;
  15. boolean debugMode = true;
  16. char buffer[80];
  17.  
  18. PROGMEM prog_char indexPage[] = "index.htm";
  19. PROGMEM prog_char playerPage[] = "player.htm";
  20. PROGMEM prog_char logPage[] = "log.htm";
  21. PROGMEM prog_char playerCsv[] = "player.csv";
  22. PROGMEM prog_char newline[] = "\n";
  23. PROGMEM prog_char breakline[] = "<br>";
  24. PROGMEM prog_char space[] = " ";
  25. PROGMEM prog_char comma[] = ",";
  26. PROGMEM prog_char htmResponse[] = "HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close\n";
  27. PROGMEM prog_char picResponse[] = "HTTP/1.1 200 OK\n";
  28. PROGMEM prog_char missingResponse[] = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n<h2>File Not Found!</h2>\n";
  29.  
  30. // MAC address from Ethernet shield sticker under board
  31. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  32. //IPAddress ip(192,168,1,177);
  33. IPAddress ip(10,0,0,177);
  34. EthernetServer server(80);  // create a server at port 80
  35.  
  36. void setup()
  37. {
  38.   Serial.begin(115200);
  39.   if (debugMode) Serial.println(F("Initializing SD card..."));
  40.   // setup to use ethernet shield and SD card
  41.   pinMode(W5200_CS, OUTPUT);
  42.   // disconnect the W5200 card
  43.   digitalWrite(W5200_CS, HIGH);
  44.  
  45.   pinMode(SDCARD_CS,OUTPUT);
  46.   if (!SD.begin(SDCARD_CS)) {
  47.     Serial.println(F("SD card initialization failed!"));
  48.     return;
  49.   }  
  50.   digitalWrite(SDCARD_CS, LOW);
  51.   indexFile = SD.open(strcpy_P(buffer, (char*) indexPage), FILE_WRITE);
  52.   if (!indexFile) Serial.println(F("SD card file error!"));
  53.   if(SD.remove(strcpy_P(buffer, (char*) playerPage))) {
  54.     if (debugMode) Serial.println(F("clearing player htm"));
  55.     indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
  56.     if (indexFile) {
  57.       indexFile.close();
  58.     } else {
  59.       if (debugMode) Serial.println(F("could not create player htm"));
  60.     }  
  61.   } else {
  62.     if (debugMode) Serial.println(F("could not remove player htm"));
  63.   }  
  64. //  if(SD.remove(strcpy_P(buffer, (char*) playerCsv))) {
  65. //    if (debugMode) Serial.println(F("clearing player csv"));
  66. //    indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
  67. //    if (indexFile) {
  68. //      indexFile.close();
  69. //    } else {
  70. //      if (debugMode) Serial.println(F("could not create player csv"));
  71. //    }  
  72. //  } else {
  73. //    if (debugMode) Serial.println(F("could not remove player csv"));
  74. //  }
  75.   digitalWrite(SDCARD_CS, HIGH);
  76.  
  77.   Ethernet.begin(mac, ip);
  78.   //digitalWrite(W5200_CS, HIGH);
  79.   server.begin();
  80.   if (debugMode) Serial.print(F("server is at "));
  81.   if (debugMode) Serial.println(Ethernet.localIP());
  82. }
  83.  
  84. void loop()
  85. {    
  86.   //all webserver code is in here
  87.   EthernetClient client = server.available();  // try to get client
  88.  
  89.   //here is the USB snooper that is looking for data from Boomer
  90.   // send data only when you receive data:
  91.   if (Serial.available() > 0) {
  92.     //delay(100); // let the buffer fill up
  93.     while (Serial.available() > 0) {    
  94.       bufferIncomingSerial();
  95.       Serial.flush();
  96.     }
  97.   }  
  98.        
  99.   if (client) {  // got client?
  100.     char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
  101.     char req_index = 0;              // index into HTTP_req buffer
  102.     if (debugMode) Serial.println(F("new client"));
  103.     boolean currentLineIsBlank = true;
  104.     while (client.connected()) {
  105.       if (client.available()) {   // client data available to read
  106.         char c = client.read();
  107.         if (req_index < (REQ_BUF_SZ - 1)) {
  108.           HTTP_req[req_index] = c;          // save HTTP request character
  109.           req_index++;
  110.         }
  111.         if (c == '\n' && currentLineIsBlank) {    
  112.           if (StrContains(HTTP_req, "GET")) {
  113.             // send appropriate header data to client
  114.             if (StrContains(HTTP_req, ".jpg") || StrContains(HTTP_req, ".gif")) {
  115.               client.println(strcpy_P(buffer, (char*) picResponse));
  116.               loadGetPage(client,HTTP_req);  
  117.             } else {
  118.               client.println(strcpy_P(buffer, (char*) htmResponse));
  119.               loadGetPage(client,HTTP_req);
  120.             }
  121.           }
  122.           break;
  123.           // every line of text received from the client ends with \r\n
  124.         }  
  125.         if (c == '\n') {
  126.           // last character on line of received text
  127.           // starting new line with next character read
  128.           currentLineIsBlank = true;
  129.         } else if (c != '\r') {
  130.           // a text character was received from client
  131.           currentLineIsBlank = false;
  132.         }
  133.       } // end if (client.available())          
  134.     } // end while (client.connected())        
  135.     delay(25);      // give the web browser time to receive the data
  136.     client.stop(); // close the connection
  137.     if (debugMode) Serial.println(F("client disconnected"));
  138.   } // end if (client)      
  139. }
  140.  
  141. void loadGetPage (EthernetClient client, char *data)
  142. {
  143.     char *pointerOne, *i;
  144.     //int bufferCount;
  145.     //char localBuffer[16];
  146.     // parse HTTP GET
  147.     pointerOne = strtok_r(data,"GET /",&i);
  148.     if (debugMode) {
  149.       Serial.print(F("Parsing HTTP Request: "));
  150.       Serial.println(pointerOne);
  151.     }
  152.     if (StrContains(data, "GET / ")) {
  153.       // send index.html on GET /
  154.       if (SD.exists(strcpy_P(buffer, (char*) indexPage))) {
  155.         indexFile = SD.open(strcpy_P(buffer, (char*) indexPage));        // open web page file
  156.         outputFile = true;
  157.       } else {
  158.         outputFile = false;
  159.       }  
  160.     } else {
  161.       // send web page after GET /----
  162.       if (SD.exists(pointerOne)) {
  163.         indexFile = SD.open(pointerOne);        // open web page file
  164.         outputFile = true;
  165.       } else {
  166.         outputFile = false;
  167.       }  
  168.     }
  169.     // don't try and output if there is nothing there, the shield gets stuck sometimes
  170.     if (indexFile and outputFile) {
  171.       while(indexFile.available()) {  
  172.         // need to buffer output
  173.         client.write(indexFile.read()); // send web page to client
  174.       }
  175.       //client.write(localBuffer,bufferCount-1); // send web page to client
  176.       indexFile.close();
  177.     } else {
  178.       // failed
  179.       if (debugMode) Serial.println(F("error opening file"));
  180.     }
  181.     // only output directly to client after the SD card is disabled
  182.     if (!outputFile) {
  183.       // everything else is a 404
  184.       client.println(strcpy_P(buffer, (char*) missingResponse));
  185.       outputFile=false;
  186.     }  
  187. }
  188.  
  189. // this function will receive available data from the 64B serial buffer
  190. // it also decide whether or not to process the data depending on whether
  191. // it is a command or the file to go to the htm output
  192. void bufferIncomingSerial ()
  193. {
  194.   int bufferLength;
  195.   char serialBuffer[SERIAL_BUF_SZ] = {0};
  196.   StrClear(serialBuffer,SERIAL_BUF_SZ);
  197.  
  198.   bufferLength = Serial.readBytesUntil('@', serialBuffer,SERIAL_BUF_SZ);
  199.   if (debugMode) {
  200.     Serial.print(bufferLength);
  201.     Serial.println(F(" Bytes of serial data received"));
  202.   }  
  203.   if (StrContains(serialBuffer, "COMMAND ")) {
  204.     if (debugMode) Serial.println(F("Command received"));
  205.     ProcessSerialCommand(serialBuffer);
  206.   } else {
  207.     if (debugMode) Serial.println(F("Data received"));
  208.     ProcessSerialData(serialBuffer);
  209.   } // if StrContains    
  210. } // end of bufferIncomingSerial  
  211.  
  212. void ProcessSerialCommand (char * data)
  213. {
  214.   char *opcode,*arg1,*i;
  215.  
  216.   opcode = strtok_r(data,"COMMAND ",&i);
  217.   if (debugMode) {
  218.     Serial.print(F("Opcode = "));
  219.     Serial.println(opcode);
  220.   }  
  221.   if (StrContains(opcode, "clear")) {
  222.     if(SD.remove(strcpy_P(buffer, (char*) playerPage))) {
  223.       if (debugMode) Serial.println(F("clearing player htm"));
  224.       indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
  225.       if (indexFile) {
  226.         indexFile.close();
  227.       } else {
  228.         if (debugMode) Serial.println(F("could not create player htm"));
  229.       }  
  230.     } else {
  231.       if (debugMode) Serial.println(F("could not remove player htm"));
  232.     }  
  233. //    if(SD.remove(strcpy_P(buffer, (char*) playerCsv))) {
  234. //      if (debugMode) Serial.println(F("clearing player csv"));
  235. //      indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
  236. //      if (indexFile) {
  237. //        indexFile.close();
  238. //      } else {
  239. //        if (debugMode) Serial.println(F("could not create player csv"));
  240. //      }  
  241. //    } else {
  242. //      if (debugMode) Serial.println(F("could not remove player csv"));
  243. //    }
  244.   } else if (StrContains(opcode, "uptime")) {
  245.     if (debugMode) Serial.println(uptime(millis()));  
  246.   } else if (StrContains(opcode, "memory")) {
  247.     if (debugMode) Serial.print(F("Free Ram: "));
  248.     if (debugMode) Serial.println(freeMemory());
  249.   } else if (StrContains(opcode, "reset")) {
  250.     if (debugMode) Serial.println(F("Resetting "));
  251.     delay(500);
  252.     software_Reboot();  
  253.   } else if (StrContains(opcode, "debug")) {
  254.     if (debugMode) {
  255.       debugMode = false;
  256.       Serial.println(F("Turning Debug Mode OFF"));
  257.     } else {
  258.       debugMode = true;
  259.       Serial.println(F("Turning Debug Mode ON"));
  260.     }
  261.   } else {
  262.     // process args depending on commands
  263.     arg1 = strtok_r(NULL," ",&i);
  264.   }  
  265. }  // end of ProcessSerialCommand
  266.  
  267. // here to process incoming serial data that isn't a COMMAND
  268. // I am using strings to make replacing \n with <br> easier
  269. void ProcessSerialData (char* data)
  270. {
  271.   //char *pointerOne, *i;
  272.   //char localBuffer[75]={0};
  273.   //StrClear(localBuffer,75);
  274.  
  275.   indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
  276.   if (indexFile) {
  277.     if (debugMode) Serial.println(F("Writing to player.htm..."));  
  278.     // parse string looking for \n and insert <br>
  279.     // into serial stream for HTML    
  280.     indexFile.write(data);    
  281.     indexFile.print("<br>");  
  282.     indexFile.close();
  283.     //StrClear(localBuffer,75);
  284.   }
  285.   else {
  286.     //failed.
  287.     if (debugMode) Serial.println(F("error opening player.htm"));
  288.   }
  289. //  indexFile = SD.open(strcpy_P(buffer, (char*) playerCsv),FILE_WRITE);
  290. //  if (indexFile) {
  291. //    if (debugMode) Serial.println(F("Writing to player.csv..."));  
  292. //    // parse string looking for " " and insert ","
  293. //    // into serial stream for csv
  294. //    pointerOne = strtok_r(data,strcpy_P(buffer, (char*) space),&i);
  295. //    if (pointerOne) {
  296. //      //Serial.println(F("newline character found"));
  297. //      //Serial.println(pointerOne);
  298. //      strcpy(localBuffer, pointerOne);
  299. //      strcat(localBuffer, strcpy_P(buffer, (char*) comma));
  300. //      while (pointerOne != NULL) {
  301. //        pointerOne = strtok_r(NULL,strcpy_P(buffer, (char*) space),&i);
  302. //        if (pointerOne) {
  303. //          strcat(localBuffer, pointerOne);
  304. //          strcat(localBuffer, strcpy_P(buffer, (char*) comma));
  305. //        } else {
  306. //          break;
  307. //        }  
  308. //      }
  309. //      indexFile.write(localBuffer);
  310. //      indexFile.close();  
  311. //    } else {
  312. //      strcpy(localBuffer, data);
  313. //      strcat(localBuffer, strcpy_P(buffer, (char*) comma));
  314. //      indexFile.write(localBuffer);
  315. //      indexFile.close();
  316. //    }  
  317. //    StrClear(localBuffer,75);
  318. //  }
  319. //  else {
  320. //    //failed.
  321. //    if (debugMode) Serial.println(F("error opening player.csv"));
  322. //  }
  323. }  // end of process_data
  324.  
  325. // sets every element of str to 0 (clears array)
  326. void StrClear(char *str, char length)
  327. {
  328.   for (int i = 0; i < length; i++) {
  329.     str[i] = 0;
  330.   }
  331. }
  332.  
  333. // searches for the string sfind in the string str
  334. // returns 1 if string found
  335. // returns 0 if string not found
  336. char StrContains(char *str, char *sfind){
  337.     char found = 0;
  338.     char index = 0;
  339.     char len;
  340.  
  341.     len = strlen(str);
  342.    
  343.     if (strlen(sfind) > len) return 0;
  344.     while (index < len) {
  345.         if (str[index] == sfind[found]) {
  346.             found++;
  347.             if (strlen(sfind) == found) {
  348.                 return 1;
  349.             }
  350.         } else {
  351.             found = 0;
  352.         }
  353.         index++;
  354.     }
  355.     return 0;
  356. }
  357.  
  358. char *uptime(unsigned long milli)
  359. {
  360.  static char _return[32];
  361.  unsigned long secs=milli/1000, mins=secs/60;
  362.  unsigned int hours=mins/60, days=hours/24;
  363.  milli-=secs*1000;
  364.  secs-=mins*60;
  365.  mins-=hours*60;
  366.  hours-=days*24;
  367.  sprintf(_return,"Uptime %d days %2.2d:%2.2d:%2.2d.%3.3d", (byte)days, (byte)hours, (byte)mins, (byte)secs, (int)milli);
  368.  return _return;
  369. }
  370.  
  371. void software_Reboot()
  372. {
  373.   wdt_enable(WDTO_15MS);
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement