WiFi PRO 19: FTP make directory

This example shows how to perform FTP download. The file transferred has previously been created in the server. The file is downloaded into the Waspmote's SD card.

Required Materials

1 x Waspmote 1 x Battery 1 x WiFi PRO Module

Notes

- The battery has to be connected. - This example can be executed in Waspmote v15

Code

/*  
 *  ------ WIFI Example -------- 
 *  
 *  Explanation: This example shows how to perform FTP download.
 *  The file transferred has previously been created in the server. 
 *  The file is downloaded into the Waspmote's SD card.
 *  
 *  Copyright (C) 2016 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 .  
 *  
 *  Version:           3.0
 *  Design:            David Gascon 
 *  Implementation:    Yuri Carmona
 */

// Put your libraries here (#include ...)
#include <WaspWIFI_PRO.h> 


// choose socket (SELECT USER'S SOCKET)
///////////////////////////////////////
uint8_t socket = SOCKET0;
///////////////////////////////////////


// choose FTP server settings
///////////////////////////////////////
char SERVER[]   = "pruebas.libelium.com";
char PORT[]     = "21";
char USER[]     = "w@libelium.com";
char PASSWORD[] = "ftp1234";
///////////////////////////////////////

/////////////////////////////////////////////////////////////////////// 
// Define filenames for SD card and FTP server:
//  - If the file is in the root directory: "FILE.TXT" or "/FILE.TXT" 
//  - If the file is inside a folder: "/FOLDER/FILE.TXT"
///////////////////////////////////////////////////////////////////////
char SERVER_FILE[]   = "/FILE1.TXT";
char SD_FILE[]       = "/FILE2.TXT";
char SERVER_FOLDER[] = "/FOLDER";
///////////////////////////////////////////////////////////////////////


// define variables
uint8_t error;
uint8_t status;
unsigned long previous;
uint8_t sd_answer;
uint16_t ftp_handle = 0;


void setup() 
{
  USB.println(F("Start program"));  
  USB.println(F("***************************************"));  
  USB.println(F("Once the module is set with one or more"));
  USB.println(F("AP settings, it attempts to join the AP"));
  USB.println(F("automatically once it is powered on"));    
  USB.println(F("Refer to example 'WIFI_PRO_01' to configure"));  
  USB.println(F("the WiFi module with proper settings"));
  USB.println(F("***************************************"));
  
  //////////////////////////////////////////////////
  // 1. Create a new file to upload
  //////////////////////////////////////////////////  

  ///////////////////////////////////
  // 1.1. Init SD card
  ///////////////////////////////////
  sd_answer = SD.ON();

  if (sd_answer == 1)
  {
    USB.println(F("1. SD init OK"));
  }
  else 
  {
    USB.println(F("1. SD init ERROR"));  
  }

  ///////////////////////////////////
  // 1.2. Delete file if exists
  ///////////////////////////////////
  sd_answer = SD.del(SD_FILE);

  if (sd_answer == 1)
  {
    USB.println(F("2. File deleted"));
  }
  else 
  {
    USB.println(F("2. File NOT deleted"));  
  }

  ///////////////////////////////////
  // 1.3. Create file
  ///////////////////////////////////
  sd_answer = SD.create(SD_FILE);

  if (sd_answer == 1)
  {
    USB.println(F("3. File created"));
  }
  else 
  {
    USB.println(F("3. File NOT created"));  
  }

  ///////////////////////////////////
  // 1.4. Append contents  
  ///////////////////////////////////
  USB.println(F("4. Appending data..."));  
  for( int i=0; i<100; i++)
  {
    sd_answer = SD.append( SD_FILE, "This is a new message\n");

    if (sd_answer != 1)
    {
      USB.println(F("SD append error"));
    }
  }

  ///////////////////////////////////
  //1.5. Close SD
  ///////////////////////////////////
  SD.OFF();   
  USB.println(F("5. SD off"));
  USB.println(F("Setup done\n\n"));
  
  
}



void loop()
{ 

  // get actual time
  previous = millis();  

  //////////////////////////////////////////////////
  // 1. Switch ON
  //////////////////////////////////////////////////  
  error = WIFI_PRO.ON(socket);

  if (error == 0)
  {    
    USB.println(F("1. WiFi switched ON"));
  }
  else
  {
    USB.println(F("1. WiFi did not initialize correctly"));
  }


  //////////////////////////////////////////////////
  // 2. Check if connected
  //////////////////////////////////////////////////  

  // check connectivity
  status =  WIFI_PRO.isConnected();

  // Check if module is connected
  if (status == true)
  {    
    USB.print(F("2. WiFi is connected OK"));
    USB.print(F(" Time(ms):"));    
    USB.println(millis()-previous);
  }
  else
  {
    USB.print(F("2. WiFi is connected ERROR")); 
    USB.print(F(" Time(ms):"));    
    USB.println(millis()-previous);      
  }



  //////////////////////////////////////////////////
  // 3. FTP
  //////////////////////////////////////////////////  

  // Check if module is connected
  if (status == true)
  {
    ////////////////////////////////////////////////
    // 3.1. Open FTP session
    ////////////////////////////////////////////////
    error = WIFI_PRO.ftpOpenSession( SERVER, PORT, USER, PASSWORD );

    // check response
    if (error == 0)
    {      
      // get FTP handle
      ftp_handle = WIFI_PRO._ftp_handle;

      USB.println(F("3.1. Open FTP session OK"));   


      //////////////////////////////////////////////
      // 3.2. Make new directory in server
      //////////////////////////////////////////////
      error = WIFI_PRO.ftpMakeDir(ftp_handle, SERVER_FOLDER);

      // check response
      if (error == 0)
      {
        USB.print(F("3.2. Created new folder in server: "));
        USB.println(SERVER_FOLDER);
      }
      else
      {
        USB.println(F("3.2. Error calling 'ftpMakeDir' function")); 
        WIFI_PRO.printErrorCode();
      }


      //////////////////////////////////////////////
      // 3.3. Change Current Working Directory
      //////////////////////////////////////////////
      error = WIFI_PRO.ftpChangeCWD(ftp_handle, SERVER_FOLDER);

      // check response
      if (error == 0)
      {
        USB.print(F("3.3. Changed to new Current Working Directory:"));
        USB.println(SERVER_FOLDER);
      }
      else
      {
        USB.println(F("3.3. Error calling 'ftpChangeCWD' function")); 
        WIFI_PRO.printErrorCode();
      }
      

      ////////////////////////////////////////////
      // 3.4. Uploading file
      ////////////////////////////////////////////
      USB.print(F("3.4. Uploading file: "));
      error = WIFI_PRO.ftpUpload(ftp_handle, SERVER_FILE, SD_FILE);

      // check response
      if (error == 0)
      {
        USB.println(F("OK"));    
      }
      else
      {
        USB.println(F("ERROR")); 
        WIFI_PRO.printErrorCode();
      }    


      //////////////////////////////////////////////
      // 3.5. List Current Working Directory contents
      //////////////////////////////////////////////
      USB.println(F("3.5. List Current Working Directory contents:"));
      error = WIFI_PRO.ftpListing(ftp_handle);

      // check response
      if (error == 0)
      {
        SD.ON();
        SD.showFile(WIFI_PRO_LISTFILE);
        SD.OFF();
      }
      else
      {
        USB.println(F("Error calling 'ftpListing' function")); 
        WIFI_PRO.printErrorCode();
      }
      
      
      //////////////////////////////////////////////
      // 3.6. Change Current Working Directory
      //////////////////////////////////////////////
      error = WIFI_PRO.ftpChangeCWD(ftp_handle, "..");

      // check response
      if (error == 0)
      {
        USB.println(F("3.6. Moved to root directory"));
      }
      else
      {
        USB.println(F("3.6. Error calling 'ftpChangeCWD' function")); 
        WIFI_PRO.printErrorCode();
      }
      
      //////////////////////////////////////////////
      // 3.7. List Current Working Directory contents
      //////////////////////////////////////////////
      USB.println(F("3.7. List Current Working Directory contents:"));
      error = WIFI_PRO.ftpListing(ftp_handle);

      // check response
      if (error == 0)
      {
        SD.ON();
        SD.showFile(WIFI_PRO_LISTFILE);
        SD.OFF();
      }
      else
      {
        USB.println(F("Error calling 'ftpListing' function")); 
        WIFI_PRO.printErrorCode();
      }          


      ////////////////////////////////////////////////
      // 3.8. Close FTP session
      ////////////////////////////////////////////////
      error = WIFI_PRO.ftpCloseSession(ftp_handle);

      // check response
      if (error == 0)
      {
        USB.println(F("3.8. Close FTP session OK"));   
      }
      else
      {
        USB.println(F("3.8. Error calling 'closeSessionFTP' function"));
        WIFI_PRO.printErrorCode();      
      }
    }
    else
    {
      USB.println(F("3.1. Error calling 'ftpOpenSession' function"));
      WIFI_PRO.printErrorCode();      
    }

  }

  USB.print(F("Time from OFF state (ms):"));    
  USB.println(millis()-previous);   


  //////////////////////////////////////////////////
  // 4. Switch OFF
  //////////////////////////////////////////////////  
  WIFI_PRO.OFF(socket);
  USB.println(F("4. WiFi switched OFF"));
  USB.println(F("Wait...\n\n"));
  delay(10000);

}

Output

H#
Start program
***************************************
Once the module is set with one or more
AP settings, it attempts to join the AP
automatically once it is powered on
Refer to example 'WIFI_PRO_01' to configure
the WiFi module with proper settings
***************************************
1. SD init OK
2. File deleted
3. File created
4. Appending data...
5. SD off
Setup done


1. WiFi switched ON
2. WiFi is connected OK Time(ms):5153
3.1. Open FTP session OK
3.2. Created new folder in server: /FOLDER
3.3. Changed to new Current Working Directory:/FOLDER
3.4. Uploading file: OK
3.5. List Current Working Directory contents:

-------------------
I/ONLINE

-------------------
3.6. Moved to root directory
3.7. List Current Working Directory contents:

-------------------
-rw-rw---- 1 507 48 0 May 19 2014 100kB.txt
-rw-rw---- 1 507 48 0 May 19 2014 10B.txt
-rw-rw---- 1 507 48 0 May 19 2014 10b.txt
-rw-rw---- 1 507 48 305 Sep 17 2014 131130.TXT
-rw-rw---- 1 507 48 305 Sep 17 2014 131130_ABCEDEFGH_1234567890.TXT
-rw-rw---- 1 507 48 103 Aug 31 2015 DEVICES.TXT
-rw-rw---- 1 507 48 2200 Apr 12 12:00 FILE.TXT
-rw-rw-r-- 1 507 48 2200 Jul 11 10:03 FILE1.TXT
-rw-rw---- 1 507 48 2200 Jul 11 09:48 FILE2.TXT
-rw-rw---- 1 507 48 2300 Feb 19 2015 FILE5.TXT
-rw-rw---- 1 507 48 75245 Jul 08 2015 FILETEST.TXT
-rw-rw---- 1 507 48 230 Aug 28 2015 FILEYU.TXT
-rw-rw---- 1 507 48 2300 May 19 2014 FILEYURI.TXT
drwxrwsr-x 2 507 48 4096 Jul 11 10:03 FOLDER
drwxrwsr-x 2 507 48 4096 Apr 12 11:20 FOLDER1
drwxrwsr-x 2 507 48 4096 Apr 12 14:23 FOLDER2
-rw-rw---- 1 507 48 36156 Aug 15 2014 SKETCH_
-rw-rw---- 1 507 48 100744 Aug 15 2014 SKETCH_.hex
-rw-rw---- 1 507 48 102400 May 06 2014 T100kB.txt
-rw-rw---- 1 507 48 10240 May 06 2014 T10kB.txt
-rw-rw---- 1 507 48 1024 May 07 2014 T1kB.txt
-rw-rw---- 1 507 48 2048 May 07 2014 T2kB.txt
-rw-rw---- 1 507 48 3072 May 07 2014 T3kB.txt
-rw-rw---- 1 507 48 4096 May 07 2014 T4kB.txt
-rw-rw---- 1 507 48 465945 May 07 2014 T500kB.txt
-rw-rw---- 1 507 48 5120 May 07 2014 T5kB.txt
-rw-rw---- 1 507 48 128 Feb 24 2014 status_366360762_240214_1830.TXT
-rw-rw---- 1 507 48 128 Feb 26 2014 status_366360762_260214_1044.TXT

-------------------
3.8. Close FTP session OK
Time from OFF state (ms):34763
4. WiFi switched OFF
Wait...

Last updated