diff --git a/daq.c b/daq.c new file mode 100644 index 0000000..fdc0654 --- /dev/null +++ b/daq.c @@ -0,0 +1,152 @@ +/* +*/ + +#include +#include +#include +#include +#include +#include +#include"uart.c" + +#define data_file_path "/var/lib/cloud9/c9projets/newUART/data.dat" + +#define GC0017 4 //co2 +#define CM31911 1 //co +#define OX0052 2 //o2 + +#define Rep_Dev_ID "Y\r\n" +#define Pulling_Mode "K 2\r\n" +#define OX_P_Mode "K 1\r\n" +#define unf_gas_con "z\r\n" +#define fil_gas_con "Z\r\n" +#define Temperature "T\r\n" +#define get_readigns "Q\r\n" +#define percent_oxigen "%\r\n" + + +int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[]); +int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess); + +int str2int(unsigned char *ptr); + +int DAQ(int t_hrs, int tm_s); + + +int main(){ + + //configuración de sensores + printf("Configuring CO2 sensor\n"); + sensConf(GC0017, B9600, Pulling_Mode, " K 00002\r\n"); + printf("Configuring CO sensor\n"); + sensConf(CM31911, B9600, Pulling_Mode, "K 00002\r\n"); + //printf("Configuring O2 sensor\n"); + //sensConf(OX0052, B9600, OX_P_Mode, "??"); + + + //adquisición + printf("Starting data acquisition\n"); + DAQ(1, 3); + sleep(1); + + //finalización + //deinit uart + uartClose(GC0017); + uartClose(CM31911); + uartClose(OX0052); + + printf("Exiting of the program...\n"); + return 0; +} + + +int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[]){ + + int count; + int i = 0; + + uartConf(uartNumber, baudRate); + + while( i < 10 ){ + uartTransmit(uartNumber, mode); + tcdrain(uartFile[uartNumber]); //wait all data has been sent + printf("Command sended.\n"); + //usleep(100000); //give the sensor a chance to respond + count = uartReceive(uartNumber); + if (count == 0) printf("There was no data available to read!\n"); + else if (strcmp(receive[uartNumber], response) == 0) { + printf("Sensor configurated after %d tries.\n", i+1); + return 0; + } else { + printf("The following was read in [%d]: %s\n",count,receive[uartNumber]); + char *c = receive[uartNumber]; + while (*c != '\0'){ + printf("%d = '%c'\n",*c, *c); + c++; + } + } + i ++; + } + printf("CO2 Sensor configuration failed.\n"); + return -1; +} + +int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess){ + int measure; + uartTransmit(uartNumber, command); + uartReceive(uartNumber); + measure = (str2int(&receive[uartNumber]) - excess)*multiplier ; + return measure; +} + +int str2int(unsigned char *ptr){ + + int number = 0; + + while (*ptr != '\0') { + if ((*ptr >= '0') && (*ptr <= '9')) { + int dig = (*ptr) - '0'; + number = number*10 + dig; + } + ptr++; + } + return number; +} + +int DAQ(int t_hrs, int tm_s) +{ + FILE* dfp; // create a file pointer dfp + int co2, co, o2, temp; + clock_t start_t, end_t; + time_t new_time, prev_time, t0; + double dif; + + + //registro datos de inicio + dfp = fopen(data_file_path, "w"); // open file for writing + time_t curtime; + time(&curtime); + fprintf(dfp, "%sStarting DAQ\ntime(s)\t\tCO2(ppm)\t\tCO(ppm)\t\tO2(ppm)\t\tTemperaure(ºC*10)\n", ctime(&curtime)); // send the value to the file + fclose(dfp); // close the file using the file pointer + + //ciclo + t0 = (prev_time=time(NULL)); + for(int i = 0; i < (t_hrs*3600); i=i+tm_s){ + while((prev_time+tm_s) != (new_time = time(NULL))); + start_t = clock(); + co2 = measure(GC0017, fil_gas_con, 10, 0); co = measure(CM31911, fil_gas_con, 1, 0); + /*o2 = measure(OX0052, percent_oxigen, 1, 0);*/temp = measure(CM31911, Temperature, 1, 1000); + dfp = fopen(data_file_path, "a"); // open file for writing + fprintf(dfp, "%ld \t\t%d\t\t%d\t\t%d\t\t%d\n", new_time-t0, co2, co, o2, temp); // send the value to the file + fclose(dfp); // close the file using the file pointer + prev_time = new_time; + end_t = clock(); + dif = (double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC; + dif = tm_s*1e6 - dif - .15e6; + if (dif < 0) dif = 0; + usleep((int)dif); + } + + return 0; +} +