Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SD.h>
- #include <EthernetV2_0.h>
- #include <avr/pgmspace.h>
- #include <MemoryFree.h>
- #include <avr/wdt.h>
- #define W5200_CS 10
- #define SDCARD_CS 4
- #define REQ_BUF_SZ 20
- #define SERIAL_BUF_SZ 64
- File indexFile;
- boolean outputFile;
- boolean debugMode = true;
- char buffer[80];
- PROGMEM prog_char indexPage[] = "index.htm";
- PROGMEM prog_char playerPage[] = "player.htm";
- PROGMEM prog_char logPage[] = "log.htm";
- PROGMEM prog_char playerCsv[] = "player.csv";
- PROGMEM prog_char newline[] = "\n";
- PROGMEM prog_char breakline[] = "<br>";
- PROGMEM prog_char space[] = " ";
- PROGMEM prog_char comma[] = ",";
- PROGMEM prog_char htmResponse[] = "HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close\n";
- PROGMEM prog_char picResponse[] = "HTTP/1.1 200 OK\n";
- PROGMEM prog_char missingResponse[] = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n<h2>File Not Found!</h2>\n";
- // MAC address from Ethernet shield sticker under board
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- //IPAddress ip(192,168,1,177);
- IPAddress ip(10,0,0,177);
- EthernetServer server(80); // create a server at port 80
- void setup()
- {
- Serial.begin(115200);
- if (debugMode) Serial.println(F("Initializing SD card..."));
- // setup to use ethernet shield and SD card
- pinMode(W5200_CS, OUTPUT);
- // disconnect the W5200 card
- digitalWrite(W5200_CS, HIGH);
- pinMode(SDCARD_CS,OUTPUT);
- if (!SD.begin(SDCARD_CS)) {
- Serial.println(F("SD card initialization failed!"));
- return;
- }
- digitalWrite(SDCARD_CS, LOW);
- indexFile = SD.open(strcpy_P(buffer, (char*) indexPage), FILE_WRITE);
- if (!indexFile) Serial.println(F("SD card file error!"));
- if(SD.remove(strcpy_P(buffer, (char*) playerPage))) {
- if (debugMode) Serial.println(F("clearing player htm"));
- indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
- if (indexFile) {
- indexFile.close();
- } else {
- if (debugMode) Serial.println(F("could not create player htm"));
- }
- } else {
- if (debugMode) Serial.println(F("could not remove player htm"));
- }
- // if(SD.remove(strcpy_P(buffer, (char*) playerCsv))) {
- // if (debugMode) Serial.println(F("clearing player csv"));
- // indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
- // if (indexFile) {
- // indexFile.close();
- // } else {
- // if (debugMode) Serial.println(F("could not create player csv"));
- // }
- // } else {
- // if (debugMode) Serial.println(F("could not remove player csv"));
- // }
- digitalWrite(SDCARD_CS, HIGH);
- Ethernet.begin(mac, ip);
- //digitalWrite(W5200_CS, HIGH);
- server.begin();
- if (debugMode) Serial.print(F("server is at "));
- if (debugMode) Serial.println(Ethernet.localIP());
- }
- void loop()
- {
- //all webserver code is in here
- EthernetClient client = server.available(); // try to get client
- //here is the USB snooper that is looking for data from Boomer
- // send data only when you receive data:
- if (Serial.available() > 0) {
- //delay(100); // let the buffer fill up
- while (Serial.available() > 0) {
- bufferIncomingSerial();
- Serial.flush();
- }
- }
- if (client) { // got client?
- char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
- char req_index = 0; // index into HTTP_req buffer
- if (debugMode) Serial.println(F("new client"));
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) { // client data available to read
- char c = client.read();
- if (req_index < (REQ_BUF_SZ - 1)) {
- HTTP_req[req_index] = c; // save HTTP request character
- req_index++;
- }
- if (c == '\n' && currentLineIsBlank) {
- if (StrContains(HTTP_req, "GET")) {
- // send appropriate header data to client
- if (StrContains(HTTP_req, ".jpg") || StrContains(HTTP_req, ".gif")) {
- client.println(strcpy_P(buffer, (char*) picResponse));
- loadGetPage(client,HTTP_req);
- } else {
- client.println(strcpy_P(buffer, (char*) htmResponse));
- loadGetPage(client,HTTP_req);
- }
- }
- break;
- // every line of text received from the client ends with \r\n
- }
- if (c == '\n') {
- // last character on line of received text
- // starting new line with next character read
- currentLineIsBlank = true;
- } else if (c != '\r') {
- // a text character was received from client
- currentLineIsBlank = false;
- }
- } // end if (client.available())
- } // end while (client.connected())
- delay(25); // give the web browser time to receive the data
- client.stop(); // close the connection
- if (debugMode) Serial.println(F("client disconnected"));
- } // end if (client)
- }
- void loadGetPage (EthernetClient client, char *data)
- {
- char *pointerOne, *i;
- //int bufferCount;
- //char localBuffer[16];
- // parse HTTP GET
- pointerOne = strtok_r(data,"GET /",&i);
- if (debugMode) {
- Serial.print(F("Parsing HTTP Request: "));
- Serial.println(pointerOne);
- }
- if (StrContains(data, "GET / ")) {
- // send index.html on GET /
- if (SD.exists(strcpy_P(buffer, (char*) indexPage))) {
- indexFile = SD.open(strcpy_P(buffer, (char*) indexPage)); // open web page file
- outputFile = true;
- } else {
- outputFile = false;
- }
- } else {
- // send web page after GET /----
- if (SD.exists(pointerOne)) {
- indexFile = SD.open(pointerOne); // open web page file
- outputFile = true;
- } else {
- outputFile = false;
- }
- }
- // don't try and output if there is nothing there, the shield gets stuck sometimes
- if (indexFile and outputFile) {
- while(indexFile.available()) {
- // need to buffer output
- client.write(indexFile.read()); // send web page to client
- }
- //client.write(localBuffer,bufferCount-1); // send web page to client
- indexFile.close();
- } else {
- // failed
- if (debugMode) Serial.println(F("error opening file"));
- }
- // only output directly to client after the SD card is disabled
- if (!outputFile) {
- // everything else is a 404
- client.println(strcpy_P(buffer, (char*) missingResponse));
- outputFile=false;
- }
- }
- // this function will receive available data from the 64B serial buffer
- // it also decide whether or not to process the data depending on whether
- // it is a command or the file to go to the htm output
- void bufferIncomingSerial ()
- {
- int bufferLength;
- char serialBuffer[SERIAL_BUF_SZ] = {0};
- StrClear(serialBuffer,SERIAL_BUF_SZ);
- bufferLength = Serial.readBytesUntil('@', serialBuffer,SERIAL_BUF_SZ);
- if (debugMode) {
- Serial.print(bufferLength);
- Serial.println(F(" Bytes of serial data received"));
- }
- if (StrContains(serialBuffer, "COMMAND ")) {
- if (debugMode) Serial.println(F("Command received"));
- ProcessSerialCommand(serialBuffer);
- } else {
- if (debugMode) Serial.println(F("Data received"));
- ProcessSerialData(serialBuffer);
- } // if StrContains
- } // end of bufferIncomingSerial
- void ProcessSerialCommand (char * data)
- {
- char *opcode,*arg1,*i;
- opcode = strtok_r(data,"COMMAND ",&i);
- if (debugMode) {
- Serial.print(F("Opcode = "));
- Serial.println(opcode);
- }
- if (StrContains(opcode, "clear")) {
- if(SD.remove(strcpy_P(buffer, (char*) playerPage))) {
- if (debugMode) Serial.println(F("clearing player htm"));
- indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
- if (indexFile) {
- indexFile.close();
- } else {
- if (debugMode) Serial.println(F("could not create player htm"));
- }
- } else {
- if (debugMode) Serial.println(F("could not remove player htm"));
- }
- // if(SD.remove(strcpy_P(buffer, (char*) playerCsv))) {
- // if (debugMode) Serial.println(F("clearing player csv"));
- // indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
- // if (indexFile) {
- // indexFile.close();
- // } else {
- // if (debugMode) Serial.println(F("could not create player csv"));
- // }
- // } else {
- // if (debugMode) Serial.println(F("could not remove player csv"));
- // }
- } else if (StrContains(opcode, "uptime")) {
- if (debugMode) Serial.println(uptime(millis()));
- } else if (StrContains(opcode, "memory")) {
- if (debugMode) Serial.print(F("Free Ram: "));
- if (debugMode) Serial.println(freeMemory());
- } else if (StrContains(opcode, "reset")) {
- if (debugMode) Serial.println(F("Resetting "));
- delay(500);
- software_Reboot();
- } else if (StrContains(opcode, "debug")) {
- if (debugMode) {
- debugMode = false;
- Serial.println(F("Turning Debug Mode OFF"));
- } else {
- debugMode = true;
- Serial.println(F("Turning Debug Mode ON"));
- }
- } else {
- // process args depending on commands
- arg1 = strtok_r(NULL," ",&i);
- }
- } // end of ProcessSerialCommand
- // here to process incoming serial data that isn't a COMMAND
- // I am using strings to make replacing \n with <br> easier
- void ProcessSerialData (char* data)
- {
- //char *pointerOne, *i;
- //char localBuffer[75]={0};
- //StrClear(localBuffer,75);
- indexFile = SD.open(strcpy_P(buffer, (char*) playerPage),FILE_WRITE);
- if (indexFile) {
- if (debugMode) Serial.println(F("Writing to player.htm..."));
- // parse string looking for \n and insert <br>
- // into serial stream for HTML
- indexFile.write(data);
- indexFile.print("<br>");
- indexFile.close();
- //StrClear(localBuffer,75);
- }
- else {
- //failed.
- if (debugMode) Serial.println(F("error opening player.htm"));
- }
- // indexFile = SD.open(strcpy_P(buffer, (char*) playerCsv),FILE_WRITE);
- // if (indexFile) {
- // if (debugMode) Serial.println(F("Writing to player.csv..."));
- // // parse string looking for " " and insert ","
- // // into serial stream for csv
- // pointerOne = strtok_r(data,strcpy_P(buffer, (char*) space),&i);
- // if (pointerOne) {
- // //Serial.println(F("newline character found"));
- // //Serial.println(pointerOne);
- // strcpy(localBuffer, pointerOne);
- // strcat(localBuffer, strcpy_P(buffer, (char*) comma));
- // while (pointerOne != NULL) {
- // pointerOne = strtok_r(NULL,strcpy_P(buffer, (char*) space),&i);
- // if (pointerOne) {
- // strcat(localBuffer, pointerOne);
- // strcat(localBuffer, strcpy_P(buffer, (char*) comma));
- // } else {
- // break;
- // }
- // }
- // indexFile.write(localBuffer);
- // indexFile.close();
- // } else {
- // strcpy(localBuffer, data);
- // strcat(localBuffer, strcpy_P(buffer, (char*) comma));
- // indexFile.write(localBuffer);
- // indexFile.close();
- // }
- // StrClear(localBuffer,75);
- // }
- // else {
- // //failed.
- // if (debugMode) Serial.println(F("error opening player.csv"));
- // }
- } // end of process_data
- // sets every element of str to 0 (clears array)
- void StrClear(char *str, char length)
- {
- for (int i = 0; i < length; i++) {
- str[i] = 0;
- }
- }
- // searches for the string sfind in the string str
- // returns 1 if string found
- // returns 0 if string not found
- char StrContains(char *str, char *sfind){
- char found = 0;
- char index = 0;
- char len;
- len = strlen(str);
- if (strlen(sfind) > len) return 0;
- while (index < len) {
- if (str[index] == sfind[found]) {
- found++;
- if (strlen(sfind) == found) {
- return 1;
- }
- } else {
- found = 0;
- }
- index++;
- }
- return 0;
- }
- char *uptime(unsigned long milli)
- {
- static char _return[32];
- unsigned long secs=milli/1000, mins=secs/60;
- unsigned int hours=mins/60, days=hours/24;
- milli-=secs*1000;
- secs-=mins*60;
- mins-=hours*60;
- hours-=days*24;
- sprintf(_return,"Uptime %d days %2.2d:%2.2d:%2.2d.%3.3d", (byte)days, (byte)hours, (byte)mins, (byte)secs, (int)milli);
- return _return;
- }
- void software_Reboot()
- {
- wdt_enable(WDTO_15MS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement