My Cart

How to Make a Daily Newspaper Online Using 3G+GPS Mobile Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to record an audio file, take a photo and store them in a personal FTP logbook.

Ingredients:

    - 1 x 3G+GPS Mobile Kit:
    • 1x Platform
    • 1x 3G + GPS shield
    • 1x GPRS antenna
    • 1x GPS antenna
    • 1x Temperature sensor (MCP9700A)
    • 1x Camera
    • 1x Breadboard
    • 1x External power supply
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the 3G + GPS Shield, consult the main tutorial.

Step 1: Connection

Connect the GPRS antenna to the shield and then, connect the shield to Arduino or to Raspberry Pi connection bridge. You will also need to connect the camera and the microphone in this example

Step 2: The Code

Arduino:

Code:
/*  
 *  3G + GPS shield
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Victor Boria
 */

//Change here your data
const char pin[]="*******";
const char apn[]="*******";
const char user_name[]="*******";
const char password[]="*******";
const char ftp_server[]="*******";
const char ftp_user_name[]="*******";
const char ftp_password[]="*******";


int8_t answer, counter;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
char aux_str[150];
char picture_name[20]; //Photo file
char clip_name[20];    //Audio file
char response[150];
int x = 0;
long previous;


void setup(){

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);   

  Serial.println("Starting...");
  power_on();
  delay(3000);

  //We recommend deleting the files in the memory, if it's full the code doesn't work
  //Uncomment these two lines for deleting
  //sendATcommand("AT+FSDEL=*.*", "OK", 2000); 
  //delay(2000);

  record_audio(10); //Write the number of seconds for your audio
  take_photo();     //Take a photo
  send_ftp();       //Send both audio and photo to your FTP
}

void loop(){
}


/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/

void record_audio(uint8_t seconds){

  Serial.println("Recording sound in:");
  delay(1000);
  Serial.println("3");
  delay(1000);
  Serial.println("2");
  delay(1000);
  Serial.println("1");
  delay(1000);

  answer = sendATcommand2("AT+CQCPREC=0,amr", "C:/Audio/", "ERROR", 5000);
  if (answer == 1)
  {
    counter = 0;
    while(Serial.available()==0);
    do{
      clip_name[counter] = Serial.read();
      counter++;
      while(Serial.available()==0);
    }
    while (clip_name[counter-1] != 0x0D);

    clip_name[counter-1] = '\0';

    Serial.print("Clip name: ");                
    Serial.println(clip_name);
    delay(seconds*1000);
    sendATcommand2("AT+CQCPSTOP", "OK", "ERROR", 2000);
  }

}

void take_photo() {

  // Starts the camera
  sendATcommand2("AT+CCAME", "OK", "CAMERA NO SENSOR", 1000);
  answer = sendATcommand2("AT+CCAMS", "OK", "CAMERA NO SENSOR", 3000);
  if (answer == 1)
  {
    // Sets resolution (Old camera)
    //sendATcommand2("AT+CCAMSETD=640,480", "OK", "ERROR", 2000);

    // Sets resolution (New camera)
    sendATcommand2("AT+CCAMSETD=1600,1200", "OK", "ERROR", 2000);

    Serial.println("Taking photo in:");
    delay(1000);
    Serial.println("3");
    delay(1000);
    Serial.println("2");
    delay(1000);
    Serial.println("1");
    delay(1000);
    // Takes a picture, but not saved it
    answer = sendATcommand2("AT+CCAMTP", "OK", "ERROR", 5000);
    delay(1000);
    if (answer == 1)
    {
      // Saves the picture into C:/Picture
      answer = sendATcommand2("AT+CCAMEP", "C:/Picture/", "ERROR", 2000);

      if (answer == 1)
      {
        memset(picture_name, 0x00, sizeof(picture_name));
        counter = 0;
        while (Serial.available() == 0);
        do {
          picture_name[counter] = Serial.read();
          counter++;
          while (Serial.available() == 0);
        } 
        while (picture_name[counter - 1] != 0x0D);

        picture_name[counter - 1] = '\0';

        Serial.print("Picture name: ");
        Serial.println(picture_name);

        sendATcommand2("AT+CCAME", "OK", "", 2000);

      }
      else
      {
        Serial.println("Error saving the picture");
      }
    }
    else if (answer == 2)
    {
      Serial.println("Camera invalid state");
    }
    else
    {
      Serial.println("Error taking the picture");
    }
  }
  else if (answer == 2)
  {
    Serial.println("Camera not detected");
  }
  else
  {
    Serial.println("Error starting the camera");
  }

}

void send_ftp(){
  //sets the PIN code
  snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
  sendATcommand(aux_str, "OK", 2000);

  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
    sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

  // sets APN, user name and password
  snprintf(aux_str, sizeof(aux_str), "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPSERV=\"%s\"", ftp_server);
  sendATcommand(aux_str, "OK", 2000);

  sendATcommand("AT+CFTPPORT=21", "OK", 2000);
  sendATcommand("AT+CFTPMODE=1", "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPUN=\"%s\"", ftp_user_name);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPPW=\"%s\"", ftp_password);
  sendATcommand(aux_str, "OK", 2000);


  // Search audio in "C:/Audio"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",7", clip_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    Serial.println("Audio Upload done");    
  }
  else
  {
    Serial.println("Audio Upload fail");    
  }

  delay(10000);

  // Search picture in "C:/Picture"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",1", picture_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    Serial.println("Picture Upload done");    
  }
  else
  {
    Serial.println("Picture Upload fail");    
  }
}


void power_on(){

  uint8_t answer=0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);

    // waits for an answer from the module
    while(answer == 0){    
      // Send AT every two 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 there are data in the UART input buffer, reads it and checks for the asnwer
    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)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}


int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, 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 there are data in the UART input buffer, reads it and checks for the answer
    if(Serial.available() != 0){    
      response[x] = Serial.read();
      x++;
      // check if the desired answer 1 is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
      // check if the desired answer 2 is in the response of the module
      if (strstr(response, expected_answer2) != NULL)    
      {
        answer = 2;
      }
    }
    // Waits for the answer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}



        

Raspberry Pi:

Code:
/*  
 *  3G + GPS shield
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Alejandro Gallego & Victor Boria
 */
 
//Include ArduPi library
#include "arduPi.h"

//Change here your data
const char pin[]="*******";
const char apn[]="*******";
const char user_name[]="*******";
const char password[]="*******";
const char ftp_server[]="*******";
const char ftp_user_name[]="*******";
const char ftp_password[]="*******";

void power_on();
int8_t sendATcommand(const char* ATcommand, const char* expected_answer1, unsigned int timeout);
int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, unsigned int timeout);
void record_audio(uint8_t seconds);
void take_photo();
void send_ftp();

int8_t answer, counter;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
char aux_str[150];
char picture_name[20]; //Photo file
char clip_name[20];    //Audio file
char response[150];
int x = 0;
long previous;


void setup(){

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);   

  printf("Starting...\n");
  power_on();
  delay(3000);

  //We recommend deleting the files in the memory, if it's full the code doesn't work
  //Uncomment these two lines for deleting
  //sendATcommand("AT+FSDEL=*.*", "OK", 2000); 
  //delay(2000);
  //printf("Deleting files in internal memory\n");

  record_audio(10); //Write the number of seconds for your audio
  take_photo();     //Take a photo
  send_ftp();       //Send both audio and photo to your FTP
}

void loop(){
}


/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/

void record_audio(uint8_t seconds){
	
  printf("Recording sound in:\n");
  delay(1000);
  printf("3\n");
  delay(1000);
  printf("2\n");
  delay(1000);
  printf("1\n");
  delay(1000);

  answer = sendATcommand2("AT+CQCPREC=0,amr", "C:/Audio/", "ERROR", 5000);
  if (answer == 1)
  {
    counter = 0;
    while(Serial.available()==0);
    do{
      clip_name[counter] = Serial.read();
      counter++;
      while(Serial.available()==0);
    }
    while (clip_name[counter-1] != 0x0D);

    clip_name[counter-1] = '\0';


    printf("Clip name: %s\n", clip_name);
    delay(seconds*1000);
    sendATcommand2("AT+CQCPSTOP", "OK", "ERROR", 2000);
  }

}

void take_photo() {

  // Starts the camera
  sendATcommand2("AT+CCAME", "OK", "CAMERA NO SENSOR", 1000);
  answer = sendATcommand2("AT+CCAMS", "OK", "CAMERA NO SENSOR", 3000);
  if (answer == 1)
  {
    // Sets resolution (Old camera)
    //sendATcommand2("AT+CCAMSETD=640,480", "OK", "ERROR", 2000);

    // Sets resolution (New camera)
    sendATcommand2("AT+CCAMSETD=1600,1200", "OK", "ERROR", 2000);

    printf("Taking photo in:\n");
    delay(1000);
    printf("3\n");
    delay(1000);
    printf("2\n");
    delay(1000);
    printf("1\n");
    delay(1000);
    
    // Takes a picture, but not saved it
    answer = sendATcommand2("AT+CCAMTP", "OK", "ERROR", 5000);
    delay(1000);
    if (answer == 1)
    {
      // Saves the picture into C:/Picture
      answer = sendATcommand2("AT+CCAMEP", "C:/Picture/", "ERROR", 2000);

      if (answer == 1)
      {
        memset(picture_name, 0x00, sizeof(picture_name));
        counter = 0;
        while (Serial.available() == 0);
        do {
          picture_name[counter] = Serial.read();
          counter++;
          while (Serial.available() == 0);
        } 
        while (picture_name[counter - 1] != 0x0D);

        picture_name[counter - 1] = '\0';

        printf("Picture name: %s", picture_name);
        sendATcommand2("AT+CCAME", "OK", "", 2000);

      }
      else
      {
		printf("Error saving the picture\n");
      }
    }
    else if (answer == 2)
    {
      printf("Camera invalid state\n");
    }
    else
    {
      printf("Error taking the picture\n");
    }
  }
  else if (answer == 2)
  {
    printf("Camera not detected\n");
  }
  else
  {
    printf("Error starting the camera\n");
  }

}

void send_ftp(){
  //sets the PIN code
  snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
  sendATcommand(aux_str, "OK", 2000);

  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
    sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

  // sets APN, user name and password
  snprintf(aux_str, sizeof(aux_str), "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPSERV=\"%s\"", ftp_server);
  sendATcommand(aux_str, "OK", 2000);

  sendATcommand("AT+CFTPPORT=21", "OK", 2000);
  sendATcommand("AT+CFTPMODE=1", "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPUN=\"%s\"", ftp_user_name);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPPW=\"%s\"", ftp_password);
  sendATcommand(aux_str, "OK", 2000);


  // Search audio in "C:/Audio"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",7", clip_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    printf("Audio Upload done\n");    
  }
  else
  {
    printf("Audio Upload fail\n");    
  }

  delay(10000);

  // Search picture in "C:/Picture"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",1", picture_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    printf("Picture Upload done\n");    
  }
  else
  {
    printf("Picture Upload fail\n");    
  }
}


void power_on()
{

  uint8_t answer = 0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin, HIGH);
    delay(3000);
    digitalWrite(onModulePin, LOW);

    // waits for an answer from the module
    while (answer == 0) {
      // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }

}



int8_t sendATcommand(const char* ATcommand, const char* expected_answer1, unsigned int timeout)
{

  uint16_t x = 0,  answer = 0;
  char response[1000];
  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();
      printf("%c", response[x]);
      x++;
      // check if the desired answer is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        printf("\n");
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}



int8_t sendATcommand2(const char* ATcommand, const char* expected_answer1, const char* expected_answer2, 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();
      printf("%c", response[x]);
      x++;
      // check if the desired answer 1 is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        printf("\n");
        answer = 1;
      }
      // check if the desired answer 2 is in the response of the module
      if (strstr(response, expected_answer2) != NULL)
      {
        printf("\n");
        answer = 2;
      }
    }
    // Waits for the answer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}


int main () {
  setup();
  while (1) {
    loop();
  }
  return (0);
}
        

Intel Galileo:

Code:
/*  
 *  3G + GPS shield
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis Martin
 */

//Change here your data
const char pin[]="*******";
const char apn[]="*******";
const char user_name[]="*******";
const char password[]="*******";
const char ftp_server[]="*******";
const char ftp_user_name[]="*******";
const char ftp_password[]="*******";


int8_t answer, counter;
int onModulePin = 2, aux;
int data_size = 0;
int end_file = 0;
char aux_str[150];
char picture_name[20]; //Photo file
char clip_name[20];    //Audio file
char response[150];
int x = 0;
long previous;


void setup(){

  pinMode(onModulePin, OUTPUT);
  Serial.begin(115200);   

  Serial.println("Starting...");
  power_on();
  delay(3000);

  //We recommend deleting the files in the memory, if it's full the code doesn't work
  //Uncomment these two lines for deleting
  //sendATcommand("AT+FSDEL=*.*", "OK", 2000); 
  //delay(2000);

  record_audio(10); //Write the number of seconds for your audio
  take_photo();     //Take a photo
  send_ftp();       //Send both audio and photo to your FTP
}

void loop(){
}


/************************************************************************
 ****               Definition of functions                          ****
 ************************************************************************/

void record_audio(uint8_t seconds){

  Serial.println("Recording sound in:");
  delay(1000);
  Serial.println("3");
  delay(1000);
  Serial.println("2");
  delay(1000);
  Serial.println("1");
  delay(1000);

  answer = sendATcommand2("AT+CQCPREC=0,amr", "C:/Audio/", "ERROR", 5000);
  if (answer == 1)
  {
    counter = 0;
    while(Serial.available()==0);
    do{
      clip_name[counter] = Serial.read();
      counter++;
      while(Serial.available()==0);
    }
    while (clip_name[counter-1] != 0x0D);

    clip_name[counter-1] = '\0';

    Serial.print("Clip name: ");                
    Serial.println(clip_name);
    delay(seconds*1000);
    sendATcommand2("AT+CQCPSTOP", "OK", "ERROR", 2000);
  }

}

void take_photo() {

  // Starts the camera
  sendATcommand2("AT+CCAME", "OK", "CAMERA NO SENSOR", 1000);
  answer = sendATcommand2("AT+CCAMS", "OK", "CAMERA NO SENSOR", 3000);
  if (answer == 1)
  {
    // Sets resolution (Old camera)
    //sendATcommand2("AT+CCAMSETD=640,480", "OK", "ERROR", 2000);

    // Sets resolution (New camera)
    sendATcommand2("AT+CCAMSETD=1600,1200", "OK", "ERROR", 2000);

    Serial.println("Taking photo in:");
    delay(1000);
    Serial.println("3");
    delay(1000);
    Serial.println("2");
    delay(1000);
    Serial.println("1");
    delay(1000);
    // Takes a picture, but not saved it
    answer = sendATcommand2("AT+CCAMTP", "OK", "ERROR", 5000);
    delay(1000);
    if (answer == 1)
    {
      // Saves the picture into C:/Picture
      answer = sendATcommand2("AT+CCAMEP", "C:/Picture/", "ERROR", 2000);

      if (answer == 1)
      {
        memset(picture_name, 0x00, sizeof(picture_name));
        counter = 0;
        while (Serial.available() == 0);
        do {
          picture_name[counter] = Serial.read();
          counter++;
          while (Serial.available() == 0);
        } 
        while (picture_name[counter - 1] != 0x0D);

        picture_name[counter - 1] = '\0';

        Serial.print("Picture name: ");
        Serial.println(picture_name);

        sendATcommand2("AT+CCAME", "OK", "", 2000);

      }
      else
      {
        Serial.println("Error saving the picture");
      }
    }
    else if (answer == 2)
    {
      Serial.println("Camera invalid state");
    }
    else
    {
      Serial.println("Error taking the picture");
    }
  }
  else if (answer == 2)
  {
    Serial.println("Camera not detected");
  }
  else
  {
    Serial.println("Error starting the camera");
  }

}

void send_ftp(){
  //sets the PIN code
  snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
  sendATcommand(aux_str, "OK", 2000);

  while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) || 
    sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

  // sets APN, user name and password
  snprintf(aux_str, sizeof(aux_str), "AT+CGSOCKCONT=1,\"IP\",\"%s\"", apn);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CSOCKAUTH=1,1,\"%s\",\"%s\"", user_name, password);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPSERV=\"%s\"", ftp_server);
  sendATcommand(aux_str, "OK", 2000);

  sendATcommand("AT+CFTPPORT=21", "OK", 2000);
  sendATcommand("AT+CFTPMODE=1", "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPUN=\"%s\"", ftp_user_name);
  sendATcommand(aux_str, "OK", 2000);

  snprintf(aux_str, sizeof(aux_str), "AT+CFTPPW=\"%s\"", ftp_password);
  sendATcommand(aux_str, "OK", 2000);


  // Search audio in "C:/Audio"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",7", clip_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    Serial.println("Audio Upload done");    
  }
  else
  {
    Serial.println("Audio Upload fail");    
  }

  delay(10000);

  // Search picture in "C:/Picture"
  sprintf(aux_str, "AT+CFTPPUTFILE=\"%s\",1", picture_name);
  answer = sendATcommand(aux_str, "+CFTPPUTFILE: 0", 60000);

  if (answer == 1)
  {        
    Serial.println("Picture Upload done");    
  }
  else
  {
    Serial.println("Picture Upload fail");    
  }
}


void power_on(){

  uint8_t answer=0;

  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // power on pulse
    digitalWrite(onModulePin,HIGH);
    delay(3000);
    digitalWrite(onModulePin,LOW);

    // waits for an answer from the module
    while(answer == 0){    
      // Send AT every two 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 there are data in the UART input buffer, reads it and checks for the asnwer
    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)
      {
        answer = 1;
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));

  return answer;
}


int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, 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 there are data in the UART input buffer, reads it and checks for the answer
    if(Serial.available() != 0){    
      response[x] = Serial.read();
      x++;
      // check if the desired answer 1 is in the response of the module
      if (strstr(response, expected_answer1) != NULL)    
      {
        answer = 1;
      }
      // check if the desired answer 2 is in the response of the module
      if (strstr(response, expected_answer2) != NULL)    
      {
        answer = 2;
      }
    }
    // Waits for the answer with time out
  }
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}