Problem in transfering a text file to FTP server using the GPRS and SIM900

Hi, I am trying to transfer a text file from SD card to a FTP server using SIM900 via GPRS. I have used the code here as my reference http://www.cooking-hacks.com/index.php/documentation/tutorials/arduino-3g-gprs-gsm-gps#step13.
Though the title says, "Uploading a file from local storage or SD to FTP server" the code is only explaining how to transfer a file from local storage to FTP server and nothing is said about the SD card. I added the SD card initialization code and opened the text file in read mode. After this I don't know How I am supposed to proceed. here is my code,

#include <SD.h>

const int chipSelect = 4;
int8_t answer;
int onModulePin= 2;

char aux_str[50];

char file_name[ ]="datalog.txt";

void setup(){
  delay(5000);
    // SD function
    Serial.begin(9600);
    pinMode(chipSelect, OUTPUT);
    pinMode(10, OUTPUT);  // SD library to work
    digitalWrite(10,HIGH);
    Serial.print("Initializing SD card...");
    if (!SD.begin(chipSelect)) 
    {
    Serial.println("Card failed, or not present");
    return;
    }
    Serial.println("card initialized.");
    File dataFile = SD.open("datalog.txt", FILE_READ);
    if (dataFile)
      Serial.println(" datalog.txt is opened in read mode.");
    // GSM function
    pinMode(onModulePin, OUTPUT); 
    Serial1.begin(9600); 
    delay(3000);
    
    sendATcommand("AT", "OK", 5000);
    Serial.println("GSM modem working!");
    delay(3000);
    
    // waits for signal
    while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
            sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
    
    // sets APN, user name and password
    sendATcommand("AT+CGSOCKCONT=1,\"IP\",\"airtelgprs.com\"", "OK", 2000);
    // ther is no username and password for my GPRS connection so I commented out this line
    //sendATcommand("AT+CSOCKAUTH=1,1,\"\",\"\"", "OK", 2000);
    
    // sets the paremeters for the FTP server
    sendATcommand("AT+CFTPSERV=\"mydomain.in\"", "OK", 2000);
    sendATcommand("AT+CFTPPORT=21", "OK", 2000);
    sendATcommand("AT+CFTPMODE=1", "OK", 2000);
    sendATcommand("AT+CFTPUN=\"username\"", "OK", 2000);
    sendATcommand("AT+CFTPPW=\"password\"", "OK", 2000);
    
    // the file must be in the current directory
    sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",0", file_name);
    answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);
    
    if (answer == 1)
    {        
        Serial.println("Upload done");    
    }
    else
    {
        Serial.println("Upload fail");    
    }
   

}
void loop(){
    
    

}

void power_on(){

    uint8_t answer=0;
    
    // checks if the module is started
    answer = sendATcommand("AT", "OK", 2000);
    if (answer == 0)
    {
        // power on pulse
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);
    
        // waits for an answer from the module
        while(answer == 0){    
            // Send AT every two seconds and wait for the answer
            answer = sendATcommand("AT", "OK", 2000);    
        }
    }
    
}


unsigned char sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout)
{

    unsigned char x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( Serial1.available() > 0) Serial1.read();    // Clean the input buffer

    Serial1.println(ATcommand);    // Send the AT command 


        x = 0;
    previous = millis();

    // this loop waits for the answer
    do{

        if(Serial1.available() != 0){    
            response[x] = Serial1.read();
            x++;
            // check if the desired answer is in the response of the module
            if (strstr(response, expected_answer1) != NULL)    
            {
                answer = 1;
            }
        }
        // Waits for the asnwer with time out
    }
    while((answer == 0) && ((millis() - previous) < timeout));    

    return answer;
}

and this is the out put I am getting.

Initializing SD card...card initialized.
 datalog.txt is opened in read mode.
GSM modem working!
Upload fail

Please help me I know I am doing some thing wrong.
Thanks in advance.

After this I don't know How I am supposed to proceed.

Perhaps by actually reading from the file, and actually sending the data read.

The problem is that the SIM900 doesn't support the AT+CFTPPUTFILE and the other CFTP commands, you can see the AT+Commands that this module supports in it's datasheet:

http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/SIM900_AT_Command_Manual.pdf

What I'm doing is reading my file from the SD and writing with the command AT+FTPPUT=2,numberBytes, where numberBytes is the max length of data can be sent at a time, it depends on the network status, in my case the max lenght is 1360, so i decided to send 1000 first, then send the other bytes that are left. I'm still working in this code but hope it helps

if (sendATcommand("AT+FTPPUT=1", "+FTPPUT:1,1,", 30000) == 1){

dataFile = SD.open(filename);

if (dataFile) {
archivosize = dataFile.size();
while(dataFile.available()){

while(archivosize >= 1000){
sendATcommand("AT+FTPPUT=2,1000","+FTPPUT:2,1000",3000);
for(int i = 0; i < 1000; i++){
Serial.write(dataFile.read());
archivosize -= i;
}
}

ScomA = "";
ScomB = "";
// archivosize += 1;
ScomA.concat("AT+FTPPUT=2,");
ScomA.concat(archivosize);
ScomA.concat(""");

ScomB.concat("+FTPPUT:2,");
ScomB.concat(archivosize);
ScomB.concat(""");

char CcomA[ScomA.length()], CcomB[ScomB.length()];

ScomA.toCharArray(CcomA,ScomA.length());
ScomB.toCharArray(CcomB,ScomB.length());

sendATcommand(CcomA,CcomB,3000);
for(int i = 0; i < archivosize; i++){
Serial.write(dataFile.read());
archivosize -= i;
}
}
// close the file:
dataFile.close();
}
//Finalizo envio de datos
sendATcommand("AT+FTPPUT=2,0", "OK", 3000);
}
else{
xbee.println("Error openning the FTP session");
}

Hi,
Have a look to following post at reply #10 : sim900: AT commands for FTP | Forum for Electronics.
I use seeduino "GPRS Shield V2.00" with Sim900.
I just tested the AT command list they suggest (using "Fun with AT Command"* sketch) and it perfectly works for both upload and download .txt file !
=> 1rst step is done : I have the correct AT instrcution list to uplaod and download files with Sim900
=> 2nd step under progress : Now I "just need" to write the code for upload .jpg file from SD card to ftp. If you have any idea ...

*"Fun with AT Command" sketch : GPRS Shield V2.0 | Seeed Studio Wiki

keep a watchful eye on the step ii,
hava you done it now?

pabm122:
The problem is that the SIM900 doesn't support the AT+CFTPPUTFILE and the other CFTP commands, you can see the AT+Commands that this module supports in it's datasheet:

http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/SIM900_AT_Command_Manual.pdf

What I'm doing is reading my file from the SD and writing with the command AT+FTPPUT=2,numberBytes, where numberBytes is the max length of data can be sent at a time, it depends on the network status, in my case the max lenght is 1360, so i decided to send 1000 first, then send the other bytes that are left. I'm still working in this code but hope it helps

if (sendATcommand("AT+FTPPUT=1", "+FTPPUT:1,1,", 30000) == 1){

dataFile = SD.open(filename);

if (dataFile) {
archivosize = dataFile.size();
while(dataFile.available()){

while(archivosize >= 1000){
sendATcommand("AT+FTPPUT=2,1000","+FTPPUT:2,1000",3000);
for(int i = 0; i < 1000; i++){
Serial.write(dataFile.read());
archivosize -= i;
}
}

ScomA = "";
ScomB = "";
// archivosize += 1;
ScomA.concat("AT+FTPPUT=2,");
ScomA.concat(archivosize);
ScomA.concat(""");

ScomB.concat("+FTPPUT:2,");
ScomB.concat(archivosize);
ScomB.concat(""");

char CcomA[ScomA.length()], CcomB[ScomB.length()];

ScomA.toCharArray(CcomA,ScomA.length());
ScomB.toCharArray(CcomB,ScomB.length());

sendATcommand(CcomA,CcomB,3000);
for(int i = 0; i < archivosize; i++){
Serial.write(dataFile.read());
archivosize -= i;
}
}
// close the file:
dataFile.close();
}
//Finalizo envio de datos
sendATcommand("AT+FTPPUT=2,0", "OK", 3000);
}
else{
xbee.println("Error openning the FTP session");
}

Hi,

I want to thank you for your code it has just saved my life :slight_smile:
I make a little update for use the reply of the FTP :

Serial2 is my Sim900 change it to Serial

if (sendATcommand("AT+FTPPUT=1", "+FTPPUT:1,1,", 30000) == 1)
{
data_size = 0;
while(Serial2.available()==0);
aux = Serial2.read();
do{
data_size *= 10;
data_size += (aux-0x30);
while(Serial2.available()==0);
aux = Serial2.read();
}while(aux != 0x0D);
dataFile = SD.open("DATALOG.TXT");
String XcomA = "";
String XcomB = "";
// archivosize += 1;
XcomA.concat("AT+FTPPUT=2,");
XcomA.concat(data_size);
XcomA.concat(""");

XcomB.concat("+FTPPUT:2,");
XcomB.concat(data_size);
XcomB.concat(""");

char XxcomA[XcomA.length()], XxcomB[XcomB.length()];

XcomA.toCharArray(XxcomA,XcomA.length());
XcomB.toCharArray(XxcomB,XcomB.length());

if (dataFile) {
int archivosize = dataFile.size();
while(dataFile.available()){
//Serial.println(archivosize);

while(archivosize >= data_size){
if (sendATcommand(XxcomA,XxcomB,3000) == 1){
for(int d = 0; d < data_size; d++){
Serial2.write(dataFile.read());
archivosize -= 1;
//Serial.print("Archive size : ");
//Serial.println(archivosize);
}
}
}
// Serial.print("il reste : ");
//Serial.println(archivosize);
String ScomA = "";
String ScomB = "";
ScomA.concat("AT+FTPPUT=2,");
ScomA.concat(archivosize);
ScomA.concat(""");

ScomB.concat("+FTPPUT:2,");
ScomB.concat(archivosize);
ScomB.concat(""");

char CcomA[ScomA.length()], CcomB[ScomB.length()];

ScomA.toCharArray(CcomA,ScomA.length());
ScomB.toCharArray(CcomB,ScomB.length());

if (sendATcommand(CcomA,CcomB,3000) == 1){
for(int t = 0; t < archivosize; t++){
Serial2.write(dataFile.read());
//Serial.print("Archive size : ");
//Serial.println(t);
}
}
}
// close the file:
dataFile.close();
}
delay(500);
if (sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", 30000)==1){
GPRS_IDLE_TIME = seconds + 10;
GsmDo = 3;
GsmStatut = 1;
}

}else{
//Erreur
}

Hi, I'm trying to do the same thing with regards to uploading and download file to an FTP server. What hosting site do you use? Thanks!