ftp
Dependencies: SDFileSystem mbed-rtos mbed
main.cpp
- Committer:
- pravinmagar
- Date:
- 2015-01-19
- Revision:
- 1:8dd592e91577
- Parent:
- 0:001407b7f10c
File content as of revision 1:8dd592e91577:
#include "mbed.h" #include "SDFileSystem.h" #include "rtos.h" #define freq 1 #define dur_sec 1 Serial pc(USBTX, USBRX); Serial gsm(D1, D0); //tx,rx DigitalOut dtr(D2); DigitalOut pwr_key(D3); DigitalOut gsm_rst(D7); SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS AnalogIn AC_input(A0); AnalogIn AC2_input(A1); AnalogIn DC_input(A2); AnalogIn DC_Detect(A3); AnalogIn TC1(A4); AnalogIn TC2(A5); unsigned short ACdata[20000]; unsigned short DCdata[20000]; unsigned short DC_Detect_data[10]; //unsigned short TC33temp; //unsigned short TC50temp; //unsigned short TCRLtemp; char sitename[100] ="MODZ"; char filename[100] = ""; char filepath[100] = ""; char timestamp[100]=""; char timestampold[100]=""; char main_dir[100] =""; char hour_dir[100] =""; char pathAC[100] =""; char pathDC[100] =""; char pathTC[100] =""; char filenameAC[100] =""; char filenameDC[100] =""; char clk_response[100] =""; int m=0,mfinal; int date, month, year, hour, minute, second, temp = 0; int c,i; int ns; unsigned long var = 0; unsigned long currentMillis = 0; uint8_t x=0,z=0, answer=0; time_t previous; float tempC; uint32_t dataLength = dur_sec*freq*1000; osThreadId tid1,tid2,tid3,tid4,tid5,tid6; char ftpget[100]; int reset_time=120; //mins int gsmerr=0; int count1=0; //--------------------------------------------------------------------------------------------------// // Module reset // //--------------------------------------------------------------------------------------------------// void reset_mod() { NVIC_SystemReset(); } //--------------------------------------------------------------------------------------------------// // 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 char response[100]; 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)); // 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 (2 response) // //--------------------------------------------------------------------------------------------------// int8_t sendATcommand2(char* ATcommand, char* expected_answer1,char* expected_answer2, unsigned int timeout) { uint8_t x=0, answer=0; char response[100]; time_t previous; memset(response, '\0', 100); // Initialize the string //Thread::wait(100); 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) // if there are data in the UART input buffer, reads it and checks for the asnwer { response[x] = gsm.getc(); pc.putc(response[x]); // sprintf(ftpget,"%s",response); x++; if (strstr(response, expected_answer1) != NULL) // check if the desired answer 1 is in the response of the module { answer = 1; } if (strstr(response, expected_answer2) != NULL) // check if the desired answer 2 is in the response of the module { answer = 2; break; } } } while((answer == 0) && ((time(NULL) - previous) < timeout)); // Waits for the asnwer with time out return answer; } //--------------------------------------------------------------------------------------------------// // Getting File Directories and names // //--------------------------------------------------------------------------------------------------// void GetFileDir() { int attempt_cnt=0; char month_dir[100]=""; char date_dir[100] =""; sendATcommand("AT+CCLK?\r", "OK", 5,1); //sprintf(response,timestamp); x=20; year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48); while(year!=14&&year!=15) { sendATcommand("AT+CCLK?\r", "OK", 5,0); year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48); attempt_cnt++; if(attempt_cnt>4) break; } if(year==14||year==15) { if(timestamp!=timestampold) { sprintf(timestampold,timestamp); //pc.printf("\r\nold time stamp is%s\r\n",timestampold); month = (timestamp[x+4]-48)*10+(timestamp[x+5]-48); date = (timestamp[x+7]-48)*10+(timestamp[x+8]-48); hour = (timestamp[x+10]-48)*10+(timestamp[x+11]-48); minute = (timestamp[x+13]-48)*10+(timestamp[x+14]-48); second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48); sprintf(main_dir,"/sd/%s",sitename); sprintf(month_dir,"%s/%s%02d%02d",main_dir,sitename,year,month); sprintf(date_dir,"%s/%s%02d%02d%02d",month_dir,sitename,year,month,date); sprintf(hour_dir,"%s/%s%02d%02d%02d%02d",date_dir,sitename,year,month,date,hour); sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02d",hour_dir,sitename,year,month,date,hour,minute,second); sprintf(filename,"%s%02d%02d%02d%02d%02d%02d",sitename,year,month,date,hour,minute,second); gsmerr=0; mkdir(main_dir,0777); mkdir(month_dir,0777); mkdir(date_dir,0777); mkdir(hour_dir,0777); } else { 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); gsmerr++; } } 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); gsmerr++; } } //--------------------------------------------------------------------------------------------------// // DC Detect // //--------------------------------------------------------------------------------------------------// void DC_Detection() { Timer t; unsigned long j=0; t.start(); while(j<10) { if((int)(t.read()*50*freq*1000)%100 == 0) { DC_Detect_data[j] = DC_Detect.read_u16(); ++j; } } t.stop(); // pc.printf("\r\nTemp Time Taken %f \n\r", t.read()); t.reset(); // pc.printf("Total Samples %d\r\n",j); osSignalSet(tid3, 0x1); } //--------------------------------------------------------------------------------------------------// // AC-DC Sampling // //--------------------------------------------------------------------------------------------------// void calculate_ADC() { Timer t; unsigned long j=0; //pc.printf("\n\rStarting Reading \n\r"); t.start(); while(j<dataLength) { if((int)(t.read()*100*freq*1000)%100 == 0) { ACdata[j] = AC_input.read_u16(); DCdata[j] = DC_input.read_u16(); ++j; } } t.stop(); //pc.printf("Time Taken %f \n\r", t.read()); t.reset(); //pc.printf("Total Samples %d\r\n",j); } //--------------------------------------------------------------------------------------------------// // Storage // //--------------------------------------------------------------------------------------------------// void store(void const *args) { tid3 = osThreadGetId(); while(true) { osSignalWait(0x1, osWaitForever); GetFileDir(); sprintf(filenameAC,"%sAC.txt",filename); 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"); reset_mod(); } else { for(unsigned long k=0; k<dataLength; ++k) { fprintf(fp, "%d.", ACdata[k]); } fprintf(fp, "EOF"); } fclose(fp); sprintf(filenameDC,"%sDC.txt",filename); sprintf(pathDC,"%sDC.txt",filepath); // pc.printf("\r\nFilename-DC\t%s", pathDC); FILE *fp1 = fopen(pathDC, "w"); if(fp1 == NULL) { //reset_mod(); } else { for(unsigned long k=0; k<dataLength; ++k) { fprintf(fp1, "%d.", DCdata[k]); } fprintf(fp1, "EOF"); } fclose(fp1); sprintf(pathTC,"%sData.txt",hour_dir); //pc.printf("\r\nFilename-TC\t%s", pathTC); FILE *fp2 = fopen(pathTC, "a"); if(fp2 == NULL) { //pc.printf("Could not open file for write\n\r"); //reset_mod(); } else { fprintf(fp2, "\r\n%s",timestamp); for(unsigned long k=0; k<10; ++k) { fprintf(fp2, "\r\nDC-%d",DC_Detect_data[k]); } } fclose(fp2); pc.printf("\n\r storage done \n\r"); osSignalSet(tid6, 0x1); } } //--------------------------------------------------------------------------------------------------// // Sampling Function // //--------------------------------------------------------------------------------------------------// void sampling(void const *args) { //int smplngcnt=0; tid1 = osThreadGetId(); while(true) { pc.printf("AC sampling attempt"); calculate_ADC(); DC_Detection(); for(int i=0;i<3; i++) { Thread::wait(60000); } } } //--------------------------------------------------------------------------------------------------// // GSM Initialization // //--------------------------------------------------------------------------------------------------// void gsm_init() { int cnt = 0; while((sendATcommand("AT+CREG?", "+CREG: 1,1", 5,0) || sendATcommand("AT+CREG?", "+CREG: 1,5", 5,0)) == 0) { if(sendATcommand("AT+CREG=1", "+CREG:1", 5,0)) { pc.printf("creg is 1"); return; } else if(sendATcommand("AT+CREG=1", "+CREG:5", 5,0)) { pc.printf("creg is 5"); return; } ++cnt; if(cnt == 4) { pc.printf("GSM registration failed"); return; } } } //--------------------------------------------------------------------------------------------------// // FTP Connect function // //--------------------------------------------------------------------------------------------------// bool ftp_connect() { int ftp_connect_attempt=0; int ftp_close_attempt=0; if(sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) { } else { while(!sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) { sendATcommand("AT+SAPBR=0,1","OK",5,0); ftp_close_attempt++; if(ftp_close_attempt>3) { return 0; } } } while(sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) { sendATcommand("AT+SAPBR=1,1", "OK", 10,0); ftp_connect_attempt++; if(ftp_connect_attempt>6) { return 0; } } sendATcommand("AT+FTPTYPE=\"I\"", "OK", 5,0); sendATcommand("AT+FTPSERV=\"ftp.panchsheel.biz\"", "OK", 10,0); sendATcommand("AT+FTPUN=\"hardware@panchsheel.biz\"", "OK\r", 10,0); if(sendATcommand("AT+FTPPW=\"DAStest123\"", "OK\r", 10,0)) return 1; else return 0; } //--------------------------------------------------------------------------------------------------// // FTP Send Function // //--------------------------------------------------------------------------------------------------// void ftp_put(bool ftp_type,int a) { char ftpfilename[30]=""; 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++) { if(sendATcommand(command1,result1, 10,0)) { // pc.printf("\n %p",ftpfilename); sendATcommand("AT+FTPPUTPATH=\"/Trial/\"", "OK", 5,0); if(sendATcommand("AT+FTPPUT=1", "OK", 10,0)) { // pc.printf("in loop"); // pc.printf("\n\r %s",ftpfilepath); FILE *fp9 = fopen(ftpfilepath, "r"); if(fp9==NULL)return; else { int trialcount=0; int allowedtrials=5; while(trialcount<allowedtrials) { if(sendATcommand("AT+FTPPUT=2,1000","+FTPPUT:2,1000", 10,0)) { trialcount=0; for(int i=0; i<1000; i++) { gsm.printf("%c", fgetc(fp9)); } pc.printf("file transfered succesfully"); break; } else { // pc.printf("attempt %d failed",trialcount); trialcount++; } } fclose(fp9); } for(int ftp_close_attempt=0; ftp_close_attempt<5; ftp_close_attempt++) { if(sendATcommand("AT+FTPPUT=2,0", "OK", 5,0)) { ftp_close_attempt=6; } } return; //} } return; } } // pc.printf("\n\r %s\n\r","i am not able to send data"); } //-----------------------------------------------------------------------------------------------------------------// // FTP Get //----------------------------------------------------------------------------------------------------------------// void downloadFTP() { Timer t; char incoming_data[100]=""; //int data_size, aux; int x = 0; int attempt=0; int allowed_attempt=4; sendATcommand("AT+FTPGETNAME=\"abc.txt\"", "OK", 5,0); sendATcommand("AT+FTPGETPATH=\"/\"", "OK", 5,0); while(attempt<allowed_attempt) { if (sendATcommand("AT+FTPGET=1", "+FTPGET:1,1",20,0)==1) { 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) { 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; token = strtok(incoming_data, s); token= strtok(NULL,s); //len = atoi(token); //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); fclose(fp1); pc.printf("\r\n Download finished"); break; } } break; } else { attempt++; } } } //--------------------------------------------------------------------------------------------------// // FTP Function Thread // //--------------------------------------------------------------------------------------------------// void FTP_Fun(void const *args) { tid6 = osThreadGetId(); while(true) { osSignalWait(0x1, osWaitForever); if(ftp_connect()) { // 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++; } } } //--------------------------------------------------------------------------------------------------// // Main Function // //--------------------------------------------------------------------------------------------------// int main() { gsm_rst=1; wait(5); gsm.baud(115200); pc.baud(115200); pc.printf("\r\nHello"); sendATcommand("AT+CLTS=1\r", "OK", 5,0); wait(2); sendATcommand("AT+CCLK?\r", "OK", 5,1); //pc.printf("\r\n%s",timestamp); FILE *fp = fopen("/sd/SiteInfo.txt","a"); if(fp == NULL) { FILE *fp = fopen("/sd/SiteInfo.txt","w"); fprintf(fp, "\r\n%s",timestamp); } else { fprintf(fp, "\r\n%s",timestamp); //pc.printf("\r\n%s",timestamp); fclose(fp); } Thread thread1(sampling); Thread thread3(store); Thread thread6(FTP_Fun); while(true) { for (int f=0; f<reset_time; f++)//reset after 60 min { Thread::wait(60000); } //pc.printf("\r\n---MODULE RESET---\r\n"); //reset_mod(); } }