1 /****************************************************************************
2 * Get data from sensors and decode
6 * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ****************************************************************************/
33 #include <sys/types.h>
35 #include <sys/select.h>
37 #include <arpa/inet.h>
38 #include "serial_utils.h"
43 #define PROG_NAME "Sensors polling and decode"
47 void help(char *prog_name)
49 fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n");
50 fprintf(stderr, "Usage: %s [options]\n" \
51 " Available options:\n" \
52 " \t -d | --device : Serial device to use for serial communication with the module\n" \
53 " \t -h | --help : Print this help\n" \
54 " \t -v | --version : Print programm version\n" \
55 " All other arguments are data for the command, handled depending on the command.\n", prog_name);
56 fprintf(stderr, "-----------------------------------------------------------------------\n");
63 int protocol_decode(char c)
83 int main(int argc, char* argv[])
93 struct option long_options[] = {
94 {"device", required_argument, 0, 'd'},
95 {"help", no_argument, 0, 'h'},
96 {"version", no_argument, 0, 'v'},
100 c = getopt_long(argc, argv, "d:hv", long_options, &option_index);
102 /* no more options to parse */
113 printf("%s Version %s\n", PROG_NAME, VERSION);
126 /* Need Serial port as parameter */
127 if (device == NULL) {
128 printf("Error, need serial (tty) device\n");
134 slave_fd = serial_setup(device);
136 printf("Unable to open specified serial port %s\n", device);
140 /* ************************************************* */
141 /* And never stop handling data ! */
143 int len = 0, ret = 0;
148 /* Send periodic requests for temperature */
149 gettimeofday(&now, NULL);
151 memset(buf, 0, BUF_SIZE);
152 /* Get serial data and try to build a packet */
153 len = read(slave_fd, buf, BUF_SIZE);
156 printf("\nError, got activity on serial link, but no data ... End of file.\n");
158 perror("serial read error");
164 slave_fd = serial_setup(device);
166 } while (slave_fd < 0);
171 ret = protocol_decode(buf[idx]);
172 /* Check return code to know if we have a valid packet */
174 /* Valid packet received, parse data */
176 int got_tsl, got_veml, got_bme;
177 uint16_t raw_humidity;
178 uint16_t lux, ir, uv;
179 uint16_t pressure, comp_temp, humidity; /* From BME */
180 uint16_t* vars = (uint16_t*)data;
182 addr = data[1] & 0x1F;
183 raw_humidity = (uint16_t)htons(vars[1]);
184 lux = (uint16_t)htons(vars[2]);
185 ir = (uint16_t)htons(vars[3]);
186 uv = (uint16_t)htons(vars[4]);
187 pressure = (uint16_t)htons(vars[5]);
188 comp_temp = (uint16_t)htons(vars[6]);
189 humidity = (uint16_t)htons(vars[7]);
192 printf("
\e[H"); /* Goto terminal home (top left) */
193 printf("
\e[KSensor %d\n", addr);
194 printf("
\e[K\tSoil: %d\n", raw_humidity);
195 printf("
\e[K\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
196 printf("
\e[K\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
198 comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
199 humidity / 10, humidity % 10);
206 } /* End of infinite loop */