If your data is more sensitive and you don't want to share it across the Wi-Fi network or you don't have Wi-Fi connectivity, a solution is to store your data on an SD card.
Let's see how data can be stored on an SD card.
Necessary hardware that will be used:
- Wemos D1 mini:
- microSD card shield:
- microSD card:
Since the microSD card is a shield for the Wemos D1 mini, it is easy to stack them; you just need to solder the pins that are coming into the package:
Let's determine the size of the SD card with the following sketch.
Include the SPI.h and the SD library:
#include <SPI.h> #include <SD.h>
Set up variables using the SD utility library functions:
Sd2Card card; SdVolume volume; SdFile root; const int chipSelect = D8;
In the setup function, we will determine if the card is inserted or not and the card details will be read over SPI:
void setup() { Serial.begin(115200); Serial.print(" Initializing SD card...");
Use the initialization code from the utility libraries:
if (!card.init(SPI_HALF_SPEED, chipSelect)) { Serial.println("initialization failed. Things to check:"); Serial.println("* is a card inserted?"); Serial.println("* is your wiring correct?"); Serial.println("* did you change the chipSelect pin to match your shield or module?"); return; } else { Serial.println("Wiring is correct and a card is present."); } // print the type of card Serial.print(" Card type: "); switch (card.type()) { case SD_CARD_TYPE_SD1: Serial.println("SD1"); break; case SD_CARD_TYPE_SD2: Serial.println("SD2"); break; case SD_CARD_TYPE_SDHC: Serial.println("SDHC"); break; default: Serial.println("Unknown"); } // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 if (!volume.init(card)) { Serial.println("Could not find FAT16/FAT32 partition. Make sure you've formatted the card"); return; } // print the type and size of the first FAT-type volume uint32_t volumesize; Serial.print(" Volume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); volumesize = volume.blocksPerCluster(); // clusters are collections of blocks volumesize *= volume.clusterCount(); // we'll have a lot of clusters volumesize *= 512; // SD card blocks are always 512 bytes Serial.print("Volume size (bytes): "); Serial.println(volumesize); Serial.print("Volume size (Kbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.print("Volume size (Mbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.println(" Files found on the card (name, date and size in bytes): "); root.openRoot(volume); // list all files in the card with date and size root.ls(LS_R | LS_DATE | LS_SIZE); }
In the loop function there is nothing to do, since determining the card type and its properties has been done in the setup function:
void loop() { // nothing happens after setup }
The output of the serial console will show the card type, card size in bytes, kilobytes and megabytes, and if there are some files on the card, the name and the size of them:
You can also use a bigger SDHC card or a FAT32 card: