esp8266 / Arduino

ESP8266 core for Arduino
GNU Lesser General Public License v2.1
16.07k stars 13.33k forks source link

Compile Error if i add #include <ESP8266WiFi.h> to readwrite example scetch #7314

Closed Primus007 closed 4 years ago

Primus007 commented 4 years ago

----------------------------- Delete below -----------------------------

If your issue is a general question, starts similar to "How do I..", is related to 3rd party libs, or is related to hardware, please discuss at a community forum like esp8266.com.

INSTRUCTIONS

Basic Infos

Platform

Settings in IDE

Problem Description

I open the readwrite example and upload it. All works fine. but if i add #include then i get this error: ReadWrite:50:3: error: 'myFile' was not declared in this scope myFile = SD.open("test.txt", FILE_WRITE); exit status 1 reference to 'File' is ambiguous

i have nothing other changed except the one include line.

How can i solve this?

MCVE Sketch


#include <ESP8266WiFi.h> 
#include <SPI.h>
//#include <SD.h>
#include "SdFat.h"

using namespace sdfat;

SdFat SD;

#define SD_CS_PIN SS
File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Debug Messages

Debug messages go here
earlephilhower commented 4 years ago

If you want to use FAT, use the existing filesystem SDFS. If you include SdFat.h you're including the raw code from the library which is not supported as it redefines a bunch of stuff we use. Just use #include <SDFS.h> (or #include <SD.h> if you have some need for the older Arduino SD API).