shdowmyst

webserverSD

Aug 9th, 2017
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5. #include <SPI.h>
  6. #include <SD.h>
  7.  
  8. const char* ssid = "ssid";
  9. const char* password = "password";
  10. const char* host = "esp8266sd";
  11.  
  12. ESP8266WebServer server(80);
  13.  
  14. static bool hasSD = false;
  15.  
  16. bool loadFromSdCard(String path){
  17.   String dataType = "text/plain";
  18.   if(path.endsWith("/")) path += "index.htm";
  19.  
  20.   if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
  21.   else if(path.endsWith(".htm")) dataType = "text/html";
  22.   else if(path.endsWith(".css")) dataType = "text/css";
  23.   else if(path.endsWith(".js")) dataType = "application/javascript";
  24.   else if(path.endsWith(".png")) dataType = "image/png";
  25.   else if(path.endsWith(".gif")) dataType = "image/gif";
  26.   else if(path.endsWith(".jpg")) dataType = "image/jpeg";
  27.   else if(path.endsWith(".ico")) dataType = "image/x-icon";
  28.   else if(path.endsWith(".xml")) dataType = "text/xml";
  29.   else if(path.endsWith(".pdf")) dataType = "application/pdf";
  30.   else if(path.endsWith(".zip")) dataType = "application/zip";
  31.  
  32.   File dataFile = SD.open(path.c_str());
  33.  
  34.   if (!dataFile)
  35.     {return false;}
  36.  
  37.  if (server.hasArg("download")) dataType = "application/octet-stream";
  38.  
  39.  if (server.streamFile(dataFile, dataType) != dataFile.size()) {
  40.   Serial.println("Sent less data than expected!");
  41.  }
  42.  
  43.   dataFile.close();
  44.   return true;
  45. }
  46.  
  47. void handleNotFound(){
  48.   if(hasSD && loadFromSdCard(server.uri())) return;
  49.   String message = "SDCARD Not Detected\n\n";
  50.   message += "URI: ";
  51.   message += server.uri();
  52.   message += "\nMethod: ";
  53.   message += (server.method() == HTTP_GET)?"GET":"POST";
  54.   message += "\nArguments: ";
  55.   message += server.args();
  56.   message += "\n";
  57.   for (uint8_t i=0; i<server.args(); i++){
  58.     message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
  59.   }
  60.   server.send(404, "text/plain", message);
  61.   Serial.print(message);
  62. }
  63.  
  64. void setup(void){
  65.   Serial.begin(74880);
  66.   Serial.setDebugOutput(true);
  67.   Serial.print("\n");
  68.   WiFi.begin(ssid, password);
  69.   Serial.print("Connecting to ");
  70.   Serial.println(ssid);
  71.  
  72.   // Wait for connection
  73.   uint8_t i = 0;
  74.   while (WiFi.status() != WL_CONNECTED && i++ < 20) {//wait 10 seconds
  75.     delay(500);
  76.   }
  77.   if(i == 21){
  78.     Serial.print("Could not connect to");
  79.     Serial.println(ssid);
  80.     while(1) delay(500);
  81.   }
  82.   Serial.print("Connected! IP address: ");
  83.   Serial.println(WiFi.localIP());
  84.  
  85.   if (MDNS.begin(host)) {
  86.     MDNS.addService("http", "tcp", 80);
  87.     Serial.println("MDNS responder started");
  88.     Serial.print("You can now connect to http://");
  89.     Serial.print(host);
  90.     Serial.println(".local");
  91.   }
  92.  
  93.   server.onNotFound(handleNotFound);
  94.  
  95.  
  96.   server.begin();
  97.   Serial.println("HTTP server started");
  98.  
  99.   if (SD.begin(SS)){
  100.      Serial.println("SD Card initialized.");
  101.      hasSD = true;
  102.   }
  103. }
  104.  
  105. void loop(void){
  106.   server.handleClient();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment