Adding application for Exanh water level sensors (bord v04)
authorNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 13 May 2017 18:54:18 +0000 (20:54 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Sat, 11 Feb 2023 04:09:54 +0000 (05:09 +0100)
v04/Makefile [new file with mode: 0644]
v04/README [new file with mode: 0644]
v04/main.c [new file with mode: 0644]

diff --git a/v04/Makefile b/v04/Makefile
new file mode 100644 (file)
index 0000000..a944f0f
--- /dev/null
@@ -0,0 +1,12 @@
+# Makefile for exanh apps
+
+MODULE = $(shell basename $(shell cd .. && pwd && cd -))
+NAME = $(shell basename $(CURDIR))
+
+.PHONY: $(NAME).bin
+$(NAME).bin:
+       @make -C ../../.. --no-print-directory NAME=$(NAME) MODULE=$(MODULE) apps/$(MODULE)/$(NAME)/$@
+
+clean mrproper:
+       @make -C ../../.. --no-print-directory $@
+
diff --git a/v04/README b/v04/README
new file mode 100644 (file)
index 0000000..6ce3aeb
--- /dev/null
@@ -0,0 +1,77 @@
+Exanh Gardener soil moisture sensor support.
+
+Copyright 2017 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 2 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 code handles the v0.2 and v0.3 versions of the Exanh Gardener soil
+moisture sensor.
+
+The code also provides support of all three sensors found on exanh sensor board
+in version 0.2 :
+ - BME280 I2C temperature, humidity and pressure sensor
+ - TSL256x I2C luminosity and IR sensor
+ - VEML6070 I2C UV sensor
+
+The default behavior for the sensor does nothing (should enter sleep, but not
+implemented yet).
+When the sensor is not configured (no address set), then all data received on
+serial is silently dropped.
+Pressing the "Mode" button on the sensor for more than 4 seconds sends a "c"
+request on the serial line, changes address to 0 (nul), and enters the
+"address requested" state. The host should then send a message to the sensor on
+it's temporary address, containing it's new address. The message is three bytes
+long and it's format is :
+  #     0       addr
+ 0x23  0x00  (0x01 to 0x1F)
+Addresses are between 1 and 31, allowing up to 30 sensors on one link.
+
+Once the address is set, the host can request samples data by sending a message
+addressed to the sensor.
+The message is three or more bytes long (depending on the type) and it's
+format is :
+  #         addr           type          [other data]
+ 0x23  (0x01 to 0x1F)  (0x00 to 0xFF)    [....]
+Valid types are :
+ - 'a' : all - request to send all available sensor data to host.
+ - 'l' : set led color - set the sensor led color. Three bytes of data (RGB).
+
+It is also possible to broadcast a message to all sensors by sending to address
+255 (0xFF)
+
+
+To set the sensor address using a serial line from a linux host, you can use
+these commands (sensor connected to ttyUSB0) :
+
+# For device address 1 (0x01) :
+echo -ne "#\x00\x01" > /dev/ttyUSB0
+
+# For device address 2 (0x02) : 
+echo -ne "#\x00\x02" > /dev/ttyUSB0
+
+# and so on ...
+[...]
+
+# To check what these command send if you are not sure :
+echo -ne "#\x00\x01" | hexdump -C
+
+
+When debug is set at compile time, then debug data is sent on UART0
+
+
diff --git a/v04/main.c b/v04/main.c
new file mode 100644 (file)
index 0000000..e63189c
--- /dev/null
@@ -0,0 +1,504 @@
+/****************************************************************************
+ *   sensors/main.c
+ *
+ * Exanh Gardener soil moisture sensor support.
+ *
+ * Copyright 2016-2017 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 2 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 "lib/stdint.h"
+#include "core/system.h"
+#include "core/systick.h"
+#include "core/watchdog.h"
+#include "core/pio.h"
+#include "core/iap.h"
+#include "lib/stdio.h"
+#include "drivers/i2c.h"
+#include "drivers/adc.h"
+#include "drivers/serial.h"
+#include "drivers/gpio.h"
+#include "drivers/timers.h"
+
+#include "extdrv/ws2812.h"
+#include "extdrv/tsl256x_light_sensor.h"
+#include "extdrv/veml6070_uv_sensor.h"
+#include "extdrv/bme280_humidity_sensor.h"
+
+
+#define MODULE_VERSION   0x04
+#define MODULE_NAME "E-Xanh Gardener"
+
+#define SELECTED_FREQ  FREQ_SEL_36MHz
+
+
+#define DEBUG 0
+#if (DEBUG == 1)
+ #define debug_printf(...) \
+       gpio_clear(tx_en); \
+       uprintf(__VA_ARGS__); \
+       gpio_set(tx_en);
+
+ #define debug(cond, ...) \
+    if (cond) { \
+               gpio_clear(tx_en); \
+               uprintf(UART0, __VA_ARGS__ ); \
+               gpio_set(tx_en); \
+       }
+
+#else
+ #define debug_printf(...) ;
+ #define debug(...) ;
+#endif
+
+/***************************************************************************** */
+/* Pins configuration */
+/* Pins blocks are passed to set_pins() for pins configuration.
+ * Unused pin blocks can be removed safely with the corresponding set_pins() call
+ * All pins blocks may be safelly merged in a single block for single set_pins() call..
+ */
+const struct pio_config common_pins[] = {
+       /* UART 0 */
+       { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
+       { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
+       /* I2C 0 */
+       { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
+       { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
+       /* ADC */
+       { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
+       /* Timers */
+       { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
+       /* GPIO */
+       { LPC_GPIO_0_2, LPC_GPIO, 0 },  /* Led */
+    { LPC_GPIO_0_3, LPC_GPIO, 0 },  /* Tx enable */
+       ARRAY_LAST_PIO,
+};
+
+/* Configure pins as used for capacitance sensing */
+const struct pio_config capacitance_pins[] = {
+       { LPC_GPIO_0_9, LPC_SCT_POUT0, 0 },
+       { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
+       ARRAY_LAST_PIO,
+};
+
+
+const struct pio button = LPC_GPIO_0_13; /* Mode button */
+const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
+const struct pio tx_en = LPC_GPIO_0_3; /* Enable comminication output */
+
+const struct pio cap_trig_gpio = LPC_GPIO_0_9;
+const struct pio cap_adc_gpio = LPC_GPIO_0_17;
+
+
+/***************************************************************************** */
+/* External Sensors */
+
+/* Note : 8bits address */
+#define BME280_ADDR   0xEC
+struct bme280_sensor_config bme280_sensor = {
+       .bus_num = I2C0,
+       .addr = BME280_ADDR,
+       .humidity_oversampling = BME280_OS_x16,
+       .temp_oversampling = BME280_OS_x16,
+       .pressure_oversampling = BME280_OS_x16,
+       .mode = BME280_NORMAL,
+       .standby_len = BME280_SB_62ms,
+       .filter_coeff = BME280_FILT_OFF,
+};
+
+
+
+/***************************************************************************** */
+/* Luminosity */
+
+/* Note : These are 8bits address */
+#define TSL256x_ADDR   0x52 /* Pin Addr Sel (pin2 of tsl256x) connected to GND */
+struct tsl256x_sensor_config tsl256x_sensor = {
+       .bus_num = I2C0,
+       .addr = TSL256x_ADDR,
+       .gain = TSL256x_LOW_GAIN,
+       .integration_time = TSL256x_INTEGRATION_100ms,
+       .package = TSL256x_PACKAGE_T,
+};
+
+
+
+
+/***************************************************************************** */
+/* UV */
+
+/* The I2C UV light sensor is at addresses 0x70, 0x71 and 0x73 */
+/* Note : These are 8bits address */
+#define VEML6070_ADDR   0x70
+struct veml6070_sensor_config veml6070_sensor = {
+       .bus_num = I2C0,
+       .addr = VEML6070_ADDR,
+};
+
+
+
+/***************************************************************************** */
+/* Soil humidity sensor  */
+
+uint16_t raw_humidity = 0;
+void soil_humidity_sample(void)
+{
+       /* Get ADC values */
+       adc_get_value(&raw_humidity, LPC_ADC(9));
+}
+
+
+/* Set led */
+void set_led(uint8_t red, uint8_t green, uint8_t blue)
+{
+       ws2812_set_pixel(0, red, green, blue);
+       ws2812_send_frame(0);
+}
+
+
+
+/***************************************************************************** */
+void system_init()
+{
+       system_set_default_power_state();
+       clock_config(SELECTED_FREQ);
+       set_pins(common_pins);
+       gpio_on();
+       /* System tick timer MUST be configured and running in order to use the sleeping
+        * functions */
+       systick_timer_on(1); /* 1ms */
+       systick_start();
+}
+
+/* Define our fault handler. This one is not mandatory, the dummy fault handler
+ * will be used when it's not overridden here.
+ * Note : The default one does a simple infinite loop. If the watchdog is deactivated
+ * the system will hang.
+ */
+void fault_info(const char* name, uint32_t len)
+{
+       debug_printf(UART0, name);
+       while (1);
+}
+
+const struct lpc_timer_pwm_config pwm_conf = {
+       .nb_channels = 1,
+       .period = 30,
+       .outputs_initial_state = 0x01,
+       .match_values = { 10, },
+       .outputs = { 0, },
+};
+
+
+enum states {
+       IDLE = 0,
+       MSG_START,
+       ADDRESSSED,
+       NEED_DATA,
+};
+#define MSG_FIRST_BYTE  '#'
+volatile uint8_t send_data = 0;
+volatile uint8_t req_cmd = 0;
+volatile uint8_t req_data_len = 0;
+#define DATA_LEN_MAX 8
+volatile uint8_t req_data[DATA_LEN_MAX];
+
+volatile uint8_t address = 0;
+volatile uint8_t store_address = 0;
+volatile uint8_t addr_req = 0;
+
+/* serial_req */
+void serial_req(uint8_t c)
+{
+       static int state = IDLE;
+       static uint8_t req_need_data_len = 0;
+       static uint8_t tmp_req_cmd = 0;
+       switch (state) {
+               case IDLE:
+                       if (c == MSG_FIRST_BYTE) {
+                               state = MSG_START;
+                       }
+                       break;
+
+               case MSG_START:
+                       if ((c == address) || (c == 0xFF)) {
+                               state = ADDRESSSED;
+                       } else {
+                               state = IDLE;
+                       }
+                       break;
+
+               case ADDRESSSED:
+                       if ((address == 0) & (addr_req == 1)) {
+                               /* We received our address, store it */
+                               address = c;
+                               store_address = 1;
+                               state = IDLE;
+                               break;
+                       }
+                       switch (c) {
+                               case 'a':
+                                       send_data = 1;
+                                       state = IDLE;
+                                       break;
+                               case 'l':
+                                       req_need_data_len = 3;
+                                       tmp_req_cmd = 'l';
+                                       state = NEED_DATA;
+                                       break;
+                       }
+                       break;
+
+               case NEED_DATA:
+                       req_data[req_data_len++] = c;
+                       if (req_data_len >= req_need_data_len) {
+                               req_need_data_len = 0;
+                               state = IDLE;
+                               req_cmd = tmp_req_cmd;
+                               tmp_req_cmd = 0;
+                       }
+                       break;
+
+               default:
+                       state = IDLE;
+       }
+}
+
+/* Should check button "hold" duration and act accordingly
+ * When pressed with no addresss : should request address from host
+ */
+volatile uint8_t need_config = 0;
+void button_request(uint32_t gpio)
+{
+       static uint32_t tick_press_date = 0;
+       int delta;
+       if (gpio_read(button) == 0) {
+               tick_press_date = systick_get_tick_count();
+               return;
+       }
+       delta = systick_get_tick_count() - tick_press_date;
+       if (delta < 0) {
+               return; /* Timer wrapped while reading button hold duration ... let user press again. */
+       }
+       if (address == 0) {
+               need_config = 1;
+               return;
+       }
+       if (delta < 2000) {
+               /* Send a sample .... */
+               /* This should enter the "menu" loop */
+               send_data = 1;
+       } else {
+               address = 0;
+               need_config = 1;
+       }
+}
+/***************************************************************************** */
+int main(void)
+{
+       uint8_t got_tsl = 1, got_veml = 1, got_bme = 1;
+       /* TSL2561 and VEML6070 */
+       uint16_t uv = 0, ir = 0;
+       uint32_t lux = 0;
+       /* BME280 */
+       uint32_t pressure = 0, temp = 0;
+       uint16_t humidity = 0;
+       int comp_temp = 0;
+       int ret = 0;
+
+       system_init();
+
+       /* Communication */
+       uart_on(UART0, 115200, serial_req);
+       config_gpio(&tx_en, 0, GPIO_DIR_OUT, 1);
+
+       /* ADC */
+       config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
+       config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
+       adc_on(NULL);
+       adc_start_burst_conversion(ADC_MCH(9), LPC_ADC_SEQA);
+       debug_printf(UART0, "ADC config done\n");
+
+       /* Led strip configuration */
+       ws2812_config(&ws2812_data_out_pin);
+       debug_printf(UART0, "Led config done\n");
+
+       /* Timer config */
+       timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
+       timer_pwm_config(LPC_SCT, &pwm_conf);
+       debug_printf(UART0, "Timer config done\n");
+
+       /* Start sampling */
+       set_pins(capacitance_pins);
+       timer_start(LPC_SCT);
+
+       /* Register callback on button */
+       set_gpio_callback(button_request, &button, EDGES_BOTH);
+
+       i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
+
+       msleep(10);
+
+       /* Configure lux sensor */
+       ret = tsl256x_configure(&tsl256x_sensor);
+       if (ret != 0) {
+               got_tsl = 0;
+               debug_printf(UART0, "Lux config error: %d\n", ret);
+       }
+       msleep(10);
+
+       /* Configure uv sensor */
+       ret = veml6070_configure(&veml6070_sensor);
+       if (ret != 0) {
+               got_veml = 0;
+               debug_printf(UART0, "UV config error: %d\n", ret);
+       }
+       msleep(10);
+
+       /* Configure barometric + humidity + temp sensor */
+       ret = bme280_configure(&bme280_sensor);
+       if (ret != 0) {
+               got_bme = 0;
+               debug_printf(UART0, "Humidity config error: %d\n", ret);
+       }
+       msleep(10);
+
+       /* Read our address from Flash */
+       memcpy((void*)&address, (void*)(0x00003FC0), 1);
+       if (address == 0) {
+               /* No sensor should have adrress 0, which is reserved for the "waiting for new address" state */
+               address = 255;
+       }
+       debug_printf(UART0, "Address: %d\n", address);
+
+       send_data = 0;
+       store_address = 0;
+       req_cmd = 0;
+       need_config = 0;
+
+       while (1) {
+
+               /* Enter sleep / low-power ? */
+
+               /* Leave some time for the others */            
+               msleep(50); /* Add some delay */
+
+               if (store_address == 1) {
+                       char buf[64] = "";
+                       store_address = 0;
+
+                       ret = iap_prepare_flash(15, 15);  /* Prepare sector 15 (16K flash size) for erase */
+                       debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
+
+                       ret = iap_erase_flash_pages(255, 255); /* Erase the last page of the Flash */
+                       debug((ret != IAP_STATUS_CMD_SUCCESS), "Error erasing page 255: %d\n", ret);
+
+                       ret = iap_prepare_flash(15, 15);  /* Prepare sector 15 (16K flash size) for write */
+                       debug((ret != IAP_STATUS_CMD_SUCCESS), "Error preparing flash: %d\n", ret);
+
+                       buf[0] = address;
+                       ret = iap_copy_ram_to_flash(0x00003FC0, (uint32_t)(&buf), 64);
+                       debug((ret != IAP_STATUS_CMD_SUCCESS), "Error writting our number: %d\n", ret);
+
+                       debug(1, "Changed our address to %d\n", address);
+               }
+
+               if (req_cmd != 0) {
+                       switch (req_cmd) {
+                               case 'l':
+                                       set_led(req_data[0], req_data[1], req_data[2]);
+                                       debug(1, "Set led to %d %d %d\n", req_data[0], req_data[1], req_data[2]); 
+                                       break;
+
+                               default:
+                                       debug(1, "Unhandled command 0x%02x (%c)\n", req_cmd, req_cmd);
+                       }
+                       req_cmd = 0;
+                       req_data_len = 0;
+               }
+
+               /* Get hold of Tx line */
+               if (need_config == 1) {
+                       /* Request a new address from host */
+                       gpio_clear(tx_en);
+                       if (serial_send_quickbyte(UART0, 'c') == 0) {
+                               addr_req = 1;
+                               need_config = 0;
+                       }
+                       gpio_set(tx_en);
+               }
+
+               if (send_data == 1) {
+                       send_data = 0;
+                       /* Take humidity sample */
+                       soil_humidity_sample();
+
+                       if (got_tsl == 1) {
+                               ret = tsl256x_sensor_read(&tsl256x_sensor, NULL, &ir, &lux);
+                               debug((ret != 0), "Lux read error: %d\n", ret);
+                               msleep(2);
+                       }
+                       if (got_veml == 1) {
+                               ret = veml6070_sensor_read(&veml6070_sensor, &uv);
+                               debug((ret != 0), "UV read error: %d\n", ret);
+                               msleep(2);
+                       }
+                       if (got_bme == 1) {
+                               ret = bme280_sensor_read(&bme280_sensor, &pressure, &temp, &humidity);
+                               debug((ret != 0), "Humidity read error: %d\n", ret);
+
+                               comp_temp = bme280_compensate_temperature(&bme280_sensor, temp) / 10;
+                               pressure = bme280_compensate_pressure(&bme280_sensor, pressure) / 100;
+                               humidity = bme280_compensate_humidity(&bme280_sensor, humidity) / 10;
+                       }
+
+                       /* Display all */
+                       debug(1, "Sensor %d:\n\tSoil: %d\n", address, raw_humidity);
+                       debug(1, "\tLux: %d, IR: %d, UV: %d\n", lux, ir, uv);
+                       debug(1, "\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);
+       
+                       set_led(0, 0, ((raw_humidity >> 6) & 0xFF));
+       
+                       /* Send for control */
+                       if (1) {
+                               char buff[20];
+                               uint16_t* data = (uint16_t*)buff;
+
+                               buff[0] = '#';
+                               buff[1] = address | (got_tsl << 5) | (got_veml << 6) | (got_bme << 7);
+                               data[1] = (uint16_t)raw_humidity;
+                               data[2] = (uint16_t)lux;
+                               data[3] = (uint16_t)ir;
+                               data[4] = (uint16_t)uv;
+                               data[5] = (uint16_t)pressure;
+                               data[6] = (uint16_t)comp_temp;
+                               data[7] = (uint16_t)humidity;
+
+                               gpio_clear(tx_en);
+                               serial_write(UART0, buff, 20);
+                               gpio_set(tx_en);
+                       }
+               }
+       }
+
+       return 0;
+}
+