Exanh sensor data dump
authorNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 26 Apr 2021 22:15:56 +0000 (00:15 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 11 Feb 2023 04:14:22 +0000 (05:14 +0100)
host/exanh_dump/.gitignore [new file with mode: 0644]
host/exanh_dump/Makefile [new file with mode: 0644]
host/exanh_dump/main.c [new file with mode: 0644]
host/exanh_dump/serial_utils.c [new file with mode: 0644]
host/exanh_dump/serial_utils.h [new file with mode: 0644]

diff --git a/host/exanh_dump/.gitignore b/host/exanh_dump/.gitignore
new file mode 100644 (file)
index 0000000..4f5aee7
--- /dev/null
@@ -0,0 +1 @@
+exanh_dump
diff --git a/host/exanh_dump/Makefile b/host/exanh_dump/Makefile
new file mode 100644 (file)
index 0000000..7e69f9e
--- /dev/null
@@ -0,0 +1,32 @@
+#CROSS_COMPILE ?= arm-linux-gnueabihf-
+CC = $(CROSS_COMPILE)gcc
+
+CFLAGS = -Wall -O2 -Wextra
+
+EXEC = exanh_dump
+
+all: $(EXEC)
+
+
+OBJDIR = objs
+SRC = $(shell find . -name \*.c)
+OBJS = ${SRC:%.c=${OBJDIR}/%.o}
+INCLUDES = includes/
+
+${OBJDIR}/%.o: %.c
+       @mkdir -p $(dir $@)
+       @echo "-- compiling" $<
+       @$(CC) -MMD -MP -MF ${OBJDIR}/$*.d $(CFLAGS) $< -c -o $@ -I$(INCLUDES)
+
+$(EXEC): $(OBJS)
+       @echo "Linking $@ ..."
+       @$(CC) $(LDFLAGS) -o $@ $(OBJS)
+       @echo Done.
+
+
+clean:
+       find ${OBJDIR} -name "*.o" -exec rm {} \;
+       find ${OBJDIR} -name "*.d" -exec rm {} \;
+
+mrproper: clean
+       rm -f $(EXEC)
diff --git a/host/exanh_dump/main.c b/host/exanh_dump/main.c
new file mode 100644 (file)
index 0000000..fbd7cbf
--- /dev/null
@@ -0,0 +1,220 @@
+/****************************************************************************
+ *  Get data from sensors and decode
+ *
+ *   main.c
+ *
+ *
+ * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ ****************************************************************************/
+
+
+/* this protocol handler is designed to run on a host replacing the DTPlug,
+ * but should be easy to use as a source for the protocol handling code for
+ * the DTPlug itself.
+ */
+
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <arpa/inet.h>
+#include "serial_utils.h"
+
+
+#define BUF_SIZE  100
+
+#define PROG_NAME  "Sensors polling and decode"
+#define VERSION  "0.1"
+
+
+void help(char *prog_name)
+{
+       fprintf(stderr, "---------------- "PROG_NAME" --------------------------------\n");
+       fprintf(stderr, "Usage: %s [options]\n" \
+               "  Available options:\n" \
+               "  \t -d | --device : Serial device to use for serial communication with the module\n" \
+               "  \t -h | --help : Print this help\n" \
+               "  \t -v | --version : Print programm version\n" \
+               "  All other arguments are data for the command, handled depending on the command.\n", prog_name);
+       fprintf(stderr, "-----------------------------------------------------------------------\n");
+}
+
+char data[150];
+int data_idx = 0;
+
+
+int protocol_decode(char c)
+{
+       if (data_idx == 0) {
+               if (c == '#') {
+                       data[0] = c;
+                       data_idx = 1;
+               }
+               return 0;
+       }
+       if (data_idx > 0) {
+               data[data_idx] = c;
+               if (data_idx == 19) {
+                       data_idx = 0;
+                       return 1;
+               }
+               data_idx++;
+       }
+       return 0;
+}
+
+int main(int argc, char* argv[])
+{
+       /* Serial */
+       char* device = NULL;
+       int slave_fd = 0;
+
+       while(1) {
+               int option_index = 0;
+               int c = 0;
+
+               struct option long_options[] = {
+                       {"device", required_argument, 0, 'd'},
+                       {"help", no_argument, 0, 'h'},
+                       {"version", no_argument, 0, 'v'},
+                       {0, 0, 0, 0}
+               };
+
+               c = getopt_long(argc, argv, "d:hv", long_options, &option_index);
+
+               /* no more options to parse */
+               if (c == -1) break;
+               switch (c) {
+
+                       /* d, device */
+                       case 'd':
+                               device = optarg;
+                               break;
+
+                       /* v, version */
+                       case 'v':
+                               printf("%s Version %s\n", PROG_NAME, VERSION);
+                               return 0;
+                               break;
+
+                       /* h, help */
+                       case 'h':
+                       default:
+                               help(argv[0]);
+                               return 0;
+               }
+       }
+
+
+       /* Need Serial port as parameter */
+       if (device == NULL) {
+               printf("Error, need serial (tty) device\n");
+               help(argv[0]);
+               return -1;
+       }
+
+       /* Open tty */
+       slave_fd = serial_setup(device);
+       if (slave_fd < 0) {
+               printf("Unable to open specified serial port %s\n", device);
+               return -2;
+       }
+
+       /* ************************************************* */
+       /* And never stop handling data ! */
+       while (1) {
+               int len = 0, ret = 0;
+               struct timeval now;
+               char buf[BUF_SIZE];
+               int idx = 0;
+
+               /* Send periodic requests for temperature */
+               gettimeofday(&now, NULL);
+
+               memset(buf, 0, BUF_SIZE);
+               /* Get serial data and try to build a packet */
+               len = read(slave_fd, buf, BUF_SIZE);
+               if (len < 0) {
+                       if (len == 0) {
+                               printf("\nError, got activity on serial link, but no data ... End of file.\n");
+                       } else {
+                               perror("serial read error");
+                       }
+                       close(slave_fd);
+                       idx = 0;
+                       len = 0;
+                       do {
+                               slave_fd = serial_setup(device);
+                               usleep(10 * 1000);
+                       } while (slave_fd < 0);
+               } else {
+                       write(2, buf, len);
+               }
+               while (idx < len) {
+                       ret = protocol_decode(buf[idx]);
+                       /* Check return code to know if we have a valid packet */
+                       if (ret == 1) {
+                               /* Valid packet received, parse data */
+                               char addr;
+                               int got_tsl, got_veml, got_bme;
+                               uint16_t raw_humidity;
+                               uint16_t lux, ir, uv;
+                               uint16_t pressure, comp_temp, humidity; /* From BME */
+                               uint16_t* vars = (uint16_t*)data;
+
+                               addr = data[1] & 0x1F;
+                               raw_humidity = (uint16_t)htons(vars[1]);
+                               lux = (uint16_t)htons(vars[2]);
+                               ir = (uint16_t)htons(vars[3]);
+                               uv = (uint16_t)htons(vars[4]);
+                               pressure = (uint16_t)htons(vars[5]);
+                               comp_temp = (uint16_t)htons(vars[6]);
+                               humidity = (uint16_t)htons(vars[7]);
+                                       
+                               /* Display data */
+                               printf("\e[H"); /* Goto terminal home (top left) */
+                               printf("\e[KSensor %d\n", addr);
+                               printf("\e[K\tSoil: %d\n", raw_humidity);
+                               printf("\e[K\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
+                               printf("\e[K\tPatm: %d hPa, Temp: %d,%02d degC, Humidity: %d,%d rH\n\n",
+                                               pressure,
+                                               comp_temp / 10,  (comp_temp > 0) ? (comp_temp % 10) : ((-comp_temp) % 10),
+                                               humidity / 10, humidity % 10);
+                               printf("\e[K\n");
+                       }
+
+                       idx++;
+               }
+
+       } /* End of infinite loop */
+
+       close(slave_fd);
+       return 0;
+}
+
+
diff --git a/host/exanh_dump/serial_utils.c b/host/exanh_dump/serial_utils.c
new file mode 100644 (file)
index 0000000..5e17e42
--- /dev/null
@@ -0,0 +1,59 @@
+/*********************************************************************
+ *
+ *   Serial utility functions
+ *
+ *
+ * Copyright 2012-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *********************************************************************/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+
+#define SERIAL_BAUD  B115200
+
+
+int serial_setup(char* name)
+{
+       struct termios tio;
+       int fd = -1;
+
+       /* Open serial port */
+       fd = open(name, O_RDWR);
+       if (fd < 0) {
+               perror("Unable to open communication with companion chip");
+               return -1;
+       }
+       /* Setup serial port */
+       memset(&tio, 0, sizeof(tio));
+       tio.c_cflag = CS8 | CREAD | CLOCAL;     /* 8n1, see termios.h for more information */
+       tio.c_cc[VMIN] = 1;
+       tio.c_cc[VTIME] = 5;
+       cfsetospeed(&tio, SERIAL_BAUD);
+       cfsetispeed(&tio, SERIAL_BAUD);
+       tcsetattr(fd, TCSANOW, &tio);
+
+       return fd;
+}
+
diff --git a/host/exanh_dump/serial_utils.h b/host/exanh_dump/serial_utils.h
new file mode 100644 (file)
index 0000000..7c17bcf
--- /dev/null
@@ -0,0 +1,32 @@
+/*********************************************************************
+ *
+ *   Serial utility functions
+ *
+ *
+ * Copyright 2012 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *********************************************************************/
+#ifndef SERIAL_UTILS_H
+#define SERIAL_UTILS_H
+
+/* Setup serial comunication, using name if given or saved name if name is NULL
+ *  SERIAL_BAUD  B38400
+ *  c_cflag  (CS7 | PARENB | CREAD | CLOCAL) (7e1)
+ */
+int serial_setup(char* name);
+
+#endif /* SERIAL_UTILS_H */
+