storing variables in single array for transmission

Dependencies:   SDFileSystem dspmodified mbed

Fork of FTPGET_Merged by Pravin Magar

Revision:
12:0ab0a2578025
Parent:
10:9184e2fb5974
Child:
13:73d6de10d15c
--- a/merged_code.cpp	Mon Mar 09 13:17:46 2015 +0000
+++ b/merged_code.cpp	Wed Mar 11 11:42:19 2015 +0000
@@ -9,7 +9,7 @@
 #define freq 1
 #define fft_points 16384
 #define dur_sec 16.384
-#define module_name "TST2"                         //enter module name here
+#define module_name "TST3"                         //enter module name here
 
 Serial pc(USBTX, USBRX);
 Serial gsm(D1, D0);
@@ -83,7 +83,7 @@
 int sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, int clk_flag)
 {
     int grs=0;
-    int answer=0;                                                                           //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
+    int answer=0;                                                                                   //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
     char response[300];
     memset(response, '\0', 100);                                                                    // Initialize the string
     wait_ms(100);
@@ -97,30 +97,30 @@
             //pc.putc(dummy);
         } 
     }
-    while((time(NULL) - previous) < 1);                                                          // Clean the input buffer
+    while((time(NULL) - previous) < 1);                                                             // Clean the input buffer
     //pc.printf("\r\n");
     gsm.printf("%s \r", ATcommand);                                                                 // Send the AT command
     grs = 0;
     previous = time(NULL);
     do 
-    {                                                                                            // this loop waits for the answer
+    {                                                                                               // this loop waits for the answer
         if(gsm.readable() != 0) 
         {
-            response[grs] = gsm.getc();                                                               // if there are data in the UART input buffer, reads it and checks for the asnwer
+            response[grs] = gsm.getc();                                                             // if there are data in the UART input buffer, reads it and checks for the asnwer
             pc.putc(response[grs]);
             grs++;
             if (strstr(response, expected_answer) != NULL) 
-            {                                        // check if the desired answer  is in the response of the module
+            {                                                                                       // check if the desired answer  is in the response of the module
                     if(clk_flag==2) answer=2;
                     else answer=1;
             }
         }
-    } while((answer !=1) && ((time(NULL) - previous) < timeout));                                  // Waits for the asnwer with time out
+    } while((answer !=1) && ((time(NULL) - previous) < timeout));                                   // Waits for the asnwer with time out
     if(clk_flag == 1) 
     {
         //pc.printf("\r\nclock updated\r\n");
         sprintf(timestamp,response);
-        pc.printf("%s",timestamp);                                                                //copies response to timestamp for further processing 
+        pc.printf("%s",timestamp);                                                                  //copies response to timestamp for further processing 
     }
     //pc.printf("\r\nanswer %d\r\n",answer);
     return answer;
@@ -142,6 +142,7 @@
     second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48);
 
     sprintf(time_stamp,"%02d%02d%02d%02d%02d%02d",year,month,date,hour,minute,second);
+    //pc.printf("Get time result:%s.....",time_stamp);
 }
 
 //--------------------------------------------------------------------------------------------------//
@@ -160,12 +161,13 @@
         attempt_cnt++;
         if(attempt_cnt>4) break;
     }
-    if(year>14&&year<21) 
+    if(year>14&&year<21&&month<13) 
     {
         if((minute!=minuteold)||(second!=secondold)) 
         {
             sprintf(timestampold,timestamp);
-            //pc.printf("\r\nold time stamp is%s\r\n",timestampold);
+            
+            pc.printf("\r\nold time stamp is%s\r\n",timestampold);
             minuteold=minute;
             secondold=second;        
             sprintf(main_dir,"/sd/%s",sitename);
@@ -192,8 +194,10 @@
     else 
     {
         sprintf(timestamp,timestampold);
-        sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr);
-        sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr);
+        //pc.printf("\r\nTimestamp......%s\r\n",timestamp);
+
+        sprintf(filepath,"%s/%s%02d%02dErr%03d",hour_dir,sitename,minuteold,secondold,gsmerr);
+        sprintf(filename,"%s%02d%02dErr%03d",sitename,minuteold,secondold,gsmerr);
         gsmerr++;
     }
 }
@@ -364,6 +368,7 @@
     if(fp == NULL) 
     {
         pc.printf("Could not open file for write\n\r");
+        mkdir("/sd/jas",0777);
         //reset_mod();
     } 
     else 
@@ -424,6 +429,7 @@
     sprintf(pathAC,"%sAC.txt",filepath);
     //pc.printf("\r\nFilename-AC\t%s", pathAC);
     FILE *fp = fopen(pathAC, "w");
+    
     if(fp == NULL) 
     {
         //pc.printf("Could not open file for write\n\r");
@@ -460,7 +466,7 @@
     }
 // DC Detect storage
     sprintf(pathDCdetect,"%sData.txt",hour_dir);
-    //pc.printf("\r\nFilename-TC\t%s", pathDCdetect);
+    //pc.printf("\r\nFilename-DCD\t%s", pathDCdetect);
     fp = fopen(pathDCdetect, "w");
     if(fp == NULL) 
     {
@@ -518,9 +524,10 @@
     }
     
     fp = fopen(pathfft, "w");
+    //pc.printf("\r\nopened path fft\r\n");
     if(fp == NULL) 
     {
-        //pc.printf("Could not open file for write\n\r");
+        //pc.printf("Could not open file for write pathfft\n\r");
         //reset_mod();
     } 
     else 
@@ -536,14 +543,34 @@
     prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments,0);
 
 //TIME STAMP FILE WRITE FUNCTION    
-    sprintf(timestampname,"TS%s%02d%02d%02d%02d.txt",sitename,year,month,date,hour);
-    sprintf(timestamppath,"/sd/%s",timestampname);
-    pc.printf("\r\n Timestampname is %s\r\n",timestampname);   
+    if(gsmerr==0)
+    {
+        
+        sprintf(timestampname,"TS%s%02d%02d%02d%02d.txt",sitename,year,month,date,hour);
+        sprintf(timestamppath,"/sd/%s",timestampname);
+    }
+    else
+    {
+        
+        sprintf(timestampname,"TS%sErr.txt",sitename);
+        //pc.printf("\r\n%s",timestampname);
+        sprintf(timestamppath,"/sd/%s",timestampname);
+        
+    }
+    //pc.printf("\r\n Timestampname is %s\r\n",timestampname);   
     FILE *fp13 = fopen(timestamppath,"a");
-    fprintf(fp13,"\r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect/100,AC_rms_value/100000,maxfreq);
-    pc.printf("Value is \r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect,AC_rms_value,maxfreq);
-    fclose(fp13);
     
+    if(fp == NULL) 
+    {
+        //pc.printf("Could not open file for write pathfft\n\r");
+        //reset_mod();
+    } 
+    else
+    {
+        fprintf(fp13,"\r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect/100,AC_rms_value/100000,maxfreq);
+        //pc.printf("Value is \r\n%02d%02d %d %.0f %d",minute,second,avg_dc_detect,AC_rms_value,maxfreq);
+        fclose(fp13);
+    }
     pc.printf("\r\nTimestamp.txt Storage Done\n\r");
         
 //DC fft
@@ -919,7 +946,7 @@
     wait(8);
     gsm_init();
     mkdir("/sd/jas",0777);
-    int old_hour=100;
+    int old_hour=100,loopcount=0;
     pc.printf("\r\nHelloWorld\r\n");
     sendATcommand("AT+CLTS=1\r", "OK", 15,2);
     //wait(10);
@@ -946,7 +973,7 @@
         sampling();
         store();
         FTP_Fun();
-        if (old_hour!=100 && hour!=old_hour)
+        if (old_hour!=100 && hour!=old_hour && hour<24)
         {
             pc.printf("\r\nhour changed");
             if(ftp_connect())
@@ -961,10 +988,11 @@
         old_hour=hour;
         sprintf(timestampname_old,timestampname);
         sprintf(timestamppath_old,timestamppath);
-        if(hour%2>0) 
+        if(hour%2>0 || loopcount>120) 
         {
-            if(minute>3 && minute<6)
+            if((minute>3 && minute<6)||(loopcount>120))
             {
+                loopcount=0;
                 reset_mod();
             }
         }