resolve merge conflict
[wakeup.git] / src / read_serial.cpp
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <iostream>
7 #include <date.h>
8
9 int main()
10 {
11    int fd;
12    int bytes;
13    char buf[500];
14
15    fd = open("/dev/ttyACM0", O_RDONLY);
16    if (fd < 0)
17    {
18       std::cerr << "Can not open /dev/ttyACM0.\n";
19       return -1;
20    }
21
22    bytes = 0;
23    while (bytes >= 0)
24    {
25       UTC timestamp;
26
27       bytes = read(fd, buf, 500);
28       if (bytes > 0)
29       {
30          //  Read a sample value from the AD converter
31          //  Voltage = 4.84 V * sample / 1023     ( Reference voltage = 4.84 )
32          // LDR resistance = Voltage * 10000 Ohm /(4.84V - Voltage)
33          buf[bytes] = '\0';
34          if (buf[0] >= '0' && buf[0] <= '9')
35          {
36             timestamp = Now();
37             std::cout << timestamp << "," << buf ;
38             std::cout.flush();
39          }
40       }
41       sleep(20);
42    }
43 }