Uploading Image to FTP not getting the right value

Please help me I am trying to upload an image to ftp. I used the codes on this link How to upload an image (200KB) from SDcard to FTP server using sim800/sim900 - Networking, Protocols, and Devices - Arduino Forum but it is stuck on a loop because it is not getting the right value.

here is my full code

byte gprs_modem_function () {
  byte reply = 1;
  int i = 0;
  while (i < 10 && reply == 1) { //Try 10 times...
    reply = sendATcommand("AT+CREG?", "+CREG: 0,1", "ERROR", 1000);
    i++;
    delay(1000);
  }
  if (reply == 0) {
    reply = sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", "ERROR", 1000);
    if (reply == 0) {
      reply = sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet\"", "OK", "ERROR", 1000);
      if (reply == 0) {
        //reply = sendATcommand("AT+SAPBR=3,1,\"USER\",\"entelpcs\"", "OK", "ERROR", 1000);
        if (reply == 0) {
          //reply = sendATcommand("AT+SAPBR=3,1,\"PWD\",\"entelpcs\"", "OK", "ERROR", 1000);
          if (reply == 0) {
            reply = 2;
            i = 0;
            while (i < 3 && reply == 2) { //Try 3 times...
              reply = sendATcommand("AT+SAPBR=1,1", "OK", "ERROR", 10000);
              if (reply == 2) {
                sendATcommand("AT+SAPBR=0,1", "OK", "ERROR", 10000);
              }
              i++;
            }
            if (reply == 0) {
              reply = sendATcommand("AT+SAPBR=2,1", "OK", "ERROR", 1000);
              if (reply == 0) {
                reply = sendATcommand("AT+FTPCID=1", "OK", "ERROR", 1000);
                if (reply == 0) {
                  reply = sendATcommand("AT+FTPSERV=\"files.000webhost.com\"", "OK", "ERROR", 1000);
                  if (reply == 0) {
                    reply = sendATcommand("AT+FTPPORT=21", "OK", "ERROR", 1000);
                    if (reply == 0) {
                      reply = sendATcommand("AT+FTPUN=\"security\"", "OK", "ERROR", 1000);
                      if (reply == 0) {
                        reply = sendATcommand("AT+FTPPW=\"123456\"", "OK", "ERROR", 1000);
                        if (reply == 0) {
                          reply = sendATcommand("AT+FTPPUTNAME=\"" + String(file_name1) + "\"", "OK", "ERROR", 1000);
                          if (reply == 0) {
                            reply = sendATcommand("AT+FTPPUTPATH=\"/\"", "OK", "ERROR", 1000);
                            if (reply == 0) {
                              unsigned int ptime = millis();
                              reply = sendATcommand("AT+FTPPUT=1", "+FTPPUT: 1,1", "+FTPPUT: 1,6", 60000);
                              Serial.println("Time: " + String(millis() - ptime));
                            }
                             
                              int data_size = 0;
                              
                              while(mySerial.available());
                              char aux = mySerial.read();
                              
                              do {
                                data_size *= 10;
                                data_size += (aux - 0x30);
                                while(mySerial.available());
                                aux = mySerial.read();
                                Serial.println(aux);
                              } while (aux != 0x0D);
                              
                              Serial.println("wewwewew");
                              File1 = SD.open(file_name1);
                              String XcomA = "";
                              String XcomB = "";
                              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()];
                              char XxcomB[XcomB.length()];

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

                              if (File1) {
                                int archivosize = File1.size();
                                while (File1.available()) {
                                  while (archivosize >= data_size) {
                                    if (sendATcommand(XxcomA, XxcomB, "OK", 3000) == 1) {
                                      for (int d = 0; d < data_size; d++) {
                                        mySerial.write(File1.read());
                                        archivosize -= 1;
                                      }
                                    }

                                  }
                                  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, "OK", 3000) == 1) {
                                    for (int t = 0; t < archivosize; t++) {
                                      mySerial.write(File1.read());
                                    }
                                  }
                                }
                                // close the file:
                                File1.close();
                              
                              delay(500);
                              if (sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", "OK", 30000) == 1) {
                                Serial.println("File " + String(file_name1) + " uploaded..." );
                              }
                              else
                                Serial.println("Error openning the FTP session");

                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return reply;
}

byte sendATcommand(String ATcommand, String answer1, String answer2, unsigned int timeout) {
  byte reply = 1;
  String content = "";
  char character;

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


  //Send the atcommand to the modem
  mySerial.println(ATcommand);
  delay(100);
  unsigned int timeprevious = millis();
  while ((reply == 1) && ((millis() - timeprevious) < timeout)) {
    while (mySerial.available() > 0) {
      character = mySerial.read();
      content.concat(character);
      Serial.print(character);
      delay(10);
    }
    //Stop reading conditions
    if (content.indexOf(answer1) != -1) {
      reply = 0;
    } else if (content.indexOf(answer2) != -1) {
      reply = 2;
    } else {
      //Nothing to do...
    }
  }
  return reply;
}

it is stuck on this loop

do {
                               data_size *= 10;
                                data_size += (aux - 0x30);
                                while(mySerial.available());
                                aux = mySerial.read();
                                Serial.println(aux);
                              } while (aux != 0x0D);

when I print aux I only get "?" values :confused:

Post complete code!

A first guess: memory problems. You have many constant strings without the F() macro and you use the String class which fragments the memory very fast and shouldn't be used on a microcontroller.

I used the codes on this link How to upload an image (200KB) from SDcard to FTP server using sim800/sim900 - Networking, Protocols, and Devices - Arduino Forum but it is stuck on a loop because it is not getting the right value.

That code runs on a Mega2560 which has 4 hardware serial interfaces. You seem to use that horrible SoftwareSerial emulation. Given that you use the same baud rate (19200) you probably won't get that reliable. The emulated serial interface usually works up 4800 baud, in some cases up to 9600 baud. If you go higher the counterpart must be extremely timing tolerant (which most cheap devices aren't).

pylon:
SoftwareSerial emulation. Given that you use the same baud rate (19200) you probably won't get that reliable. The emulated serial interface usually works up 4800 baud, in some cases up to 9600 baud. If you go higher the counterpart must be extremely timing tolerant (which most cheap devices aren't).

It was true for the old SoftwareSerial. From 2013 SoftwareSerial is NewSoftSerial and has no problem with 19200 baud. I flashed esp8266 over it at 38200 baud.

What does this do: while(mySerial.available()); in your loop?

Paul

It was true for the old SoftwareSerial. From 2013 SoftwareSerial is NewSoftSerial and has no problem with 19200 baud. I flashed esp8266 over it at 38200 baud.

That's true for NewSoftSerial also. If the counterpart has no automatic rate adaption everything above 9600 won't work. I would expect that an ESP8266 has rate adaption but not every cheap device does.

pylon:
Post complete code!

A first guess: memory problems. You have many constant strings without the F() macro and you use the String class which fragments the memory very fast and shouldn't be used on a microcontroller.

That code runs on a Mega2560 which has 4 hardware serial interfaces. You seem to use that horrible SoftwareSerial emulation. Given that you use the same baud rate (19200) you probably won't get that reliable. The emulated serial interface usually works up 4800 baud, in some cases up to 9600 baud. If you go higher the counterpart must be extremely timing tolerant (which most cheap devices aren't).

Ok sir thanks for the info I will note that. I will post the complete code later after I got home from the school.

Paul_KD7HB:
What does this do: while(mySerial.available()); in your loop?

Paul

Actually none I just forgot to remove it.

Here is the full code
I change it to Hardware Serial as you guys mention.But now Im having a wierd problem it sends data serial twice "see the image below"

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


char* file_name1 = "pudge.jpg";
//char char_buffer;
String string_buffer = "";
//int buffer_space = 1000;
File File1;

DFRobot_SIM808 sim808(&Serial2);//Connect RX,TX,PWR,


void setup() {
Serial.begin(19200);
Serial2.begin(19200);
pinMode(53, OUTPUT);


//******** Initialize sim808 module *************
while (!sim808.init())
{
 Serial.print("Sim808 init error\r\n");
 delay(1000);
}
delay(3000);

Serial.println("SIM Init success");
if (!SD.begin(53)) {
 Serial.println("Initialization failed!");
 return;
 while (true);
} else {
 Serial.println("Initialization done.");
}

File1 = SD.open(file_name1);
if (File1) {
 Serial.println("Opening the file: " + String(file_name1) + " done.");
} else {
 Serial.println("Error opening " + String(file_name1));
 while (true);
}



Serial.println("Starting...");
gprs_modem_function ();
Serial.println("The end...");


while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
}


Serial.println("Connected");

}

void loop() {

}
byte gprs_modem_function () {
byte reply = 1;
int i = 0;
while (i < 10 && reply == 1) { //Try 10 times...
 reply = sendATcommand("AT+CREG?", "+CREG: 0,1", "ERROR", 1000);
 i++;
 delay(1000);
}
if (reply == 0) {
 reply = sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", "ERROR", 1000);
 if (reply == 0) {
   reply = sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet\"", "OK", "ERROR", 1000);
   if (reply == 0) {
     //reply = sendATcommand("AT+SAPBR=3,1,\"USER\",\"entelpcs\"", "OK", "ERROR", 1000);
     if (reply == 0) {
       //reply = sendATcommand("AT+SAPBR=3,1,\"PWD\",\"entelpcs\"", "OK", "ERROR", 1000);
       if (reply == 0) {
         reply = 2;
         i = 0;
         while (i < 3 && reply == 2) { //Try 3 times...
           reply = sendATcommand("AT+SAPBR=1,1", "OK", "ERROR", 10000);
           if (reply == 2) {
             sendATcommand("AT+SAPBR=0,1", "OK", "ERROR", 10000);
           }
           i++;
         }
         if (reply == 0) {
           reply = sendATcommand("AT+SAPBR=2,1", "OK", "ERROR", 1000);
           if (reply == 0) {
             reply = sendATcommand("AT+FTPCID=1", "OK", "ERROR", 1000);
             if (reply == 0) {
               reply = sendATcommand("AT+FTPSERV=\"files.000webhost.com\"", "OK", "ERROR", 1000);
               if (reply == 0) {
                 reply = sendATcommand("AT+FTPPORT=21", "OK", "ERROR", 1000);
                 if (reply == 0) {
                   reply = sendATcommand("AT+FTPUN=\"security\"", "OK", "ERROR", 1000);
                   if (reply == 0) {
                     reply = sendATcommand("AT+FTPPW=\"123456\"", "OK", "ERROR", 1000);
                     if (reply == 0) {
                       reply = sendATcommand("AT+FTPPUTNAME=\"" + String(file_name1) + "\"", "OK", "ERROR", 1000);
                       if (reply == 0) {
                         reply = sendATcommand("AT+FTPPUTPATH=\"/\"", "OK", "ERROR", 1000);
                         if (reply == 0) {
                           unsigned int ptime = millis();
                           Serial.println("Time: " + String(millis() - ptime));
                           reply = sendATcommand("AT+FTPPUT=1", "+FTPPUT: 1,1", "OK", 60000);
                           
                         
                          
                           int data_size = 0;
                           int data_size2 = 0;
                           
                           
                           char aux = Serial2.read();
                            
                           do {
                             data_size2 *= 10;
                             data_size2 += (aux - 0x30);
                             while(Serial2.available());
                             aux = Serial2.read();
                             
                           } while (aux != 0x0D);
                           Serial.println(File1.size());
                           File1 = SD.open(file_name1);
                           String XcomA = "";
                           String XcomB = "";
                           
                           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()];
                           char XxcomB[XcomB.length()];

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

                           if (File1) {
                             int archivosize = File1.size();
                             
                             while (File1.available()) {
                               while (archivosize >= data_size) {
                                 if (sendATcommand(XxcomA, XxcomB, "OK", 3000) == 1) {
                                   for (int d = 0; d < data_size; d++) {
                                     Serial2.write(File1.read());
                                     archivosize -= 1;
                                   }
                                 }

                               }
                               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, "OK", 3000) == 1) {
                                 for (int t = 0; t < archivosize; t++) {
                                   Serial2.write(File1.read());
                                 }
                               }
                             }
                             // close the file:
                             File1.close();
                           
                           delay(500);
                           if (sendATcommand("AT+FTPPUT=2,0", "+FTPPUT:1,0", "OK", 30000) == 1) {
                             Serial.println("File " + String(file_name1) + " uploaded..." );
                           }
                           else
                             Serial.println("Error openning the FTP session");
                             }
                         }
                       }
                     }
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
 }
}
return reply;
}

byte sendATcommand(String ATcommand, String answer1, String answer2, unsigned int timeout) {
byte reply = 1;
String content = "";
char character;

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


//Send the atcommand to the modem
Serial2.println(ATcommand);
delay(100);
unsigned int timeprevious = millis();
while ((reply == 1) && ((millis() - timeprevious) < timeout)) {
 while (Serial2.available() > 0) {
   character = Serial2.read();
   content.concat(character);
   Serial.print(character);
   delay(10);
 }
 //Stop reading conditions
 if (content.indexOf(answer1) != -1) {
   reply = 0;
 } else if (content.indexOf(answer2) != -1) {
   reply = 2;
 } else {
   //Nothing to do...
 }
}
return reply;
}

But now Im having a wierd problem it sends data serial twice "see the image below"

That doesn't look like a problem. I guess your modem is configured to echo all characters sent.