├── README.md ├── Card Wiring.jpg ├── Licence.txt └── ESP8266_D1_MicroSD_Test.ino /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-SD-Card-Reading-Writing 2 | Examples of using the SD-Card with the ESP8266 3 | -------------------------------------------------------------------------------- /Card Wiring.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/G6EJD/ESP8266-SD-Card-Reading-Writing/HEAD/Card Wiring.jpg -------------------------------------------------------------------------------- /Licence.txt: -------------------------------------------------------------------------------- 1 | This software, the ideas and concepts is Copyright (c) David Bird 2014 and beyond. 2 | 3 | All rights to this software are reserved. 4 | 5 | It is prohibited to redistribute or reproduce of any part or all of the software contents in any form other than the following: 6 | 7 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only. 8 | 9 | 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material. 10 | 11 | 3. You may not, except with my express written permission, distribute or commercially exploit the content. 12 | 13 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes. 14 | 15 | 5. You MUST include all of this copyright and permission notice ('as annotated') and this shall be included in all copies or substantial portions of the software and where the software use is visible to an end-user. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. 18 | 19 | FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | 21 | IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /ESP8266_D1_MicroSD_Test.ino: -------------------------------------------------------------------------------- 1 | /* (c) D Bird 2016 2 | * Micro SD Shield - Append data to file 3 | * 4 | * This example creates a file, writes data to it, then closes it, reads and dsiplays the data, then adds more data. Use it as the basis of a data logger. 5 | * The following data pins are used on the Wemos D1 Mini 6 | * D5, D6, D7, D8, 3V3 and G 7 | * The SD-Card Reader uses the SPI bus pins: 8 | * WEMOS SD-CARD Reader 9 | * D5 <-> SCK/CLK 10 | * D6 <-> MISO 11 | * D7 <-> MOSI 12 | * D8 <-> CS 13 | * Vcc <-> Vcc 14 | * Gnd <-> Gnd 15 | * 16 | * Note that the SD card library uses 8.3 format filenames and is case-insensitive so filenames such as IMAGE.JPG is the same as image.jpg 17 | * When you use file.write() or file.print() etc, it doesn't write to the card until you flush() or close(). 18 | * Whenever you open a file, be sure to close it to save your data. 19 | */ 20 | /* 21 | Commands aviable with the SD-Card filing system 22 | 23 | SD.begin(ChipSelectPin) 24 | SD.exists(filename) returns TRUE is found the name of the file to test for existence, which can include directories (delimited by forward-slashes, /) 25 | SD.open(filename,OPEN_READ|OPEN_WRITE) SD.open(filename) defaults to OPEN_READ, if OPEN_WRITE parameter used and if file does not exist, it is created 26 | SD.mkdir(filename) where filename is the directory name to create, with sub-directories separated by forward-slashes / Returns true if directory creation succeeded 27 | SD.rmdir(filename) Remove a directory from the SD card. The directory must be empty, returns true if it succeeds. 28 | SD.remove(filename) Remove filename as the name of the file to remove, which can include directories (delimited by forward-slashes, / Returns true if successful 29 | 30 | file.read() 31 | file.read(buf,len) Where buf is an array of characters or bytes and len the number of elements in buf 32 | file.write() 33 | file.print() 34 | file.println() 35 | 36 | file.available() Check if there are bytes available for reading from the file. Returns the number of bytes available (int) 37 | file.seek(location) and returns status if a function so TRUE if location achieved, i.e. not past EOF 38 | file.position() Get the current position within the file i.e. the location to which the next byte will be read from or written to 39 | file.close() Close the file, and ensure that any data written to it is physically saved to the SD card 40 | file.flush() Ensures that any bytes written to the file are physically saved to the SD card. This is done automatically when the file is closed. 41 | file.peek() Read a byte from the file without advancing to the next one. Successive calls to peek() will return the same value, as will the next call to read(). 42 | file.size() Returns the size of the file in bytes (unsigned long) 43 | file.isDirectory() Directories (or folders) are special kinds of files and this function reports if the current file is a directory or not. Returns true if it is 44 | file.openNextFile() Reports the next file or folder in a directory 45 | file.rewindDirectory() Brings you back to the first file in the directory, used in conjunction with openNextFile(). 46 | 47 | */ 48 | 49 | #include 50 | #include 51 | 52 | const int chipSelect = D8; // use D0 for Wemos D1 Mini 53 | File root; 54 | 55 | void setup() { 56 | // Open serial communications and wait for port to open: 57 | Serial.begin(9600); 58 | Serial.print("\r\nWaiting for SD card to initialise..."); 59 | if (!SD.begin(chipSelect)) { // CS is D8 in this example 60 | Serial.println("Initialising failed!"); 61 | return; 62 | } 63 | Serial.println("Initialisation completed"); 64 | } 65 | 66 | void loop() { 67 | // SD.open(filename,OPEN_READ|OPEN_WRITE) SD.open(filename) defaults to OPEN_READ 68 | root = SD.open("/"); 69 | root.rewindDirectory(); 70 | printDirectory(root, 0); //Display the card contents 71 | root.close(); 72 | Serial.println("\r\nOPEN FILE example completed"); 73 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 74 | //------------------------------------------------------------------------------- 75 | // SD.exists(filename) returns TRUE is found the name of the file to test for existence, which can include directories (delimited by forward-slashes, /) 76 | Serial.println("Look for a file called 'testfile.txt', if found remove it"); 77 | if (SD.exists("testdata.txt")) { 78 | Serial.println("File testfile.txt found"); 79 | if (SD.remove("testdata.txt")) Serial.println("File successfully deleted"); else Serial.println("Error - file not deleted"); 80 | } 81 | if (!SD.exists("testdata.txt")); { 82 | Serial.println("Following confirmation checks, 'testfile.txt' now deleted\r\n"); 83 | } 84 | root = SD.open("/"); 85 | root.rewindDirectory(); 86 | printDirectory(root, 0); //Display the card contents 87 | root.close(); 88 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 89 | //------------------------------------------------------------------------------- 90 | // SD.open(filename,OPEN_READ|OPEN_WRITE) SD.open(filename) defaults to OPEN_READ 91 | Serial.println("Open a new file called 'testfile.txt' and write 1 to 5 to it"); 92 | File testfile = SD.open("testdata.txt", FILE_WRITE); // FILE_WRITE opens file for writing and moves to the end of the file, returns 0 if not available 93 | if (testfile) { 94 | for (int entry = 0; entry <= 5; entry = entry + 1) { 95 | testfile.print(entry); 96 | Serial.print(entry); 97 | } 98 | } 99 | Serial.println("\r\nCompleted writing data 1 to 5\r\n"); 100 | testfile.close(); 101 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 102 | //------------------------------------------------------------------------------- 103 | Serial.println("Open a file called 'testfile.txt' and read the data from it"); 104 | testfile = SD.open("testdata.txt", FILE_READ); // FILE_WRITE opens file for writing and moves to the end of the file 105 | while (testfile.available()) { 106 | Serial.write(testfile.read()); 107 | } 108 | Serial.println("\r\nCompleted reading data from file\r\n"); 109 | // close the file: 110 | testfile.close(); 111 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 112 | //------------------------------------------------------------------------------- 113 | Serial.println("Open a file called 'testfile.txt' and append 5 downto 1 to it"); 114 | testfile = SD.open("testdata.txt", FILE_WRITE); // FILE_WRITE opens file for writing and moves to the end of the file 115 | for (int entry = 5; entry >= 0; entry = entry - 1) { 116 | testfile.print(entry); 117 | Serial.print(entry); 118 | } 119 | Serial.println("\r\nCompleted writing data 5 to 1\r\n"); 120 | testfile.close(); 121 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 122 | //------------------------------------------------------------------------------- 123 | Serial.println("Open a file called 'testfile.txt' and read it"); 124 | testfile = SD.open("testdata.txt", FILE_READ); // FILE_WRITE opens file for writing and moves to the end of the file 125 | while (testfile.available()) { 126 | Serial.write(testfile.read()); 127 | } 128 | Serial.println("\r\nCompleted reading from the file\r\n"); 129 | // close the file: 130 | testfile.close(); 131 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 132 | //012345543210 133 | //------------------------------------------------------------------------------- 134 | Serial.println("\r\nOpen a file called 'testfile.txt' and move to position 8 in the file then print the data (should be 3)"); 135 | testfile = SD.open("testdata.txt", FILE_READ); // FILE_WRITE opens file for writing and moves to the end of the file 136 | Serial.print("Data at file location (8): "); 137 | testfile.seek(8); 138 | Serial.write(testfile.read()); 139 | //------------------------------------------------------------------------------- 140 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 141 | Serial.println("\r\nOpen a file called 'testfile.txt' and move to position 6 in the file then print the data (should be 5)"); 142 | Serial.print("Data at file location (6): "); 143 | testfile.seek(6); 144 | Serial.write(testfile.read()); 145 | //------------------------------------------------------------------------------- 146 | Serial.print("\r\nFile pointer is at file location: "); 147 | Serial.println(testfile.position()); 148 | Serial.print("\r\nData at current file location (should be 4): "); 149 | Serial.println(char(testfile.peek())); 150 | //------------------------------------------------------------------------------- 151 | // close the file: 152 | testfile.close(); 153 | //------------------------------------------------------------------------------- 154 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 155 | //------------------------------------------------------------------------------- 156 | Serial.println("\r\nOpen a file called 'testfile.txt' and write some data records"); 157 | testfile = SD.open("testdata.txt", FILE_WRITE); // FILE_WRITE opens file for writing and moves to the end of the file 158 | int hours = 10; 159 | int mins = 00; 160 | String names = "Mr Another"; 161 | testfile.print("\r\n"+String(hours)); 162 | testfile.print(":"); 163 | testfile.print(String(mins,2)); 164 | testfile.println(" "+names); 165 | Serial.print(hours); 166 | Serial.print(":"); 167 | Serial.print(mins); 168 | Serial.println(names); 169 | names = "Mr And Another"; 170 | testfile.print(String(hours+1)); 171 | testfile.print(":"); 172 | testfile.print(String(mins+1)); 173 | testfile.println(" "+names+"\r\n"); 174 | Serial.print(hours); 175 | Serial.print(":"); 176 | Serial.print(mins,2); 177 | Serial.print(names); 178 | Serial.println("\r\nCompleted writing data records to the file\r\n"); 179 | testfile.close(); 180 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 181 | 182 | // Now read file contents 183 | Serial.println("\r\nOpen a file called 'testfile.txt' and move to position 8 in the file then print the data (should be 4)"); 184 | testfile = SD.open("testdata.txt", FILE_READ); // FILE_WRITE opens file for writing and moves to the end of the file 185 | while (testfile.available()) { 186 | Serial.write(testfile.read()); 187 | } 188 | Serial.println("\r\nCompleted reading records from the file\r\n"); 189 | Serial.flush(); Serial.begin(9600); while(!Serial.available()); 190 | Serial.println("\r\nStarting again"); 191 | } 192 | 193 | void printDirectory(File dir, int numTabs) { 194 | int colcnt =0; 195 | while(true) { 196 | File entry = dir.openNextFile(); 197 | if (! entry) { 198 | // no more files 199 | break; 200 | } 201 | if (numTabs > 0) { 202 | for (uint8_t i=0; i<=numTabs; i++) { 203 | Serial.print('\t'); 204 | } 205 | } 206 | Serial.print(entry.name()); 207 | if (entry.isDirectory()) { 208 | Serial.println("/"); 209 | printDirectory(entry, numTabs+1); 210 | } else 211 | { 212 | // files have sizes, directories do not 213 | Serial.print("\t"); 214 | Serial.println(entry.size(), DEC); 215 | } 216 | entry.close(); 217 | } 218 | } 219 | 220 | --------------------------------------------------------------------------------