ftp

Dependencies:   SDFileSystem mbed-rtos mbed

Revision:
1:8dd592e91577
Parent:
0:001407b7f10c
--- a/main.cpp	Sun Jan 18 12:54:35 2015 +0000
+++ b/main.cpp	Mon Jan 19 11:48:57 2015 +0000
@@ -4,7 +4,7 @@
 #define freq 1
 
 
-#define dur_sec 0.5
+#define dur_sec 1
 
 Serial pc(USBTX, USBRX);
 Serial gsm(D1, D0); //tx,rx
@@ -24,18 +24,9 @@
 unsigned short DCdata[20000];
 unsigned short DC_Detect_data[10];
 
-/*union Data
-    {
-    unsigned short datai[50000];
-    char datac[100000];
-    };
-union Data ACdata;
-union Data DCdata;*/
-
-
-unsigned short TC33temp;
-unsigned short TC50temp;
-unsigned short TCRLtemp;
+//unsigned short TC33temp;
+//unsigned short TC50temp;
+//unsigned short TCRLtemp;
 
 char sitename[100] ="MODZ";
 char filename[100] = "";
@@ -65,7 +56,7 @@
 char ftpget[100];
 int reset_time=120; //mins
 int gsmerr=0;
-
+int count1=0;
 //--------------------------------------------------------------------------------------------------//
 //                  Module reset                                                                    //
 //--------------------------------------------------------------------------------------------------//
@@ -76,47 +67,6 @@
 //--------------------------------------------------------------------------------------------------//
 //                  Send AT Command (1 response)                                                    //
 //--------------------------------------------------------------------------------------------------//
-int8_t sendATcommand3(char* ATcommand, char* expected_answer, unsigned int timeout)
-{
-    uint8_t x=0,answer=0;                                                                           //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
-    char response[100];
-  // int time_previous;
-    memset(response, '\0', 100);                                                                    // Initialize the string
-    wait_ms(100);
-    while( gsm.readable() > 0) gsm.getc();                                                          // Clean the input buffer
-    gsm.printf("%s \r", ATcommand);                                                                 // Send the AT command 
-    x = 0;
-    previous = time(NULL);
-    do                                                                                              // this loop waits for the answer
-    { 
-        if(gsm.readable() != 0)
-        {    
-            response[x] = gsm.getc();                                                               // if there are data in the UART input buffer, reads it and checks for the asnwer 
-            pc.putc(response[x]);
-            x++;
-            if (strstr(response, expected_answer) != NULL)                                          // check if the desired answer  is in the response of the module
-            {
-                answer = 1;
-            } 
-        }
-    }while((answer == 0) && ((time(NULL) - previous) < timeout));    
-    FILE *fp9 = fopen("/sd/Record.txt", "a");
-    if(fp9==NULL)
-    {
-        error("Could not open the file for write\r\n");
-    }
-    fprintf(fp9,"%s",response);
-    fclose(fp9);                                // Waits for the asnwer with time out
-   /* if(clk_flag == 1)
-    {
-        sprintf(timestamp,response);                                                                //copies response to timestamp for further processing    }
-    }*/
-    return answer;
-}
-
-//--------------------------------------------------------------------------------------------------//
-//                  Send AT Command (1 response)                                                    //
-//--------------------------------------------------------------------------------------------------//
 int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, bool clk_flag)
 {
     uint8_t x=0,answer=0;                                                                           //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
@@ -354,7 +304,7 @@
                         }             
             }
         fclose(fp2);
-        pc.printf("\n storage done");
+        pc.printf("\n\r storage done \n\r");
         osSignalSet(tid6, 0x1); 
               
     }
@@ -447,23 +397,38 @@
 //                 FTP Send Function                                                                //
 //--------------------------------------------------------------------------------------------------//
 
-void ftp_put(bool ftp_type)
+void ftp_put(bool ftp_type,int a)
 {  
     char ftpfilename[30]="";
-    char ftpfilepath[100]="";
-    if (ftp_type ==0)
-    {
-    sprintf(ftpfilename,"%s",filenameAC);
-    sprintf(ftpfilepath,"%s",pathAC);
-    }
-    else if (ftp_type ==1)
-    {
-    sprintf(ftpfilename,"%s",filenameDC);
-    sprintf(ftpfilepath,"%s",pathDC);
-    }
+    char ftpfilepath[100]="";  
+   
+      if(((ftp_type==0) || (ftp_type==1)) && (a==0));
+       {
+           if ((ftp_type ==0)&&(a==0))
+             {
+                pc.printf("@@@@");
+                sprintf(ftpfilename,"%s",filenameAC);                                       
+                sprintf(ftpfilepath,"%s",pathAC);
+             }
+           else if((ftp_type==1) && (a==0))
+             {
+               pc.printf("###");
+               sprintf(ftpfilename,"%s",filenameDC);
+               sprintf(ftpfilepath,"%s",pathDC);
+             }
+       }         
+       while(((ftp_type==0) && (a==1)))
+        {
+            pc.printf("\r\n##\r\n");
+        sprintf(ftpfilename,"%s","MODZ150119112458AC.txt");                                       
+        sprintf(ftpfilepath,"%s",ftpget);
+        pc.printf("\r\n@@@\r\n");
+        break;
+        } 
     pc.printf("\r\nftp_put attempt\n\r");
     char command1[40];
     char result1[20];
+    pc.printf("\r\r--%s--",ftpfilename);
     sprintf(command1,"%s\"%s\"", "AT+FTPPUTNAME=", ftpfilename);
     sprintf(result1,"%s", "OK");
     for(int putname_attempt=0; putname_attempt<3;putname_attempt++)
@@ -471,7 +436,7 @@
        if(sendATcommand(command1,result1, 10,0))
         {
           //  pc.printf("\n %p",ftpfilename);
-            sendATcommand("AT+FTPPUTPATH=\"/Trial//\"", "OK", 5,0);
+            sendATcommand("AT+FTPPUTPATH=\"/Trial/\"", "OK", 5,0);
             if(sendATcommand("AT+FTPPUT=1", "OK", 10,0))
             {
                    // pc.printf("in loop");
@@ -492,6 +457,7 @@
                                     gsm.printf("%c", fgetc(fp9));
                                     
                                 }
+                                pc.printf("file transfered succesfully");
                                 break;
                             }
                             else 
@@ -520,53 +486,47 @@
     }
    // pc.printf("\n\r %s\n\r","i am not able to send data");
 }
-/*
+
 //-----------------------------------------------------------------------------------------------------------------//
 //                         FTP Get 
 //----------------------------------------------------------------------------------------------------------------//
 void downloadFTP()
 
 {
-    char incoming_data[100];
-   
+    Timer t;
+    char incoming_data[100]="";
     //int data_size, aux;
-    
     int x = 0;
     int attempt=0;
     int allowed_attempt=4;
-    sendATcommand3("AT+FTPGETNAME=\"abc.txt\"", "OK", 5);
-    sendATcommand3("AT+FTPGETPATH=\"/\"", "OK", 5);
+    sendATcommand("AT+FTPGETNAME=\"abc.txt\"", "OK", 5,0);
+    sendATcommand("AT+FTPGETPATH=\"/\"", "OK", 5,0);
     while(attempt<allowed_attempt)
     {
-       if (sendATcommand3("AT+FTPGET=1", "+FTPGET:1,1",20)==1)
+       if (sendATcommand("AT+FTPGET=1", "+FTPGET:1,1",20,0)==1)
        {
-       
-       
-          for(int i=0; i<4;i++)
+         for(int i=0; i<4;i++)
           {
            if (sendATcommand2("AT+FTPGET=2,70","+FTPGET:2,","+FTPGET:1,",10) == 1)
-            {
-                  
+            {                  
                     //data_size = 0;
                     int previous = time(NULL);
-   
-   
-                   do{
-                        if(gsm.readable() == 1){    
-                            
+                    do{
+                        if(gsm.readable() == 1)
+                        {                            
                            incoming_data[x] = gsm.getc();
                             x++;
-                          }
+                         }
                    }while((time(NULL) - previous) < 5);
-                   
-                    
+                                
                     pc.printf("Data is %s\r\n",incoming_data);
-                   // const char s[2] = "\n";
-                    //char *token;
-                    //int len=0;
-                    //token = strtok(incoming_data, s);
+                    const char s[2] = "\n";
+                    char *token;
+                    token = strtok(incoming_data, s);
+                    token= strtok(NULL,s);
                     //len = atoi(token);
-                    strncpy(ftpget,incoming_data+3,51);
+                    //strncpy(ftpget,incoming_data+3,51);
+                    sprintf(ftpget,token);
                     pc.printf("Data is %s",ftpget);
                     FILE *fp1 = fopen("/sd/Query.txt","w");
                     fprintf(fp1,"%s",ftpget);
@@ -585,14 +545,11 @@
          {
             attempt++;
          }  
-       
     }
-    pc.printf("\n@@@");
-}
+   
+}   
 
-                
-  */    
- 
+
 //--------------------------------------------------------------------------------------------------//
 //                 FTP Function Thread                                                              //
 //--------------------------------------------------------------------------------------------------//
@@ -603,15 +560,25 @@
         
     while(true)
     {
+      
         osSignalWait(0x1, osWaitForever);
+          
         if(ftp_connect())
-        {
-            
-            ftp_put(0);
-            ftp_put(1);
-           // downloadFTP();
-          
-        }
+        {  
+      //  Timer t;
+       // t.start();          
+       //     ftp_put(0,0);
+        //ftp_put(1,0);
+       // t.stop(); 
+       // pc.printf("\n\r Time=%f",t.read());  
+            if (count1==0)
+             {   downloadFTP();
+                 ftp_put(0,1);
+                 int count1=0;
+                break;
+             }     
+          count1++;
+        } 
     }
 }
 //--------------------------------------------------------------------------------------------------//
@@ -641,7 +608,7 @@
             //pc.printf("\r\n%s",timestamp);
             fclose(fp);
          }
-    
+   
     Thread thread1(sampling);
     Thread thread3(store);
     Thread thread6(FTP_Fun);