libelium-dev wrote:
Hola bentele,
necesitamos más información:
- Módulo que estás utilizando y sobre que plataforma.
- Fotos de tu configuración Hardware.
- Código completo.
- Detallada descripción del fallo.
Un saludo.
Hola, la placa es arduino UNO R3 y el shield es el SIM5218E. Acá les adjunto el código completo, con excepción del nombre y clave del servidor (por cuestiones de seguridad). El código no es eficiente, es decir, a la hora de ejecutar el programa, descarga y carga el archivo al servidor correctamente (no en todos los casos) pero en el programa de arroja carga fallida, como que la respuesta de los comando AT no fueran los correctos. Algunas veces sube el archivo incompleto. como podría solucionar esto?. también me gustaría saber como guardo esos archivos de descargo en la SD que le coloqué al shield. Atento a su respuesta y muchas gracias de antemano. Trabajando directamente en el IDE arduino 1.6.6
Code:
const char pin[] = "";
const char apn[] = "internet.movistar.ve";
const char user_name[] = "";
const char password[] = "";
const char ftp_server[] = "xxxxxx";
const char ftp_user_name[] = "xxxxx";
const char ftp_password[] = "xxxxx";
char aux_str[150];
char file_name []="/texto1.txt";
unsigned long ftpTime, firstTime = 0;
unsigned int ftpTimeDown = 0;
unsigned int setupTime = 0;
int8_t answer;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
int x = 0;
int uploadcomplete = 0;
int uploadincomplete = 0;
int uploadTimeout = 0;
int downloadcomplete = 0;
int downloadincomplete = 0;
int downloadTimeout = 0;
int upload = 0;
int download = 0;
int intento = 1;
long previous;
int i = 1;
String KPIDownload = "";
String KPIUpload = "";
int ftp_mode = 0;
char ftp_type[] = "A";
char ftp_directorio[] = "Picture";
void setup() {
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(1000);
set_FTP_parameters();
Serial.println("\n");
delay(1000);
Serial.println("Inicio de Pruebas...\n");
}
void loop() {
Serial.flush();
Serial.print("Intento: ");
Serial.println(i);
downloadFTPserver();
delay(3000);
Serial.println(KPIDownload);
Serial.println("\n");
Serial.flush();
delay(3000);
uploadFTPserver();
delay(3000);
Serial.println(KPIUpload);
Serial.println("\n");
delay(3000);
i++;
delay(5000);
KPIUpload = "";
KPIDownload = "";
data_size = 0;
end_file = 0;
Serial.println("KPIs Mostrados........................................\n");
}
void power_on() {
uint8_t answer = 0;
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin, HIGH);
delay(4000);
digitalWrite(onModulePin, LOW);
// waits for an answer from the module
while (answer == 0) {
// Send AT every four seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout)
{
uint8_t x = 0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while ( Serial.available() > 0)
Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
//x = 0;
previous = millis();
// this loop waits for the answer
do {
if (Serial.available() != 0) {
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
Serial.println(response);
answer = 1;
}
}
// Waits for the answer with time out
}
while ((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
void set_FTP_parameters() {
Serial.println("Parametros de conexion:\n");
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );
sendATcommand("AT+CFTPPORT=21", "OK", 2000);
sendATcommand(aux_str, "OK", 1000);
sendATcommand("AT+CFTPMODE=0", "OK", 2000);
sendATcommand(aux_str, "OK", 1000);
sendATcommand("AT+CFTPTYPE=A", "OK", 2000);
sendATcommand(aux_str, "OK", 1000);
/* sendATcommand("AT+FSCD=C:/Picture", "+FSCD: C:/Picture/", 2000);
sendATcommand(aux_str, "OK", 1000);*/
snprintf(aux_str, sizeof(aux_str), "AT+CFTPSERV=\"%s\"", ftp_server);
sendATcommand(aux_str, "OK", 1000);
snprintf(aux_str, sizeof(aux_str), "AT+CFTPUN=\"%s\"", ftp_user_name);
sendATcommand(aux_str, "OK", 1000);
snprintf(aux_str, sizeof(aux_str), "AT+CFTPPW=\"%s\"", ftp_password);
sendATcommand(aux_str, "OK", 1000);
}
void downloadFTPserver(){
Serial.flush();
String uploadpr = "";
Serial.println("*****Inicio de las Descargas*****\n");
sprintf(aux_str, "AT+CFTPGETFILE=\"%s\",1", file_name);
answer = sendATcommand(aux_str, "OK", 80000);
Serial.print("\n");
if (answer == 1)
{
firstTime = millis();
Serial.println("Descargando Archivo..");
delay(500);
while(Serial.available()>0){
delay(1000);
uploadpr += (char)Serial.read();
}
//Serial.println(uploadpr);
char prueba[50];
uploadpr.toCharArray(prueba,50);
if (strstr(prueba, "+CFTPGETFILE: 0")) {
//int pos=uploadpr.indexOf("+CFTPPUTFILE: 0");
//if (pos>=0) {
//Serial.print(pos);
Serial.println("Descarga Exitosa..");
ftpTime = firstTime - millis();
downloadcomplete ++;
KPIDownloads("tiempodeDescargaExitosa", ftpTime);
}
else
{
//envio fallido
Serial.println("Descarga Fallida..");
downloadTimeout++;
ftpTime = firstTime - millis();
KPIDownloads("tiempodeDescargaFallida", ftpTime);
}
}
delay(1000);
firstTime = 0;
}
void uploadFTPserver(){
Serial.flush();
String uploadpr = "";
Serial.println("*****Inicio de las Cargas*****\n");
sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",1", file_name);
answer = sendATcommand(aux_str, "OK", 80000);
Serial.println("\n");
Serial.println("OK RECIBIDO");
if (answer == 1)
{
firstTime = millis();
Serial.println("Cargando Archivo..");
delay(5000);
while(Serial.available()>0){
delay(1000);
uploadpr += (char)Serial.read();
}
Serial.println(uploadpr);
char prueba[50];
uploadpr.toCharArray(prueba,50);
if (strstr(prueba, "+CFTPPUTFILE: 0")) {
//int pos=uploadpr.indexOf("+CFTPPUTFILE: 0");
//if (pos>=0) {
//Serial.print(pos);
Serial.println("Carga Exitosa..");
ftpTime = firstTime - millis();
uploadcomplete ++;
KPIUploads("tiempodeCargaExitosa", ftpTime);
}
else
{
//envio fallido
Serial.println("Carga Fallida..");
uploadTimeout++;
ftpTime = firstTime - millis();
KPIUploads("tiempodeCargaFallida", ftpTime);
}
}
delay(1000);
firstTime = 0;
}
void KPIUploads(String valor, int uploadTime) {
KPIUpload = "[";
KPIUpload += valor;
KPIUpload += ",";
KPIUpload += i;
KPIUpload += ",";
KPIUpload += uploadcomplete;
KPIUpload += ",";
KPIUpload += uploadincomplete;
KPIUpload += ",";
KPIUpload += uploadTimeout;
KPIUpload += ",";
KPIUpload += uploadTime;
KPIUpload += "]";
}
void KPIDownloads(String valor, int downloadTime) {
KPIDownload = "[";
KPIDownload += valor;
KPIDownload += ",";
KPIDownload += i;
KPIDownload += ",";
KPIDownload += downloadcomplete;
KPIDownload += ",";
KPIDownload += downloadincomplete;
KPIDownload += ",";
KPIDownload += downloadTimeout;
KPIDownload += ",";
KPIDownload += downloadTime;
KPIDownload += "]";
}