Testbed / Mbed 2 deprecated DNDDemoCode161114

Dependencies:   SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DNDDemoCode161114.cpp Source File

DNDDemoCode161114.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 
00004 #define freq 1
00005 #define time_interval_sec 30
00006 
00007 #define dur_sec 20
00008 Ticker int1;
00009 Ticker int2;
00010 Ticker int3;
00011 Ticker int4;
00012 
00013 Serial pc(USBTX, USBRX);
00014 Serial gsm(D1, D0); //tx,rx
00015 DigitalOut dtr(D2);
00016 DigitalOut pwr_key(D3);
00017 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
00018 AnalogIn AC_input(A0);
00019 AnalogIn DC_input(A2);
00020 AnalogIn TC_RLY(A3);
00021 AnalogIn TC_50(A4);
00022 AnalogIn TC_33(A5);
00023 
00024 
00025 union Data
00026     {
00027     unsigned short datai[50000];
00028     char datac[100000];
00029     };
00030 union Data ACdata;
00031 union Data DCdata;
00032 
00033 union Temp
00034     {
00035     unsigned short tempi;
00036     char tempc;
00037     };
00038 char sitename[100] ="MOD2";
00039 char filename[100] = "";
00040 char timestamp[100]="";
00041 char timestampold[100]="";
00042 char main_dir[100] ="";
00043 char month_dir[100]="";
00044 char date_dir[100] ="";
00045 char hour_dir[100] ="";
00046 char pathAC[100]   ="";
00047 char pathDC[100]   ="";
00048 char pathTC[100]   ="";
00049 char response[100] ="";
00050 char command[100] ="";
00051 char result[100] ="";
00052 char FTP_File_Name[100] ="";
00053 char FTPPUT_File_Name[100] ="";
00054 char FTP_Path_Name[100] ="";
00055 
00056 int dateu = 0;
00057 int datet = 0;
00058 int monthu = 0;
00059 int montht = 0;
00060 int year = 0;
00061 int houru = 0;
00062 int hourt = 0;
00063 int minuteu = 0;
00064 int minutet = 0;
00065 int secondu = 0;
00066 int secondt = 0;
00067 
00068 int time_stamp = 0;
00069 int temp = 0;
00070 int ftp_update_flag = 1;
00071 int data_update_flag = 1;
00072 int name_update_flag = 1;
00073 int c,i;
00074 
00075 unsigned long var = 0;
00076 unsigned long currentMillis = 0;
00077 uint8_t x=0,z=0,  answer=0;
00078 
00079 time_t previous;
00080 float tempC;
00081 uint32_t dataLength = dur_sec*freq*1000;
00082 unsigned long j=0;
00083 
00084 
00085 //----------------------------------------------------------------------------------------------------------------------------------
00086 
00087 int8_t sendATcommandclk(char* ATcommand, char* expected_answer, unsigned int timeout)
00088 {
00089     uint8_t x=0,answer=0;//x=0 and answer=0, if not put, AT+CCLK?\r is not executed
00090     char response[100];
00091     memset(response, '\0', 100);    // Initialize the string
00092     wait_ms(100);
00093     while( gsm.readable() > 0) gsm.getc();    // Clean the input buffer
00094     gsm.printf("%s \r", ATcommand);    // Send the AT command 
00095     pc.printf("..@");
00096     x = 0;
00097     previous = time(NULL);
00098     
00099     // this loop waits for the answer
00100     do{ 
00101         if(gsm.readable() != 0){    
00102             
00103             // if there are data in the UART input buffer, reads it and checks for the asnwer
00104           
00105             response[x] = gsm.getc();
00106             pc.putc(response[x]);
00107             x++;
00108             sprintf(timestamp,response);//copies response to timestamp for further processing
00109             
00110             // check if the desired answer  is in the response of the module
00111             if (strstr(response, expected_answer) != NULL)    
00112             {
00113             answer = 1;
00114             } 
00115         }
00116   
00117          // Waits for the asnwer with time out
00118     }while((answer == 0) && ((time(NULL) - previous) < timeout));
00119     //pc.printf("%s \r\n",response);  
00120     FILE *fp=fopen("/sd/TeraTerm.txt","a");
00121         if(fp == NULL)
00122         {
00123         error("Could not open file for write\n\r");
00124         }
00125         fprintf(fp,"%s",response);
00126         fclose(fp);
00127       return answer;
00128 }
00129 
00130 //----------------------------------------------------------------------------------------------------------------------------------
00131     
00132 void GetFileDir()
00133 {
00134     
00135     sendATcommandclk("AT+CCLK?\r", "OK", 5);  
00136     sprintf(response,timestamp);
00137     x=20;
00138     int gsmerr;
00139     year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48);
00140     while(year!=14)
00141     {
00142     wait(1);
00143         GetFileDir();
00144         pc.printf("\r\ncurrent timestamp %s\r\n",timestamp);
00145         
00146     }
00147     sprintf(timestampold,response);
00148     pc.printf("\r\nold time stamp is%s\r\n",timestampold);
00149     //month onwards all parameters are processed in both 'unit' and 'ten' position separately, else for numbers<10, only unit digit is printed e.g 09 is shown as 9 only
00150     montht = (timestamp[x+4]-48); 
00151     monthu = (timestamp[x+5]-48);
00152     datet = (timestamp[x+7]-48);
00153     dateu = (timestamp[x+8]-48);
00154     hourt = (timestamp[x+10]-48);
00155     houru = (timestamp[x+11]-48);
00156     minutet = (timestamp[x+13]-48); 
00157     minuteu = (timestamp[x+14]-48);
00158     secondt = (timestamp[x+16]-48);
00159     secondu = (timestamp[x+17]-48); 
00160     sprintf(main_dir,"/sd/%s",sitename);
00161     sprintf(month_dir,"%s/%s%d%d%d",main_dir,sitename,year,montht,monthu);
00162     sprintf(date_dir,"%s/%s%d%d%d%d%d",month_dir,sitename,year,montht,monthu,datet,dateu);
00163     sprintf(hour_dir,"%s/%s%d%d%d%d%d%d%d",date_dir,sitename,year,montht,monthu,datet,dateu,hourt,houru);
00164     if(timestamp==timestampold)
00165     {
00166     sprintf(filename,"%s/%s%d%d%d%d%d%d%d%d%d%d%dErr%d",hour_dir,sitename,year,montht,monthu,datet,dateu,hourt,houru,minutet,minuteu,secondt,secondu,gsmerr);
00167     gsmerr++;
00168     }
00169     else
00170     {
00171     sprintf(filename,"%s/%s%d%d%d%d%d%d%d%d%d%d%d",hour_dir,sitename,year,montht,monthu,datet,dateu,hourt,houru,minutet,minuteu,secondt,secondu);
00172     gsmerr=0;
00173     }
00174     mkdir(main_dir,0777);
00175     mkdir(month_dir,0777);
00176     mkdir(date_dir,0777);
00177     mkdir(hour_dir,0777);
00178     
00179     //pc.printf("dir are\r\nmain_dir\t%s\r\nmonth_dir\t%s\r\ndate_dir\t%s\r\nhour_dir\t%s\r\nfilename\t%s\r\n",main_dir,month_dir,date_dir,hour_dir,filename);   
00180 }
00181 
00182 //-------------------------------------------------------------------------------------------------------------------------------------
00183 
00184 void temp_val()
00185 {
00186 union Temp TC33temp;
00187 union Temp TC50temp;
00188 union Temp TCRLtemp;
00189 
00190 TC33temp.tempi=TC_33.read_u16();
00191 TC50temp.tempi=TC_50.read_u16();
00192 TCRLtemp.tempi=TC_RLY.read_u16();
00193 
00194 
00195 GetFileDir();
00196 
00197 sprintf(pathTC,"%sTC.txt",hour_dir);
00198 pc.printf("\r\nFilename-TC\t%s", pathTC);
00199 FILE *fp = fopen(pathTC, "a");
00200     
00201 if(fp == NULL)
00202     {
00203     error("Could not open file for write\n\r");
00204     }
00205     
00206 fprintf(fp, "\r\n20%d/%d%d/%d%d-%d%d:%d%d:%d%d",year,montht,monthu,datet,dateu,hourt,houru,minutet,minuteu,secondt,secondu);
00207 fprintf(fp, "\r\n3.3V-%d C",TC33temp.tempi );  
00208 fprintf(fp, "\r\n5.0V-%d C",TC50temp.tempi );  
00209 fprintf(fp, "\r\nRlay-%d C",TCRLtemp.tempi );           
00210 fprintf(fp, ",");
00211 fclose(fp);
00212 pc.printf("\r\n3.3V temp - %c C\t%d",TC33temp.tempc,TC33temp.tempi );  
00213 pc.printf("\r\n5.0V temp - %c C\t%d",TC50temp.tempc,TC50temp.tempi);  
00214 pc.printf("\r\nRly temp - %c C\t%d",TCRLtemp.tempc,TCRLtemp.tempi );    
00215 FILE *fp1=fopen("/sd/TeraTerm.txt","a");
00216     if(fp1 == NULL)
00217     {
00218     error("Could not open file for write\n\r");
00219     }
00220     fprintf(fp1,"\r\nTemperature writing Done\n\r");
00221     fclose(fp1);
00222 pc.printf("\n\rWriting Temp done \n \r"); 
00223  
00224 }
00225 
00226 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
00227 
00228 void calculate_ADC()
00229 {
00230 
00231     Timer t;
00232     
00233     pc.printf("\n\rStarting Reading \n\r");
00234     t.start();
00235     while(j<dataLength)
00236     {        
00237         if((int)(t.read()*100*freq*1000)%100 == 0)
00238         {
00239             ACdata.datai[j] = AC_input.read_u16();
00240             DCdata.datai[j] = DC_input.read_u16();
00241             ++j;
00242         }
00243     }
00244     
00245     t.stop();
00246     pc.printf("Time Taken %f \n\r", t.read());
00247     t.reset();
00248     pc.printf("Total Samples %d\r\n",j);
00249     j=0;
00250     
00251     GetFileDir();
00252     
00253     sprintf(pathAC,"%sAC.txt",filename);
00254     pc.printf("\r\nFilename-AC\t%s", pathAC);
00255     FILE *fp = fopen(pathAC, "w");
00256         if(fp == NULL)
00257         {
00258         error("Could not open file for write\n\r");
00259         }
00260     for(unsigned long k=0; k<dataLength; ++k)
00261         {
00262             if (k%4==0)
00263                 {
00264                     fprintf(fp,"\n\n");
00265                 }
00266             fprintf(fp, "%c%c", ACdata.datac[k]);
00267         }
00268     fprintf(fp, ",");
00269     fclose(fp);
00270     
00271     sprintf(pathDC,"%sDC.txt",filename);
00272     FILE *fp1 = fopen(pathDC, "w");
00273         if(fp1 == NULL)
00274         {
00275         error("Could not open file for write\n\r");
00276         }
00277 
00278     for(unsigned long k=0; k<dataLength; ++k)
00279         {
00280             if (k%4==0)
00281                 {
00282                     fprintf(fp,"\n\n");
00283                 }
00284 
00285             fprintf(fp1, "%c%c", DCdata.datac[k]);
00286         }
00287     fprintf(fp1, ",");
00288     fclose(fp1);
00289     FILE *fp2=fopen("/sd/TeraTerm.txt","a");
00290         if(fp2 == NULL)
00291         {
00292         error("Could not open file for write\n\r");
00293         }
00294     fprintf(fp2,"\r\nSamples writing Done\n\r");
00295     fclose(fp2);
00296     pc.printf("Writing Samples done \n \r");
00297         
00298 }
00299 
00300 //---------------------------------------------------------------------------------------------------------------------------
00301 
00302 void reset_mod()
00303 {
00304     pc.printf("\r\nresetting module\n\n\n\r\n");
00305     
00306     NVIC_SystemReset();     
00307 }
00308 
00309 //---------------------------------------------------------------------------------------------------------------------------
00310 int main()
00311 {
00312     mkdir("/sd/strtup", 0777);
00313     FILE *fp=fopen("/sd/TeraTerm.txt","a");
00314         if(fp == NULL)
00315         {
00316         error("Could not open file for write\n\r");
00317         }
00318         fprintf(fp,"\nHello-Module\r\n");
00319         fclose(fp);
00320     
00321    
00322     pc.printf("Hello\r\n");
00323     wait(2);
00324   
00325     int1.attach(&temp_val,10);
00326     int2.attach(&calculate_ADC,179); 
00327     int3.attach(&reset_mod,3601);       
00328     
00329 }