Go Down

Topic: How to upload an image (200KB) from SDcard to FTP server using sim800/sim900 (Read 151 times) previous topic - next topic

ctorregrosa

I have a proyect to upload an imagen stored into a SDcard to a FTP server using a GPRS modem like sim800 or sim900.

I made a code that can upload files and I did some test and my experience is as follows:

- A "text1.txt" (47 bytes) file with a single line of simple characters: It Works (same size)  :smiley-cool:
- A "text2.txt" (2061 bytes) file with a many lines of simple characters: It works (same size)  :smiley-cool:
- A "text3.text" (57 bytes) file with a single line of symbols characters: It works (same size)  :smiley-cool:
- A "word.doc" (12649 bytes) file with a single line of simple characters: Corrupt file into FTP server (same size)  :smiley-cry:
- A "flower.jpg" (4643 bytes) file with a single line of simple characters: Corrupt file into FTP server (same size)  :smiley-cry:

The code that I use is as follows:

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

char* file_name1 = "text1.txt";
char char_buffer;
String string_buffer = "";
int buffer_space = 1000;
File File1;

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

  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...");
  pinMode(7,OUTPUT); //Reset pin of a sim800L
  digitalWrite(7, HIGH);
 
  Serial.println("Starting...");
  gprs_modem_function ();
  Serial.println("The end...");
}


void loop() {
  // put your main code here, to run repeatedly:
}

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\",\"imovil.entelpcs.cl\"", "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=\"ftp.xxx.com\"", "OK", "ERROR", 1000);
                  if (reply == 0){
                    reply = sendATcommand("AT+FTPPORT=21", "OK", "ERROR", 1000);
                    if (reply == 0){
                      reply = sendATcommand("AT+FTPUN=\"monkey\"", "OK", "ERROR", 1000);
                      if (reply == 0){
                        reply = sendATcommand("AT+FTPPW=\"banana\"", "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));
                              if (reply == 0){
                                if (File1) {
                                  int i = 0;
                                  while (File1.available()>0) {
                                    char_buffer = File1.read();
                                    string_buffer.concat(char_buffer);
                                    i++;
                                    if (i == buffer_space) {
                                      sendATcommand("AT+FTPPUT=2," + String(buffer_space), "AT+FTPPUT=2,10", "ERROR", 1000);
                                      sendATcommand(string_buffer, "OK", "ERROR", 5000);
                                      string_buffer = "";
                                      i = 0;
                                    }
                                  }
                                  if (string_buffer != ""){
                                    sendATcommand("AT+FTPPUT=2," + String(i), "AT+FTPPUT=2,10", "ERROR", 1000);
                                    sendATcommand(string_buffer, "OK", "ERROR", 5000);
                                    sendATcommand("AT+FTPPUT=2,0", "OK", "ERROR", 1000);
                                  }
                                  File1.close();
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  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(Serial3.available()>0) Serial3.read();

  //Send the atcommand to the modem
  Serial3.println(ATcommand);
  delay(100);
  unsigned int timeprevious = millis();
  while((reply == 1) && ((millis() - timeprevious) < timeout)){
    while(Serial3.available()>0) {
      character = Serial3.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;
}
I hope someone can help me...

PaulS

Code: [Select]
char char_buffer;
A one element buffer. How useful is that?

Which Arduino are you using? Only a fool thinks that a 1000 character String and File will operate together on a Uno.

ctorregrosa


miketedeschi

I'm using Arduino mega 2560.

Currently I solved my problem  :)
What was the problem with the larger files?  thanks!
Mike

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy