Development

Communication / GPRS+GPS:

» GPRS SIM928A 21: Downloading files from a FTP server

This example shows how to download a file from a FTP server

Required Materials

1 x Waspmote
1 x Battery
1 x GPRS_SIM928A module
1 x GPRS/3G antenna
1 x SIM card
1 x SD card

Notes

- GPRS_SIM928A module must be powered with a battery
- Be carefully typing the name and the path of the files of SD card and FTP server
- FTP actions need some time to complete it
- When GPRS_SIM928 is in use, interrupts can't be used
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  ------  GPRS_SIM928A_21 - Downloading files from a FTP server  -------- 
 *  
 *  Explanation: This example shows how to download a file 
 *  from a FTP server
 *  
 *  Copyright (C) 2015 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:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

#include "WaspGPRS_SIM928A.h"

char apn[] = "apn";
char login[] = "login";
char password[] = "password";

int answer;

void setup()
{
    USB.println(F("**************************"));
    // 1. sets operator parameters
    GPRS_SIM928A.set_APN(apn, login, password);
    // And shows them
    GPRS_SIM928A.show_APN();
    USB.println(F("**************************")); 
}

void loop()
{

    // setup for Serial port over USB:
    USB.ON();
    USB.println(F("USB port started..."));

    // 2. activates the GPRS_SIM928A module:
    answer = GPRS_SIM928A.ON(); 
    if ((answer == 1) || (answer == -3))
    {
        USB.println(F("GPRS_SIM928A module ready..."));

        // 3. sets pin code:
        USB.println(F("Setting PIN code..."));
        // **** must be substituted by the SIM code
        if (GPRS_SIM928A.setPIN("****") == 1) 
        {
            USB.println(F("PIN code accepted"));
        }
        else
        {
            USB.println(F("PIN code incorrect"));
        }

        // 4. waits for connection to the network:
        answer = GPRS_SIM928A.check(180);    
        if (answer == 1)
        {
            USB.println(F("GPRS_SIM928A module connected to the network..."));

            // 5. configures GPRS Connection for HTTP or FTP applications:
            answer = GPRS_SIM928A.configureGPRS_HTTP_FTP(1);
            if (answer == 1)
            {

                USB.println(F("Downloading the file 1..."));

                // 6a. downloads file from FTP server to the SD card:
                answer = GPRS_SIM928A.downloadFile( "/ftp1/ftp2/fileFTP1.txt", "/dirSD1/dirSD2/fileSD1.txt", "username", "password", "IP_server", "port", 1);
                
                // checks the answer
                if (answer == 1)
                {
                    USB.println(F("Download done"));
                }
                else if(answer < -40)
                {
                    USB.print(F("Download failed. Error code: "));
                    USB.println(answer, DEC);
                    USB.print(F("CME error code: "));
                    USB.println(GPRS_SIM928A.CME_CMS_code, DEC);
                }
                else 
                {
                    USB.print(F("Download failed. Error code: "));
                    USB.println(answer, DEC);
                }

                USB.println(F("Downloading the file 2"));

                // 6b. downloads file from FTP server to the SD card:
                answer = GPRS_SIM928A.downloadFile("/fileFTP2.txt", "/fileSD2.txt", "username", "password", "IP_server", "port", 1);
                
                // checks the answer
                if (answer == 1)
                {
                    USB.println(F("Download done"));
                }
                else if(answer < -40)
                {
                    USB.print(F("Download failed. Error code: "));
                    USB.println(answer, DEC);
                    USB.print(F("CME error code: "));
                    USB.println(GPRS_SIM928A.CME_CMS_code, DEC);
                }
                else 
                {
                    USB.print(F("Download failed. Error code: "));
                    USB.println(answer, DEC);
                }
            }
            else
            {
                USB.println(F("Configuration failed. Error code:"));
                USB.println(answer, DEC);
            }
        }
        else
        {
            USB.println(F("GPRS_SIM928A module cannot connect to the network"));     
        }
    }
    else
    {
        USB.println(F("GPRS_SIM928A module not ready"));    
    }

    // 7. powers off the GPRS_SIM928A module
    GPRS_SIM928A.OFF(); 

    USB.println(F("Sleeping..."));

    // 8. sleeps one hour
    PWR.deepSleep("00:01:00:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

}

Output

H#
**************************
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
**************************
USB port started...
GPRS_SIM928A module ready...
Setting PIN code...
PIN code accepted
GPRS_SIM928A module connected to the network...
Downloading the file 1
Download done
Downloading the file 2
Download done
Sleeping...

Quick Publish: