Add BME280 humidity sensor test
authorNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 26 Sep 2016 16:14:23 +0000 (18:14 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 26 Sep 2016 16:14:23 +0000 (18:14 +0200)
apps/exanh/sensors/main.c

index 5f57f96..0ec8e1a 100644 (file)
@@ -35,6 +35,7 @@
 #include "extdrv/tmp101_temp_sensor.h"
 #include "extdrv/tsl256x_light_sensor.h"
 #include "extdrv/veml6070_uv_sensor.h"
+#include "extdrv/bme280_humidity_sensor.h"
 
 
 #define MODULE_VERSION    0x01
@@ -62,6 +63,25 @@ const struct pio_config common_pins[] = {
 
 
 
+
+/***************************************************************************** */
+/* Sensor */
+
+/* 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 */
 
@@ -133,9 +153,16 @@ void fault_info(const char* name, uint32_t len)
 /***************************************************************************** */
 int main(void)
 {
+       /* TSL2561 and VEML6070 */
        uint16_t uv = 0, ir = 0;
        uint32_t lux = 0;
+       /* TMP101 */
        int deci_degrees = 0;
+       /* BME280 */
+       uint32_t pressure = 0, temp = 0;
+       uint16_t humidity = 0;
+       int comp_temp = 0;
+
        int ret = 0;
 
        system_init();
@@ -152,6 +179,7 @@ int main(void)
                        uprintf(UART0, "Temp config error: %d\n", ret);
                }
                msleep(10);
+
                /* Configure lux sensor */
                ret = tsl256x_configure(&tsl256x_sensor);
                if (ret != 0) {
@@ -166,13 +194,21 @@ int main(void)
                }
                msleep(10);
 
+               /* Configure barometric + humidity + temp sensor */
+               ret = bme280_configure(&bme280_sensor);
+               if (ret != 0) {
+                       uprintf(UART0, "Humidity config error: %d\n", ret);
+               }
+               msleep(10);
+
+
                while (1) {
                        ret = tmp101_sensor_start_conversion(&tmp101_sensor);
                        if (ret != 0) {
                                uprintf(UART0, "Temp start conversion error: %d\n", ret);
                                break;
                        }
-                       msleep(460); /* Wait for the end of the conversion : 40ms min */
+                       msleep(60); /* Wait for the end of the conversion : 40ms min */
                        ret = tmp101_sensor_read(&tmp101_sensor, NULL, &deci_degrees);
                        if (ret != 0) {
                                uprintf(UART0, "Temp read error: %d\n", ret);
@@ -190,10 +226,27 @@ int main(void)
                                uprintf(UART0, "UV read error: %d\n", ret);
                                break;
                        }
+                       msleep(10);
+
+                       ret = bme280_sensor_read(&bme280_sensor, &pressure, &temp, &humidity);
+                       if (ret != 0) {
+                               uprintf(UART0, "Humidity read error: %d\n", ret);
+                               break;
+                       }
+
+                       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 */
-                       uprintf(UART0, "Lux: %d, IR: %d, UV: %d, Temp: %d,%d\n", lux, ir, uv,
+                       uprintf(UART0, "Lux: %d, IR: %d, UV: %d, Temp: %d,%d degC\n", lux, ir, uv,
                                        (deci_degrees/10), (deci_degrees%10));
+                       uprintf(UART0, "Patm: %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);
+
+                       msleep(400); /* Add some delay */
                }
 
                i2c_off(I2C0);