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)
- A "text2.txt" (2061 bytes) file with a many lines of simple characters: It works (same size)
- A "text3.text" (57 bytes) file with a single line of symbols characters: It works (same size)
- A "word.doc" (12649 bytes) file with a single line of simple characters: Corrupt file into FTP server (same size)
- A "flower.jpg" (4643 bytes) file with a single line of simple characters: Corrupt file into FTP server (same size)
The code that I use is as follows:
#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...