Pages: [1]   Go Down
Author Topic: Arduino Due with Ethernet shield and sdfat problem (please help)  (Read 1736 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi forum,
I have the Arduino Due and the Ethernet Shield (W5100). My testing project is transmit a text file from LabVIEW throught Ethernet to the Due, then storage to the SD card. I tried with the SD standard library and they can work well, but the transmission and writting speed too slow (about 1kB/s). Then i tried the sdfat library (SdFatBeta20130621) hope they can work faster. I test individual Ethernet and write data to SD card both work well, but when i paired together, just some first characters from text file can write to SD file, all characters later are ÿÿÿÿÿÿÿÿÿÿÿÿÿ...
This is my code:

Code:
#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>
#define USE_ARDUINO_SPI_LIBRARY 1

SdFat sd;
SdFile myFile;

const int chipSelect = 4;

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

long int i=0;
char j=0;
char c;


EthernetClient client;

void setup()
{
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  //Serial Init------------------------------------------------------
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(400); 
 
 //SD Card Init-----------------------------------------------------
  Serial.print("\nInitializing SD card...");
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  if (!myFile.open("test.txt", O_RDWR | O_CREAT)) {
    sd.errorHalt("opening test.txt for write failed");}
 
//Ethernet--------------------------------------------------------
  digitalWrite(10, LOW);
  Ethernet.begin(mac, ip);
  delay(1000);
  Serial.println("connecting...");
  connect_again:
  if (client.connect(server, 8001)){
    Serial.println("connected");}
  else{
    Serial.println("connection failed");
    delay(2000);
    goto connect_again;}
}

void loop()
{
  if (client.available())
  {
    c = client.read();   
//    Serial.print(c);
    myFile.print(c);
    i++;
    if(i==1000)
     {
       myFile.close();
       Serial.println("Finished");
     }
  }
  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
}

I am a newbie in arduino, any help please.
Thank you so much,
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50912
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where do you prevent corruption by closing the file?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The text file i want to write to SD card is: " aaaa...a " (1000 characters "a"), but result is " aÿÿÿÿ...ÿ ", just the first charecter is true smiley-cry
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50912
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  connect_again:
  if (client.connect(server, 8001)){
    Serial.println("connected");}
  else{
    Serial.println("connection failed");
    delay(2000);
    goto connect_again;}
Forget everything you know about basic. Lose the label and the goto statement. Learn to use a while loop and the break statement.

You are not reading all the data from the client. You need a while loop that executes the body as long as client.connected() is true.

In the body of that while loop, you need to read any available data, and write to the SD card, if the count is less than 1000. Do NOT stop reading from the client if the response contains more than 1000 characters.

Code:
while(client.connected())
{
   while(client.available() > 0)
   {
      // Read and possibly store data
   }
}

The way your code is structured, you tell the client to go away as soon as you have read all data that has arrived, even though the client may still be sending data.

You can read data far faster than the client can send it, in the same way you can read this reply in far less time than it took me to hunt and peck it out.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am really thanks with your help, Mr.PaulS.
I rewrited the code:
Code:
void loop()
{
while(client.connected())
{
   while(client.available() > 0)
   {
      c = client.read();
      myFile.print(c);
      i++;
      if(i==1000)
       {
       myFile.close();
       Serial.println("Finished");
       }
   }
}
}

The result is " aaa...aÿÿÿ...ÿ " , with 512 first characters is true "a" , and other characters later still wrong "ÿ..."

When i change the code:
Code:
void loop()
{
while(client.connected())
{
   while(client.available() > 0)
   {
      c = client.read();
      if( c>0 && c<255 )
      {
      myFile.print(c);
      i++;
      if(i==1000)
       {
       myFile.close();
       Serial.println("Finished");
       }
      }
   }
}
}

Result is it never print the "Finished"
I guess that ethernet has been disconnected when transmitting data !?
I will try more...  
« Last Edit: September 21, 2013, 10:37:13 pm by anna_x » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would try to determine if it is the ethernet or the SD card. It sounds like it may be the SD. Add the "Serial.print(c);" call again to see what you are getting from the w5100.
Code:
void loop()
{
  while(client.connected())
  {
     while(client.available() > 0)
     {
        c = client.read();
        // add this
        Serial.print(c);

        if( c>0 && c<255 )
        {
          myFile.print(c);
          i++;
          if(i==1000)
          {
            myFile.close();
            Serial.println("Finished");
          }
       }
     }
  }
}
This code will cause the sketch to lock up if the connection breaks or the server stalls. It will never exit the "while(client.connected())" loop. It needs a timeout to prevent that. This code has the timeout feature:
http://playground.arduino.cc/Code/WebClient

edit: And it does not close the connection. There should be a "client.stop()" call after the "while(client.connected())" loop exits.
« Last Edit: September 22, 2013, 03:48:12 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much sir,
I try with your suggest code and it print back to serial monitor " aaa...aÿÿÿ...ÿ " , with 526 first characters "a", later "ÿ..."
Seem no problem with SD card, so i will try to check the timeout from your link. Thank you,
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try the code on the link I posted without the SD code, just the w5100 code and no SD card in the slot. See if it does the same thing. If it does, let me know. Maybe I can help you debug it.

There was an ethernet library problem in IDE versions prior to V1.0.1. It would not exit the "while(client.available())" loop.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry about my feedback late, my health not good yesterday smiley-roll-blue
I checked the code just only Ethernet code, it work very well, all characters read and print true.
My IDE version is 1.5.4 r2 (the latest version for Due).
So, i really do not know what happen when use Ehernet and SD together smiley-cry
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You said the standard SD library worked, but very slow? Maybe you should go back to it. There are ways to improve performance.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you sir, i will try more... smiley-razz
Logged

Pages: [1]   Go Up
Jump to: