Go Down

Topic: SD card initialization failed using a Ethernet shield (Read 282 times) previous topic - next topic

DolphinPig28

I tried to make a web server for my website.
I use a Arduino Uno, a Ethernet Shield, and a Samsung EVO 64 GB micro SD card.
The problem is that when I open it on my browser (I type in 192.168.1.20), It's empty.
There's definitely a website there.
When I check my serial monitor, it said:
Initializing SD card...
ERROR - SD card initialization failed!

Here's the code:
Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,20);
EthernetServer server(80);

File webFile;

void setup()
{
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);
   
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find kyledillon.html file!");
        return;
    }
    Serial.println("SUCCESS - Found kyledillon.html file.");
}

void loop()
{
    EthernetClient client = server.available();

    if (client) {
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    webFile = SD.open("kyledillon.html");
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    break;
                }
                if (c == '\n') {
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1);
        client.stop();
    }
}

I tried the WebServer example and it worked.
Is my SD card not compatible with Arduino?

Juraj

try to swap initialization. first SD then Ethernet.
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

DolphinPig28

try to swap initialization. first SD then Ethernet.
Sorry!!! I'm a newbie, so, I don't know how to swap initialization. Can you tell me how to do that??? Thank you!!!

pert

Here's your code for the initialization of the Ethernet library:
Code: [Select]
    Ethernet.begin(mac, ip);
    server.begin();

Here's your code for the initialization of the SD library:
Code: [Select]
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
    Serial.println("SUCCESS - SD card initialized.");

So move the SD initialization code to above the Ethernet initialization code.

DolphinPig28

Here's your code for the initialization of the Ethernet library:
Code: [Select]
    Ethernet.begin(mac, ip);
    server.begin();

Here's your code for the initialization of the SD library:
Code: [Select]
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
    Serial.println("SUCCESS - SD card initialized.");

So move the SD initialization code to above the Ethernet initialization code.
First, I moved the SD initialization to the top.
Then, I see some bugs, such as incorrect file name.
The problem is that there's an error:
Arduino: 1.8.7 (Windows Store 1.8.15.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\EthernetServer.cpp: In member function 'begin':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\EthernetServer.cpp:38:1: internal compiler error: Segmentation fault

 }

 ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.15.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

What's wrong? I can't understand what it mean.
This is my new, revised code:
Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,20);
EthernetServer server(80);

File webFile;

void setup()
{
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
   
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);
   
    Serial.println("SUCCESS - SD card initialized.");
    if (!SD.exists("kyledillon.html")) {
        Serial.println("ERROR - Can't find kyledillon.html file!");
        return;
    }
    Serial.println("SUCCESS - Found kyledillon.html file.");
}

void loop()
{
    EthernetClient client = server.available();

    if (client) {
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    webFile = SD.open("kyledillon.html");
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    break;
                }
                if (c == '\n') {
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1);
        client.stop();
    }
}

What should I do???

pert

The change you made to the code looks pretty good. The only problem I see is that you forgot to move this line:
Code: [Select]
Serial.println("SUCCESS - SD card initialized.");
That won't actually break your code but it makes the output from your program not so useful. As is, if the SD initialization code was successful, but the Ethernet initialization code caused the program to hang, then you would never get the output that the SD code worked.

The problem is that there's an error:
Code: [Select]
Arduino: 1.8.7 (Windows Store 1.8.15.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\EthernetServer.cpp: In member function 'begin':

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\libraries\Ethernet\src\EthernetServer.cpp:38:1: internal compiler error: Segmentation fault

 }

 ^

Please submit a full bug report,

with preprocessed source if appropriate.

See <http://gcc.gnu.org/bugs.html> for instructions.

lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.15.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status

compilation terminated.

c:/program files/windowsapps/arduinollc.arduinoide_1.8.15.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

This is caused by a bug in the compiler. It is not caused by anything you did wrong.

This bug is specific to the 5.4.0-atmel3.6.1-arduino2 version of avr-gcc used by Arduino AVR Boards 1.6.22 and newer. It has been reported here:
https://github.com/arduino/Arduino/issues/7949

Here's the workaround:
  • Tools > Board > Boards Manager
  • Wait for downloads to finish.
  • When you move the mouse pointer over "Arduino AVR Boards", you will see a "Select version" dropdown menu appear. Select "1.6.21".
  • Click "Install".
  • Wait for installation to finish.
  • Click "Close".


If you have File > Preferences > Check for updates on startup checked, the Arduino IDE may occasionally notify you that a new version of Arduino AVR Boards is available, you'll need to refrain from updating back to the new Arduino AVR Boards version, otherwise you'll be back to seeing the segmentation fault error again.

DolphinPig28

The change you made to the code looks pretty good. The only problem I see is that you forgot to move this line:
Code: [Select]
Serial.println("SUCCESS - SD card initialized.");
That won't actually break your code but it makes the output from your program not so useful. As is, if the SD initialization code was successful, but the Ethernet initialization code caused the program to hang, then you would never get the output that the SD code worked.
This is caused by a bug in the compiler. It is not caused by anything you did wrong.

This bug is specific to the 5.4.0-atmel3.6.1-arduino2 version of avr-gcc used by Arduino AVR Boards 1.6.22 and newer. It has been reported here:
https://github.com/arduino/Arduino/issues/7949

Here's the workaround:
  • Tools > Board > Boards Manager
  • Wait for downloads to finish.
  • When you move the mouse pointer over "Arduino AVR Boards", you will see a "Select version" dropdown menu appear. Select "1.6.21".
  • Click "Install".
  • Wait for installation to finish.
  • Click "Close".


If you have File > Preferences > Check for updates on startup checked, the Arduino IDE may occasionally notify you that a new version of Arduino AVR Boards is available, you'll need to refrain from updating back to the new Arduino AVR Boards version, otherwise you'll be back to seeing the segmentation fault error again.
I installed the 1.6.21 version and there's no errors when I upload it.
The problem is that in the serial monitor there's NOTHING, no "Initializing SD card...", no "ERROR - SD card initialization failed!", no "SUCCESS - SD card initialized.", no anything.
And when I opened 192.168.1.20, the website won't load.
HELP!!!!!

Juraj

Serial.begin?
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

DolphinPig28

Serial.begin?
Fixed the Serial problem (I forgot to add Serial.begin() ).
But in the Serial Monitor, a ERROR - SD card initialization failed! pop up, even through I swapped the initialization.
Here's my new code:
Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,20);
EthernetServer server(80);

File webFile;

void setup()
{
    Serial.begin(9600);
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
    Serial.println("SUCCESS - SD card initialized.");
   
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);
   
   
    if (!SD.exists("kyledillon.html")) {
        Serial.println("ERROR - Can't find kyledillon.html file!");
        return;
    }
    Serial.println("SUCCESS - Found kyledillon.html file.");
}

void loop()
{
    EthernetClient client = server.available();

    if (client) {
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    webFile = SD.open("kyledillon.html");
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    break;
                }
                if (c == '\n') {
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1);
        client.stop();
    }
}

Juraj

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

DolphinPig28

try the CardInfo example
I tried it and it said:
Initializing SD card...Wiring is correct and a card is present.


Card type:         SDHC
Could not find FAT16/FAT32 partition.
Make sure you've formatted the card

What does it mean???

Juraj

format the card in a computer to FAT file system.
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

DolphinPig28

format the card in a computer to FAT file system.
What is FAT file system??? Sorry, I'm a newbie, so I don't know what it is (Plus, it's my first time using a SD card).

Juraj

What is FAT file system??? Sorry, I'm a newbie, so I don't know what it is (Plus, it's my first time using a SD card).
a file system with File Allocation Table.
put the card in a computer and format it. it will ask for the file system. choose FAT32
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

DolphinPig28

I change the format and
Initializing SD card...
SUCCESS - SD card initialized.
ERROR - Can't find Kyle Dillon.html file!

(I realized that the file name is wrong in the code)
Here's my new code:
Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,20);
EthernetServer server(80);

File webFile;

void setup()
{
    Serial.begin(9600);
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;
    }
    Serial.println("SUCCESS - SD card initialized.");
   
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);
   
   
    if (!SD.exists("Kyle Dillon.html")) {
        Serial.println("ERROR - Can't find Kyle Dillon.html file!");
        return;
    }
    Serial.println("SUCCESS - Found Kyle Dillon.html file.");
}

void loop()
{
    EthernetClient client = server.available();

    if (client) {
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    webFile = SD.open("Kyle Dillon.html");
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    break;
                }
                if (c == '\n') {
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    currentLineIsBlank = false;
                }
            }
        }
        delay(1);
        client.stop();
    }
}

The picture attached is what I see when I opened the SD card in my computer.

Go Up