-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
84 lines (68 loc) · 1.63 KB
/
test.c
File metadata and controls
84 lines (68 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h> // Standard input/output definitions
#include <stdlib.h>
#include <string.h> // String function definitions
#include <unistd.h> // para usleep()
#include <getopt.h>
#include <math.h>
#include "arduino-serial-lib.h"
float calculateSD(float data[]);
void error(char* msg)
{
fprintf(stderr, "%s\n",msg);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
int fd = -1,reader;
int baudrate = 9600; // default
char temp='t';
char h='h';
short buffer;
short nbytes=1;
int sumTotalTemp=0,sumToralH=0, promt, promh,cont=0;
fd = serialport_init("/dev/ttyACM0", baudrate);
if( fd==-1 )
{
error("couldn't open port");
return -1;
}
/* Abrimos los dos archivos */
/* Bucle de lectura/escritura */
while(1){
write(fd, &temp, nbytes);
usleep(50000);
reader = read(fd, &buffer, nbytes);
printf("%u\n", buffer);
usleep(500000);
sumTotalTemp=sumTotalTemp+buffer;
write(fd, &h, nbytes);
usleep(50000);
reader = read(fd, &buffer, nbytes);
printf("%u\n", buffer);
sumToralH=sumToralH+buffer;
cont++;
if(cont==12){
break;
}
}
close(fd);
promt=sumTotalTemp/cont;
printf("\nel promedio de temperatura es : %d\n",promt);
promh=sumToralH/cont;
printf("el promedio de humedad es : %d\n",promh);
return 0;
}
/* Ejemplo para calcular desviacion estandar y media */
float calculateSD(float data[])
{
float sum = 0.0, mean, standardDeviation = 0.0;
int i;
for(i = 0; i < 10; ++i)
{
sum += data[i];
}
mean = sum/10;
for(i = 0; i < 10; ++i)
standardDeviation += pow(data[i] - mean, 2);
return sqrt(standardDeviation / 10);
}