1 /****************************************************************************
2 * Get data from sensors and decode
7 * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ****************************************************************************/
26 /* this protocol handler is designed to run on a host replacing the DTPlug,
27 * but should be easy to use as a source for the protocol handling code for
41 #include <sys/types.h>
43 #include <sys/select.h>
45 #include <arpa/inet.h>
46 #include "serial_utils.h"
51 #define PROG_NAME "Sensors polling and decode"
55 void help(char *prog_name)
57 fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n");
58 fprintf(stderr, "Usage: %s [options]\n" \
59 " Available options:\n" \
60 " \t -d | --device : Serial device to use for serial communication with the module\n" \
61 " \t -h | --help : Print this help\n" \
62 " \t -v | --version : Print programm version\n" \
63 " All other arguments are data for the command, handled depending on the command.\n", prog_name);
64 fprintf(stderr, "-----------------------------------------------------------------------\n");
71 int protocol_decode(char c)
91 int main(int argc, char* argv[])
101 struct option long_options[] = {
102 {"device", required_argument, 0, 'd'},
103 {"help", no_argument, 0, 'h'},
104 {"version", no_argument, 0, 'v'},
108 c = getopt_long(argc, argv, "d:hv", long_options, &option_index);
110 /* no more options to parse */
121 printf("%s Version %s\n", PROG_NAME, VERSION);
134 /* Need Serial port as parameter */
135 if (device == NULL) {
136 printf("Error, need serial (tty) device\n");
142 slave_fd = serial_setup(device);
144 printf("Unable to open specified serial port %s\n", device);
148 /* ************************************************* */
149 /* And never stop handling data ! */
151 int len = 0, ret = 0;
156 /* Send periodic requests for temperature */
157 gettimeofday(&now, NULL);
159 memset(buf, 0, BUF_SIZE);
160 /* Get serial data and try to build a packet */
161 len = read(slave_fd, buf, BUF_SIZE);
164 printf("\nError, got activity on serial link, but no data ... End of file.\n");
166 perror("serial read error");
172 slave_fd = serial_setup(device);
174 } while (slave_fd < 0);
179 ret = protocol_decode(buf[idx]);
180 /* Check return code to know if we have a valid packet */
182 /* Valid packet received, parse data */
184 int got_tsl, got_veml, got_bme;
185 uint16_t raw_humidity;
186 uint16_t lux, ir, uv;
187 uint16_t pressure, comp_temp, humidity; /* From BME */
188 uint16_t* vars = (uint16_t*)data;
190 addr = data[1] & 0x1F;
191 raw_humidity = (uint16_t)htons(vars[1]);
192 lux = (uint16_t)htons(vars[2]);
193 ir = (uint16_t)htons(vars[3]);
194 uv = (uint16_t)htons(vars[4]);
195 pressure = (uint16_t)htons(vars[5]);
196 comp_temp = (uint16_t)htons(vars[6]);
197 humidity = (uint16_t)htons(vars[7]);
200 printf("
\e[H"); /* Goto terminal home (top left) */
201 printf("
\e[KSensor %d\n", addr);
202 printf("
\e[K\tSoil: %d\n", raw_humidity);
203 printf("
\e[K\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
204 printf("
\e[K\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
206 comp_temp / 10, (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
207 humidity / 10, humidity % 10);
214 } /* End of infinite loop */