SIM800L FTP upload issue

Hi there!
I'm trying to make an app that uploads data from sdcard to my ftp server using a teensy 3.6 and SIM800L GPRS modem on Serial2. The initialisation goes without errors; It seems my modem doesn't answer "OK" after AT+FTPPUT=2,48 command. Because of this, the sendATcommand throws a timeout.

AT+FTPPUT=1
OK
+FTPPUT: 1,1,xxxx

then:
AT+FTPPUT=2,48
...... and I don't get any answer from SIM800... so here might be my problem.

Here's my code:

#include <SD.h>
#define SD_FAT_TYPE 3
const int chipSelect = BUILTIN_SDCARD;

bool debug = 1;
bool roaming = false;
char* file_name = "testA.txt";
File dataFile;
File dataFileCopy;
String apn_name = "live.vodafone.com";
String apn_username = "live"; 
String apn_password = ""; 
String ftp = "myftpserver.my";
String ftp_user = "user";
String ftp_password = "mypassword";

void setup() {
  byte answer = 0;
  if (debug) {Serial.begin(9600);}
  Serial2.begin(9600);
  Serial.println("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("SD initialization failed!");
  }


  //GPRS part
  answer = powerUp_Sim900();
  if (answer == 0) { Serial.println("powerUp_Sim900 failure");}
  else if (answer == 1) {delay(20000); answer = initSim900();} //Delay to let startuo the SIM900 Module
  if (debug) {Serial.println("--- Answer initSim900: " + String(answer));}
  if (answer == 0) {/*Send SMS that Sim900 init failed*/}
  else if (answer == 1) {answer = initSim900FTP();}
  if (debug) {Serial.println("--- Answer initSim900FTP: " + String(answer));}
  if (answer == 0) {/*Send SMS that Sim900FTP init failed*/}
  else if (answer == 1) {answer = Sim900FTPSend();}
  if (debug) {Serial.println("--- Answer Sim900FTPSend: " + String(answer));}
  if (answer == 0) {/*Send SMS that Sim900FTPSend failed*/}
}

void loop() {
  
}

byte initSim900() {
  byte answer = 0;
  int i = 0;
  while (i < 10 && answer == 0){
    answer = sendATcommand("AT+CREG?","+CREG: 0,1", 1000);
    i++;
    delay(1000);
  }
  if (roaming and answer == 0) {
    while (i < 20 && answer == 0){
      answer = sendATcommand("AT+CREG?","+CREG: 0,5", 1000);
      i++;
      delay(1000);
    }
  }
  if (answer == 1){answer = sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"","OK", 1000);}
  String com = "AT+SAPBR=3,1,\"APN\",\"" + apn_name + "\"";
  if (answer == 1){answer = sendATcommand(string2char(com), "OK", 1000);}
  if (answer == 1){
    answer = 0;
    i = 0;
    while (i < 3 && answer == 0){
      answer = sendATcommand("AT+SAPBR=1,1", "OK", 10000);
      if (answer == 0){sendATcommand("AT+SAPBR=0,1", "OK", 10000);}
      i++;
    }
  }
  if (answer == 1){answer = sendATcommand("AT+SAPBR=2,1", "OK", 1000);}
  return answer;
}

byte initSim900FTP() {
  byte answer = 0;
  answer = sendATcommand("AT+FTPCID=1", "OK", 1000);
  String com = "AT+FTPSERV=\"" + ftp + "\"";
  if (answer == 1){answer = sendATcommand(string2char(com), "OK", 1000);}
  if (answer == 1){answer = sendATcommand("AT+FTPPORT=21", "OK", 1000);}
  com = "AT+FTPUN=\"" + ftp_user + "\"";
  if (answer == 1){answer = sendATcommand(string2char(com), "OK", 1000);}
  com = "AT+FTPPW=\"" + ftp_password + "\"";
  if (answer == 1){answer = sendATcommand(string2char(com), "OK", 1000);}
  com = "AT+FTPPUTNAME=\"" + String(file_name) + "\"";
  if (answer == 1){answer = sendATcommand(string2char(com), "OK", 1000);}
  if (answer == 1){answer = sendATcommand("AT+FTPPUTPATH=\"/G/\"", "OK", 1000);}
  return answer;
}

byte Sim900FTPSend (){
  byte answer = 0;
  Serial.println("Try AT+FTPPUT:");
  if (answer = sendATcommand("AT+FTPPUT=1", "+FTPPUT:1,1,", 60000) == 1) {
    int data_size = 0;
    Serial.println("--- AT+FTPPUT=1 OK");
    while(Serial2.available()==0);
    char aux = Serial2.read();
    do{
      data_size *= 10;
      data_size += (aux-0x30);
      while(Serial2.available()==0);
      aux = Serial2.read();       
    } while(aux != 0x0D);
    data_size=48;
    //Serial.print("--- data_size"); Serial.println(data_size);
    dataFile = SD.open(file_name);
    Serial.println("SD.open file");
    String XcomA = "";
    String XcomB = "";
    XcomA.concat("AT+FTPPUT=2,");
    XcomA.concat(data_size);
    //XcomA.concat("""");
    XcomA.concat("\r");
       
    XcomB.concat("+FTPPUT:2,");
    XcomB.concat(data_size);
    XcomB.concat("\"");
    //XcomB.concat("""");
       
    char XxcomA[XcomA.length()];
    char XxcomB[XcomB.length()];
       
    XcomA.toCharArray(XxcomA,XcomA.length());
    XcomB.toCharArray(XxcomB,XcomB.length());
       
    if (dataFile) {
      int archivosize = dataFile.size();
      Serial.print("dataFile OK, size:"); Serial.println(archivosize);
      while(dataFile.available() and answer == 1){
        while(archivosize >= data_size){
          if (answer = sendATcommand(XxcomA,XxcomB,60000) == 1) {
            Serial.println("Write file pieces");
            for(int d = 0; d < data_size; d++){
              Serial2.write(dataFile.read());
              Serial.write(dataFile.read());
              archivosize -= 1;
            }
          }
          else {answer = 0; Serial.println("--ERR sendATcommand(XxcomA,XxcomB,30000)");}
        }
        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()];
        char CcomB[ScomB.length()];
               
        ScomA.toCharArray(CcomA,ScomA.length());
        ScomB.toCharArray(CcomB,ScomB.length());
                       
        if (sendATcommand(CcomA,CcomB,30000) == 1) {
          for(int t = 0; t < archivosize; t++){
            Serial2.write(dataFile.read());
          }
        }
      }
      // close the file:
      dataFile.close();
    }
    delay(500);
    if (sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", 30000)==1){
      if (debug) {Serial.println("File " + String(file_name) + " uploaded..." );}
    }          
  }
  else {answer = 0; Serial.println("--- AT+FTPPUT=1 ERROR");}
  return answer;
}

unsigned char sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {
  unsigned char x = 0;
  unsigned char answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);
  delay(100);
  while( Serial2.available() > 0) Serial2.read();
  Serial.println(ATcommand);
  Serial2.println(ATcommand);
  x = 0;
  previous = millis();
  do{
    if(Serial2.available() != 0){   
      response[x] = Serial2.read();
      x++;
      if (strstr(response, expected_answer1) != NULL) {
        answer = 1;
      }
      if (strstr(response, "OK") != NULL) {
        answer = 1;
      }
    }
  } while((answer == 0) && ((millis() - previous) < timeout));
  if (debug) {if((millis() - previous) < timeout) Serial.println(response); else Serial.println("sendATcommand - timeout");}
  return answer;
}

/*
unsigned char sendATcommandWData(char* ATcommand, char* expected_answer1, char* buffer_data unsigned int timeout) {
  unsigned char x = 0;
  unsigned char answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);
  delay(100);
  while( Serial2.available() > 0) Serial2.read();
  Serial.println(ATcommand);
  Serial2.println(ATcommand);
  x = 0;
  previous = millis();
  do{
    if(Serial2.available() != 0){   
      response[x] = Serial2.read();
      x++;
      if (strstr(response, expected_answer1) != NULL) {
        answer = 1;
      }
      if (strstr(response, "OK") != NULL) {
        answer = 1;
      }
    }
  } while((answer == 0) && ((millis() - previous) < timeout));
  if (debug) {if((millis() - previous) < timeout) Serial.println(response); else Serial.println("sendATcommand - timeout");}
  return answer;
}*/

byte powerUp_Sim900() {
  byte answer = 0;
  int i = 0;
  powerSim900();
  while (i < 2 && answer == 0){
    answer = sendATcommand("AT+CREG?","+CREG:", 1000);
    i++;
    delay(500);
  }
  if (answer == 0) {powerSim900();}
  i = 0;
  while (i < 2 && answer == 0){
    answer = sendATcommand("AT+CREG?","+CREG:", 1000);
    i++;
    delay(500);
  }
  return answer; //send 1 if power up
}

byte powerDown_Sim900() {
  byte answer = 0;
  int i = 0;
  powerSim900();
  while (i < 2 && answer == 0){
    answer = sendATcommand("AT+CREG?","+CREG:", 1000);
    i++;
    delay(500);
  }
  if (answer == 1) {powerSim900();}
  i = 0;
  while (i < 2 && answer == 1){
    answer = sendATcommand("AT+CREG?","+CREG:", 1000);
    i++;
    delay(500);
  }
  return answer; //send 0 if power up
}

void powerSim900() {
  pinMode(Sim900PowerPin, OUTPUT);
  digitalWrite(Sim900PowerPin,LOW);
  delay(1000);
  digitalWrite(Sim900PowerPin,HIGH);
  delay(2000);
  digitalWrite(Sim900PowerPin,LOW);
  delay(5000);
}

char* string2char(String command){
  if(command.length() != 0){
    char *p = const_cast<char*>(command.c_str());
    return p;
  }
}

byte checkFile() {
  byte answer = 0;
  if(SD.exists(file_name)) {answer = 1;}
  return answer;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.