ESP8266 and the SD library

I have a data logger running on an ESP32 (Wemos D1 Mini 32) that logs data to an SD card and I would like to replace the ESP32 with an ESP8266 (Wemos D1 R2)

No problem I thought as the 2 Wemos boards are pin compatible, at least as far as the 8266 pins are concerned, and the board is mounted in a prototype board so it is a matter of seconds to replace one with the other, which is what I did

Changing the board type to Wemos D1 R2 and recompiling results in a pile of errors.

Rather than continuing down that route I tried something simpler.

This compiles and runs as expected on the ESP32

#include <SPI.h>
#include <SD.h>

#define SD_CS_PIN D8
File myFile;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.println();
  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS_PIN))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  myFile = SD.open("/test.txt", "a"); //append to file
  if (myFile)
  {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    myFile.close();
    Serial.println("done.");
  }
  else
  {
    Serial.println("error opening test.txt to write");
  }
  myFile = SD.open("/test.txt", "r"); //read from file
  if (myFile)
  {
    Serial.println("test.txt:");
    String inString;  //need to use Strings because of the ESP32 webserver
    while (myFile.available())
    {
      inString += myFile.readString();
    }
    myFile.close();
    Serial.print(inString);
  }
  else
  {
    Serial.println("error opening test.txt to read");
  }
}

void loop()
{
}

Changing the board and board type to Wemos D1 R2 results in a slew of errors. See attached

This led me to go looking at alternatives such as SDFat and SDFS and different versions of SD.h, but all the suggested solutions that I found caused errors of one kind or another

I have the latest version of the ESP8266 Community package (2.7.1) installed and I am using IDE 1.8.10 on Windows 10

Has anyone got an ESP8266 writing and reading an SD card interfaced using SPI and if so, which library/libraries are you using ?

ESP8266 SD errors.txt (62.4 KB)

the SDFat should be resolved to ESP8266SDFat but you have the 1.8.10 IDE with the library priority error

Thanks for the reply but I am not sure what you are suggesting might solve the problem

Compiling with 1.8.5, 1.8.10 and 1.8.12 all fail in seemingly the same way

Good news/bad news

The SdFat ReadWrite example compiles OK on both boards using 1.8.12

Unfortunately neither of them can initialise the SD card

Multiple libraries were found for "SPI.h"
 Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SPI
Multiple libraries were found for "SD.h"
 Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SD
 Not used: C:\Program
 Not used: C:\Users\Bob\Documents\Arduino\libraries\SD
Multiple libraries were found for "SDFS.h"
 Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SDFS
Multiple libraries were found for "SdFat.h"
 Used: C:\Users\Bob\Documents\Arduino\libraries\SdFat

You have a lot of choices for library files here and the IDE is just choosing for you. (I just puled a small snippet out of your listing.) There may be something in here that you don't want mixed in? Something from somehere you didn't think of? I've been bit by this. That's why I looked here first. My compiling for Teensys I have to be careful of the teensy version of libraries that on my machine are hidden.

-jim lee

Jim - thanks for looking and for your suggestion

I have had a brief look at the libraries selected, of which there are many to choose from due to the number of boards of different types, but so far I have not spotted anything obvious

jimLee:

Multiple libraries were found for "SPI.h"

Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SPI
Multiple libraries were found for "SD.h"
Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SD
Not used: C:\Program
Not used: C:\Users\Bob\Documents\Arduino\libraries\SD
Multiple libraries were found for "SDFS.h"
Used: C:\Users\Bob\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1\libraries\SDFS
Multiple libraries were found for "SdFat.h"
Used: C:\Users\Bob\Documents\Arduino\libraries\SdFat




You have a lot of choices for library files here and the IDE is just choosing for you. (I just puled a small snippet out of your listing.) There may be something in here that you don't want mixed in? Something from somehere you didn't think of? I've been bit by this. That's why I looked here first. My compiling for Teensys I have to be careful of the teensy version of libraries that on my machine are hidden.

-jim lee

it was bug which printed 'multiple found' for every library

In my case it was that I expected it to be using library x and it was using library y that I couldn't see. Made debugging really frustrating.

-jim lee

jimLee:
In my case it was that I expected it to be using library x and it was using library y that I couldn't see. Made debugging really frustrating.

-jim lee

it was a bug in IDE 1.8.10 fixed in 1.8.11 and it was the original problem in this thread too

it was a bug in IDE 1.8.10 fixed in 1.8.11 and it was the original problem in this thread too

It may have been the cause of the original problem in this thread but using 1.8.12 I am still not able to compile a sketch for the 8266 for an SD card using any SD.h library that I have found and using SdFat.h results in a sketch that compiles for both the 8266 and 32 but then cannot initialise the SD interface using the same hardware that compiles and works with SD.h for the 32

As it happens none of this matters as the project is only for my amusement and for me to investigate the use of MQTT on a local server, temperature sensor in the garage, saving to SD in the house, displaying the garage temperature on an OLED in the house and accessing the data via a webserver hosted on the 32

The complications of the above are the reason that when I had problems swapping the 32 for an 8266 I used the ReadWrite SD example as a test bed and that is what I am basing my results on

I can only repeat that I can compile the sketch from the original post for esp8266 2.7.1 on IDE 1.8.12. so the difference could be some incorrect library selection by the builder