/* Web server sketch for IDE v1.0.5 and w5100/w5200 Originally posted November 2013 by SurferTim Last modified 4 DEC 2014 */ #include #include #include // comment out the next line to eliminate the Serial.print stuff // saves about 1.6K of program memory #define ServerDEBUG // this must be unique byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x06, 0xE4 }; IPAddress ip(192,168,0,6); // IP address, may need to change depending on network char ssid[] = "mySSID"; // your network SSID (name) char pass[] = "myPass"; // your network password //IPAddress gateway( 192,168,2,1 ); //IPAddress subnet( 255,255,255,0 ); WiFiServer server(80); const char legalChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890/.-_~"; unsigned int requestNumber = 0; unsigned long connectTime[MAX_SOCK_NUM]; int freeRam() { extern int __heap_start,*__brkval; int v; return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); } void setup() { Serial.begin(115200); // disable w5100 SPI while starting SD pinMode(10,OUTPUT); digitalWrite(10,HIGH); #ifdef ServerDEBUG Serial.print(F("Starting SD..")); #endif if(!SD.begin(4)) { #ifdef ServerDEBUG Serial.println(F("failed")); #endif } else { #ifdef ServerDEBUG Serial.println(F("ok")); #endif } WiFi.config(ip); WiFi.begin(ssid, pass); delay(2000); server.begin(); unsigned long thisTime = millis(); for(int i=0;i 1000) { // close connection client.stop(); #ifdef ServerDEBUG Serial.println("\r\nTimeout"); #endif }//if(loopCount > 1000) // delay 1ms for timeout timing delay(1); }//while (client.connected()) #ifdef ServerDEBUG Serial.println(F("disconnected")); #endif }//if(client) }//checkServer() void serialPrintTundR(char myParamBuffer[]) { int r,t; char *pch; pch = strtok(myParamBuffer,"&"); while(pch != NULL) { if(strncmp(pch,"t=",2) == 0) { t = atoi(pch+2); #ifdef ServerDEBUG Serial.print("t="); Serial.println(t,DEC); #endif }//if(strncmp(pch,"t=",2) == 0) if(strncmp(pch,"r=",2) == 0) { r = atoi(pch+2); #ifdef ServerDEBUG Serial.print("r="); Serial.println(r,DEC); #endif }//if(strncmp(pch,"r=",2) == 0) pch = strtok(NULL,"& "); }//while(pch != NULL) } void noGetMethod(WiFiClient myClient,char my_tBuf[]) { strcpy_P(my_tBuf,PSTR("")); myClient.write(my_tBuf); strcpy_P(my_tBuf,PSTR("

TEST

")); myClient.write(my_tBuf); strcpy_P(my_tBuf,PSTR("T:
")); myClient.write(my_tBuf); strcpy_P(my_tBuf,PSTR("R:
")); myClient.write(my_tBuf); } boolean checkFile(WiFiClient myClient,char myFileName[],char myFileType[],char my_tBuf[],char myRequestBuffer[],char myMethodBuffer[]) { // if dynamic page name if(strcmp(myRequestBuffer,"/MYTEST.PHP") == 0) { #ifdef ServerDEBUG Serial.println(F("dynamic page")); #endif }//if(strcmp(requestBuffer,"/MYTEST.PHP") else { if(strcmp(myFileName,"/") == 0) { strcpy(myFileName,"/index.htm"); strcpy(myFileType,"HTM"); #ifdef ServerDEBUG Serial.print(F("Home page ")); #endif }//if(strcmp(fileName,"/") == 0) #ifdef ServerDEBUG Serial.println(F("SD file")); #endif if(strlen(myFileName) > 30) { #ifdef ServerDEBUG Serial.println(F("filename too long")); #endif sendBadRequest(myClient); return false; }//if(strlen(fileName) > 30) else if(strlen(myFileType) > 3 || strlen(myFileType) < 1) { #ifdef ServerDEBUG Serial.println(F("file type invalid size")); #endif sendBadRequest(myClient); return false; }//else von if(strlen(fileName) > 30) else { #ifdef ServerDEBUG Serial.println(F("filename format ok")); #endif if(SD.exists(myFileName)) { return true; }//if(SD.exists(fileName)) else { #ifdef ServerDEBUG Serial.println(F("File not found")); #endif sendFileNotFound(myClient); return false; }//else von if(SD.exists(fileName)) } }//else von if(strcmp(requestBuffer,"/MYTEST.PHP") } boolean checkRequest(int *myScanCount,WiFiClient myClient,char my_tBuf[],char myMethodBuffer[],char myRequestBuffer[],char myParamBuffer[],char myProtocolBuffer[],char myFileName[],char myFileType[]) { if(*myScanCount != 3) { #ifdef ServerDEBUG Serial.println(F("bad request")); #endif sendBadRequest(myClient); return false; }//if(scanCount != 3) char* pch = strtok(myRequestBuffer,"?"); if(pch != NULL) { strncpy(myFileName,pch,31); strncpy(my_tBuf,pch,31); pch = strtok(NULL,"?"); if(pch != NULL) { strcpy(myParamBuffer,pch); }//if(pch != NULL) else myParamBuffer[0] = 0; }//if(pch != NULL) strtoupper(myRequestBuffer); strtoupper(my_tBuf); for(int x = 0; x < strlen(myRequestBuffer); x++) { if(strchr(legalChars,myRequestBuffer[x]) == NULL) { Serial.println(F("bad character")); sendBadRequest(myClient); return false; }//if(strchr(legalChars,requestBuffer[x]) == NULL) }//for(int x = 0; x < strlen(requestBuffer); x++) #ifdef ServerDEBUG Serial.print(F("file = ")); Serial.println(myRequestBuffer); #endif pch = strtok(my_tBuf,"."); if(pch != NULL) { pch = strtok(NULL,"."); if(pch != NULL) strncpy(myFileType,pch,4); else myFileType[0] = 0; #ifdef ServerDEBUG Serial.print(F("file type = ")); Serial.println(myFileType); #endif }//if(pch != NULL) #ifdef ServerDEBUG Serial.print(F("method = ")); Serial.println(myMethodBuffer); #endif if(strcmp(myMethodBuffer,"GET") != 0 && strcmp(myMethodBuffer,"HEAD") != 0) { sendBadRequest(myClient); return false; }//if(strcmp(methodBuffer,"GET") != 0 && strcmp(methodBuffer,"HEAD") != 0) #ifdef ServerDEBUG Serial.print(F("params = ")); Serial.println(myParamBuffer); Serial.print(F("protocol = ")); Serial.println(myProtocolBuffer); #endif return true; } void openFile(WiFiClient myClient, char myFileName[],char myFileType[],char my_tBuf[],char myMethodBuffer[]) { int clientCount = 0; #ifdef ServerDEBUG // SRAM check Serial.print(F("SRAM = ")); Serial.println(freeRam()); Serial.print(F("file found..")); #endif File myFile = SD.open(myFileName); if(!myFile) { #ifdef ServerDEBUG Serial.println(F("open error")); #endif sendFileNotFound(myClient); return; }//if(!myFile) #ifdef ServerDEBUG else Serial.print(F("opened..")); #endif strcpy_P(my_tBuf,PSTR("HTTP/1.0 200 OK\r\nContent-Type: ")); // client.write(tBuf); // client.print(F("HTTP/1.0 200 OK\r\nContent-Type: ")); // send Content-Type if(strcmp(myFileType,"HTM") == 0) strcat_P(my_tBuf,PSTR("text/html")); else if(strcmp(myFileType,"PHP") == 0) strcat_P(my_tBuf,PSTR("text/html")); else if(strcmp(myFileType,"TXT") == 0) strcat_P(my_tBuf,PSTR("text/plain")); else if(strcmp(myFileType,"CSS") == 0) strcat_P(my_tBuf,PSTR("text/css")); else if(strcmp(myFileType,"GIF") == 0) strcat_P(my_tBuf,PSTR("image/gif")); else if(strcmp(myFileType,"JPG") == 0) strcat_P(my_tBuf,PSTR("image/jpeg")); else if(strcmp(myFileType,"JS") == 0) strcat_P(my_tBuf,PSTR("application/javascript")); else if(strcmp(myFileType,"ICO") == 0) strcat_P(my_tBuf,PSTR("image/x-icon")); else if(strcmp(myFileType,"PNG") == 0) strcat_P(my_tBuf,PSTR("image/png")); else if(strcmp(myFileType,"PDF") == 0) strcat_P(my_tBuf,PSTR("application/pdf")); else if(strcmp(myFileType,"ZIP") == 0) strcat_P(my_tBuf,PSTR("application/zip")); else strcat_P(my_tBuf,PSTR("text/plain")); strcat_P(my_tBuf,PSTR("\r\nConnection: close\r\n\r\n")); myClient.write(my_tBuf); if(strcmp(myMethodBuffer,"GET") == 0) { #ifdef ServerDEBUG Serial.print(F("send..")); #endif while(myFile.available()) { my_tBuf[clientCount] = myFile.read(); clientCount++; my_tBuf[clientCount] = 0; if(clientCount > 63) { myClient.write((byte*)my_tBuf,64); clientCount = 0; }//if(clientCount > 63) }//while(myFile.available()) if(clientCount > 0) { myClient.write((byte*)my_tBuf,clientCount); }//if(clientCount > 0) }//if(strcmp(methodBuffer,"GET") == 0) myFile.close(); #ifdef ServerDEBUG Serial.println(F("closed")); #endif myClient.stop(); #ifdef ServerDEBUG Serial.println(F("disconnected")); #endif } void sendFileNotFound(WiFiClient thisClient) { char tBuf[64]; strcpy_P(tBuf,PSTR("HTTP/1.0 404 File Not Found\r\n")); thisClient.write(tBuf); strcpy_P(tBuf,PSTR("Content-Type: text/html\r\nConnection: close\r\n\r\n")); thisClient.write(tBuf); strcpy_P(tBuf,PSTR("

FILE NOT FOUND

")); thisClient.write(tBuf); thisClient.stop(); #ifdef ServerDEBUG Serial.println(F("disconnected")); #endif } void sendBadRequest(WiFiClient thisClient) { char tBuf[64]; strcpy_P(tBuf,PSTR("HTTP/1.0 400 Bad Request\r\n")); thisClient.write(tBuf); strcpy_P(tBuf,PSTR("Content-Type: text/html\r\nConnection: close\r\n\r\n")); thisClient.write(tBuf); strcpy_P(tBuf,PSTR("

BAD REQUEST

")); thisClient.write(tBuf); thisClient.stop(); #ifdef ServerDEBUG Serial.println(F("disconnected")); #endif } void strtoupper(char* aBuf) { for(int x = 0; x