This tutorial presents code for reading from, and writing to an SD (or microSD) card.
This may seem a terribly lightweight tutorial.
But it is the fruits of two very long days of work. Though there is little here, there is a great deal of information for you in the comments within the sourcecode... which you can download. (free).
Click these, to download the Arduino IDE files (.ino) files...
It is tweaked for use in an Sparkfun ESP8266 Thing Dev.
As it happens... I don't think it will have made a big difference to what's here, I was using the Hobbytronics.co.uk SDCARD-REG2. Not expensive (£7.20 at 3/19)... with features which means it plays nicely with either 5v or 3v3 systems.
If you search for that by name, don't be alarmed if you find "it" disconinuted... there was a version 1 of the module... which has beenm discontinued. At 3/19, we had the benefit of a version 2.
There are many pages about reading and writing to SD cards out there in internet land.... with the usual proportion of them containing bad or out of date information.
However, there isn't nearly as much on the web about doing it with an ESP8266... a wonderful device, but one with a few quirks to be overcome.
Although I haven't tested it I believe that it would work with other ESP8266s (I would be grateful for news that it did, if you find that to be so.)
The code should also work in non-ESP8266 Arduinos...
For that, you would remove the line saying SPI.setFrequency(1000000);... and...
... you would also probably have to change the numbers in...
#define pDI 13//no ; here #define pDO 12 #define pCLK 14
I believe the "right" pins to use for these three are "set in stone" by the microprocessor you use.
The program reads two lines from an SD card. They need to be in a file called "creds.txt" in the root of the card. The file needs a very specific format... but if you read the comments in the code, you will see that this is not set in stone... as long as you can do a little re-programming.
The program isn't as "user friendly" as it ought to be. Various problems with the creds.txt file would cause inelegant system failures. (Nothing to damage your hardware... it would just fail to do what was wanted, and maybe not give very clear error messages.)
The Arduino IDE Serial Monitor will help you see "what's what", up to a point, when the program runs.
There is a great deal more help on how to read or write from (to) SD cards in the comments in the sourcecode.
The read routine offered here is crude. Useable! But not flexible, and not tolerant of imperfections in the composition of the file being read.
Someday someone will have to improve the core part of this. (The "access the SD card" part is, touch wood, "finished".)
Significant planning for that "better world" is available in another essay I've written: sFetchLineFromFile- a general function for reading text from a file.
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.
Click here to visit editor's Sheepdog Software freeware, shareware pages.. Material on this page © TK Boyd 3/19
Click here to visit the homepage of my biggest site.
Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?
Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
....... P a g e . . . E n d s .....