|
| 1 | +/* |
| 2 | + SDWebServer - Example WebServer with SD Card backend for esp8266 |
| 3 | +
|
| 4 | + Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
| 5 | + This file is part of the ESP8266WebServer library for Arduino environment. |
| 6 | + |
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +
|
| 21 | + Have a FAT Formatted SD Card connected to the SPI port of the ESP8266 |
| 22 | + The web root is the SD Card root folder |
| 23 | + File extensions with more than 3 charecters are not supported by the SD Library |
| 24 | + File Names longer than 8 charecters will be truncated by the SD library, so keep filenames shorter |
| 25 | + index.htm is the default index (works on subfolders as well) |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <ESP8266WiFi.h> |
| 29 | +#include <WiFiClient.h> |
| 30 | +#include <ESP8266WebServer.h> |
| 31 | +#include <ESP8266mDNS.h> |
| 32 | +#include <SPI.h> |
| 33 | +#include <SD.h> |
| 34 | + |
| 35 | +//do not go larger than 1460 bytes as that is the maximum that could fit in a packet |
| 36 | +#define WWW_BUF_SIZE 1460 |
| 37 | + |
| 38 | +const char* ssid = "**********"; |
| 39 | +const char* password = "**********"; |
| 40 | +const char* hostname = "esp8266sd"; |
| 41 | + |
| 42 | +MDNSResponder mdns; |
| 43 | +ESP8266WebServer server(80); |
| 44 | + |
| 45 | +static bool hasSD = false; |
| 46 | + |
| 47 | +bool loadFromSdCard(String path){ |
| 48 | + String dataType = "text/plain"; |
| 49 | + //handle default index |
| 50 | + if(path.endsWith("/")) path += "index.htm"; |
| 51 | + |
| 52 | + //set proper Content-Type for the most common extensions |
| 53 | + if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf(".")); |
| 54 | + else if(path.endsWith(".htm")) dataType = "text/html"; |
| 55 | + else if(path.endsWith(".css")) dataType = "text/css"; |
| 56 | + else if(path.endsWith(".js")) dataType = "application/javascript"; |
| 57 | + else if(path.endsWith(".png")) dataType = "image/png"; |
| 58 | + else if(path.endsWith(".gif")) dataType = "image/gif"; |
| 59 | + else if(path.endsWith(".jpg")) dataType = "image/jpeg"; |
| 60 | + else if(path.endsWith(".ico")) dataType = "image/x-icon"; |
| 61 | + else if(path.endsWith(".xml")) dataType = "text/xml"; |
| 62 | + else if(path.endsWith(".pdf")) dataType = "application/pdf"; |
| 63 | + else if(path.endsWith(".zip")) dataType = "application/zip"; |
| 64 | + |
| 65 | + //Try to open the file |
| 66 | + File dataFile = SD.open(path.c_str()); |
| 67 | + |
| 68 | + //if it's a folder, try to open the default index |
| 69 | + if(dataFile && dataFile.isDirectory()){ |
| 70 | + path += "/index.htm"; |
| 71 | + dataType = "text/html"; |
| 72 | + dataFile = SD.open(path.c_str()); |
| 73 | + } |
| 74 | + |
| 75 | + //and finally if the file exists, stream the content to the client |
| 76 | + if (dataFile) { |
| 77 | + WiFiClient client = server.client(); |
| 78 | + //send the file headers |
| 79 | + String head = "HTTP/1.1 200 OK\r\nContent-Type: "; |
| 80 | + head += dataType; |
| 81 | + head += "\r\nContent-Length: "; |
| 82 | + head += dataFile.size(); |
| 83 | + head += "\r\n\r\n"; |
| 84 | + client.print(head); |
| 85 | + |
| 86 | + //partition the data packets to fit in a TCP packet (1460 bytes MAX) |
| 87 | + uint8_t obuf[WWW_BUF_SIZE]; |
| 88 | + while (dataFile.available() > WWW_BUF_SIZE){ |
| 89 | + dataFile.read(obuf, WWW_BUF_SIZE); |
| 90 | + client.write(obuf, WWW_BUF_SIZE); |
| 91 | + } |
| 92 | + //stream the last data left (size is at most WWW_BUF_SIZE bytes) |
| 93 | + uint16_t leftLen = dataFile.available(); |
| 94 | + dataFile.read(obuf, leftLen); |
| 95 | + client.write(obuf, leftLen); |
| 96 | + |
| 97 | + dataFile.close(); |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | +} |
| 102 | + |
| 103 | +void tryLoadFromSdCard(){ |
| 104 | + String message = "FileNotFound\n\n"; |
| 105 | + if(hasSD){ |
| 106 | + //try to load the URL from SD Card |
| 107 | + if(loadFromSdCard(server.uri())) return; |
| 108 | + } else { |
| 109 | + message = "SDCARD Not Detected\n\n"; |
| 110 | + } |
| 111 | + server.send(404, "text/plain", message); |
| 112 | +} |
| 113 | + |
| 114 | +void setup(void){ |
| 115 | + uint8_t i = 0; |
| 116 | + Serial.begin(115200); |
| 117 | + |
| 118 | + //setup WiFi |
| 119 | + WiFi.begin(ssid, password); |
| 120 | + Serial.print("\nConnecting to "); |
| 121 | + Serial.println(ssid); |
| 122 | + |
| 123 | + //wait for WiFi to connect |
| 124 | + while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); |
| 125 | + |
| 126 | + //check if we have connected? |
| 127 | + if(i == 20){ |
| 128 | + Serial.print("Could not connect to"); |
| 129 | + Serial.println(ssid); |
| 130 | + //stop execution and wait forever |
| 131 | + while(1) delay(500); |
| 132 | + } |
| 133 | + Serial.print("Connected! IP address: "); |
| 134 | + Serial.println(WiFi.localIP()); |
| 135 | + |
| 136 | + //start mDNS Server |
| 137 | + if (mdns.begin(hostname, WiFi.localIP())) { |
| 138 | + Serial.println("MDNS responder started"); |
| 139 | + Serial.print("You can now connect to http://"); |
| 140 | + Serial.print(hostname); |
| 141 | + Serial.println(".local"); |
| 142 | + } |
| 143 | + |
| 144 | + //Attach handler |
| 145 | + server.onNotFound(tryLoadFromSdCard); |
| 146 | + |
| 147 | + //start server |
| 148 | + server.begin(); |
| 149 | + Serial.println("HTTP server started"); |
| 150 | + |
| 151 | + //init SD Card |
| 152 | + if (SD.begin(SS)){ |
| 153 | + Serial.println("SD Card initialized."); |
| 154 | + hasSD = true; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +void loop(void){ |
| 159 | + mdns.update(); |
| 160 | + server.handleClient(); |
| 161 | +} |
0 commit comments