Not Opening the SD file to upload to FTP

Hello all

I am farily new to arduino and programming in general, so please bare with me.

I have been developing a code to read a CO2 sensor and an Ultrasonic range finder. That code is working great.
I am using surferTim's FTP upload/download code from the arduino playground. When I load that code into the arduino board it works fine.
THe problem comes when I try to combine the two codes. Everything runs like it should until it is time to upload the files to the FTP. The program gets stuck opening the file on the sd card, so the FTP upload does not begin. After the FTP try, the CO2 sensor is producing a number that indicates it is not reading.

Any help would be appreciated

Perhaps there is a problem in your code. If you show your code someone might be able to figure out what is going wrong.

My first guess would be that you didn't close out the file you were writing before you tried to read it.

How do I put the code into inner scroll?

I know the files are closed.

CO2scaboo:
How do I put the code into inner scroll?

Click in the reply text box.
Click on the '#' button above the text box to insert the code tags.
Click between the code tags.
Paste your code.

Alternatively:
Past in your code.
Select all of your code.
Click on the '#' button above the text box to put code tags around the selected text.

I attached the code because it was saying it was too long

CO2_Range_SD_save_send_ftp_declarations.ino (14.2 KB)

So you are writing data into two different files: "scaboo.txt" and "RData001.txt".

Are you getting any of the messages from your FTP code? Like "SD open fail", "SD opened", "Command connected", "Command connection failed"?

Your FTP server is at 192.168.0.1??? That's usually the address of your LAN router.

The Scaboo.txt file is one that I put on my sd card. I'm writing cdata001 and rdata001.
I couldn't get either file that the program wires to open so I put a file on the sd card to see if it would open any file at all.

I'm getting the sd open fail message then the ftp fail message.

I have a simple ftp set on my computer using filzilla just to test. So I set my own ftp server address.

What device are you using? If it is an Uno, you are close to using up all your SRAM with the FTP code. An SD file open fail is one symptom.

I am using an UNO. If I take out the ability to download from the ftp will that free up enough space? And I forsee more code being written anyways. I want to eventually pull time from the internet and put date stamps on my files. I also will need to change file names after each upload. That seems to be 4 to 5 kB right there. I had to leave my office so the device is not with me to test.

The FTP code only includes one function or the other in the compile. It compiles either the SD-to-FTP code, or the FTP-to-SD code, depending on the FTPWRITE define. That is so it will fit easier into an Uno.

And there is a difference between program memory and SRAM. You have 32K of program memory, but only 2K SRAM.

Here is a short sketch that has a SRAM checking function. It doesn't show zero when out of SRAM, but a very large or negative number. You can add it to your sketch and call it just before the SD open fail to see how much SRAM you have before the SD file open.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

void setup() {
  Serial.begin(9600);
  // all your setup stuff, then

  Serial.print(F("SRAM = "));
  Serial.println(freeRam());
}

void loop() {
}

If i understand SRAM, it is the storage of data while powered but erases when not powered. If that is correct, is there a way to clear the SRAM before the FTP upload?

It isn't really a matter of clearing it, but freeing it. That is why the F() function is prevalent in that code. It keeps those static strings in program memory instead of copying them to SRAM to use them.

Try the freeRam() function. Call it at the end of your setup() function to check how much you have remaining there, then again just before the SD file open. It is possible this is not the problem, but until you check...

edit: Some of the SRAM is used by the "operating system", and the Ethernet and SD libraries take their toll on it too. Then when it runs that FTP function, you have a file and two sockets open, all at the same time.

I'm not going to be able to check until tomorrow, but thank you so much for the help! I'll let you guys know what happens tomorrow.

FYI: If the SRAM is less than 200 at any time, the sketch is subject to becoming unstable.

Serial.println(F("FTP FAIL"));

Is that the F() that you are referring too?

CO2scaboo:
Serial.println(F("FTP FAIL"));

Is that the F() that you are referring too?

Yes. :slight_smile:

can I use that in all my Serial.print functions then? dataFile.print functions as well? That should free up some space in the SRAM then correct?

Yes. :slight_smile:

edit: The Serial.write() function call, and probably all the other .write() functions, have a problem with the F() function. They will throw a "I don't know anything about this FlashStringHelper thing" error.

Just had a chance to check the new code. IT's working! XD

Thanx so much for your help!