samples: sensor: add sample for SHT3XD sensor

Added sample code for Sensirion SHT3XD humidity sensor.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2018-11-20 13:52:37 -06:00 committed by Anas Nashif
commit 6e29416fff
9 changed files with 244 additions and 0 deletions

View file

@ -0,0 +1,12 @@
#
# Copyright (c) 2018 Peter Bigot Consulting, LLC
#
# SPDX-License-Identifier: Apache-2.0
#
cmake_minimum_required(VERSION 3.8.2)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(sht3xd)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,52 @@
.. _sht3xd:
SHT3XD: High accuracy digital I2C humidity sensor
#################################################
Description
***********
This sample application periodically (2 Hz) measures the ambient
temperature and humidity. The result is written to the console.
Optionally, it also shows how to use the upper threshold triggers.
References
**********
- `SHT3X-DIS sensor <https://www.sensirion.com/en/environmental-sensors/humidity-sensors/digital-humidity-sensors-for-various-applications/>`_
Wiring
*******
This sample uses the SHT3X_DIS sensor controlled using the I2C interface.
Connect Supply: **VDD**, **GND** and Interface: **SDA**, **SCL**
and optionally connect the **ALERT** to a interrupt capable GPIO.
The supply voltage can be in the 2.15V to 5.5V range.
Depending on the baseboard used, the **SDA** and **SCL** lines require Pull-Up
resistors.
Building and Running
********************
This project outputs sensor data to the console. It requires a SHT3XD
sensor. It should work with any platform featuring a I2C peripheral
interface. It does not work on QEMU. In this example below the
:ref:`nrf51_ble400` board is used.
.. zephyr-app-commands::
:zephyr-app: samples/sensors/sht3xd
:board: nrf51_ble400
:goals: build flash
Sample Output
=============
.. code-block:: console
SHT3XD: 19.64 Cel ; 41.96 %RH
SHT3XD: 19.74 Cel ; 42.06 %RH
SHT3XD: 19.75 Cel ; 42.08 %RH
<repeats endlessly>

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2018 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#if defined(CONFIG_HAS_DTS_I2C)
#ifndef DT_SHT3XD_NAME
#ifdef DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_LABEL
#define DT_SHT3XD_NAME DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_LABEL
#define DT_SHT3XD_I2C_ADDR DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_BASE_ADDRESS
#define DT_SHT3XD_I2C_MASTER_DEV_NAME DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_BUS_NAME
#ifdef DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_ALERT_GPIOS_CONTROLLER
#define DT_SHT3XD_GPIO_ALERT_DEV_NAME DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_ALERT_GPIOS_CONTROLLER
#define DT_SHT3XD_GPIO_ALERT_PIN DT_NORDIC_NRF_I2C_40003000_SENSIRION_SHT3XD_44_ALERT_GPIOS_PIN
#endif /* GPIO_ALERT */
#else /* board selection */
#error No device tree bindings for SHT3XD
#endif /* board selection */
#endif /* DT_SHT3XD_NAME */
#endif /* CONFIG_HAS_DTS_I2C */

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2018 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
status = "ok";
sda-pin = <0>;
scl-pin = <1>;
sht3xd: sht3xd@44 {
compatible = "sensirion,sht3xd";
reg = <0x44>;
label = "SHT3XD";
alert-gpios = <&gpio0 2 GPIO_INT_ACTIVE_HIGH>;
};
};

View file

@ -0,0 +1,10 @@
#
# Copyright (c) 2018 Peter Bigot Consulting, LLC
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_SHT3XD=y

View file

@ -0,0 +1,13 @@
#
# Copyright (c) 2018 Peter Bigot Consulting, LLC
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_I2C_NRFX=y
CONFIG_I2C_0=y
CONFIG_SENSOR=y
CONFIG_SHT3XD=y
CONFIG_SHT3XD_TRIGGER_OWN_THREAD=y

View file

@ -0,0 +1,14 @@
#
# Copyright (c) 2018 Peter Bigot Consulting, LLC
#
# SPDX-License-Identifier: Apache-2.0
#
sample:
name: SHT3XD Sensor Sample
tests:
test:
build_only: true
platform_whitelist: nrf51_ble400
tags: sensors
extra_args: CONF_FILE="prj_nrf.conf"

View file

@ -0,0 +1,89 @@
/*
* Copyright (c) 2018 Peter Bigot Consulting, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <sensor.h>
#include <stdio.h>
#define ALERT_HUMIDITY 40
#ifdef CONFIG_SHT3XD_TRIGGER
static volatile bool alerted;
static void trigger_handler(struct device *dev, struct sensor_trigger *trig)
{
alerted = !alerted;
}
#endif
void main(void)
{
struct device *dev = device_get_binding(DT_SHT3XD_NAME);
int rc;
if (dev == NULL) {
printf("Could not get SHT3XD device\n");
return;
}
#ifdef CONFIG_SHT3XD_TRIGGER
struct sensor_trigger trig = {
.type = SENSOR_TRIG_THRESHOLD,
.chan = SENSOR_CHAN_HUMIDITY,
};
struct sensor_value lo_thr = { 0 };
struct sensor_value hi_thr = { ALERT_HUMIDITY };
bool last_alerted = false;
rc = sensor_attr_set(dev, SENSOR_CHAN_HUMIDITY,
SENSOR_ATTR_LOWER_THRESH, &lo_thr);
if (rc == 0) {
rc = sensor_attr_set(dev, SENSOR_CHAN_HUMIDITY,
SENSOR_ATTR_UPPER_THRESH, &hi_thr);
}
if (rc == 0) {
rc = sensor_trigger_set(dev, &trig, trigger_handler);
}
printf("Alert outside %d..%d %%RH got %d\n", lo_thr.val1,
hi_thr.val1, rc);
#endif
while (true) {
struct sensor_value temp, hum;
#ifdef CONFIG_SHT3XD_TRIGGER
if (alerted != last_alerted) {
static const char *const alert_str[] = {
"below",
"above",
};
printf("Humidity %s %d!\n", alert_str[alerted],
hi_thr.val1);
last_alerted = alerted;
}
#endif
rc = sensor_sample_fetch(dev);
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
&temp);
}
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY,
&hum);
}
if (rc != 0) {
printf("SHT3XD: failed: %d\n", rc);
break;
}
printf("SHT3XD: %.2f Cel ; %0.2f %%RH\n",
sensor_value_to_double(&temp),
sensor_value_to_double(&hum));
k_sleep(2000);
}
}

View file

@ -138,6 +138,14 @@
#define DT_LIS2DH_INT2_GPIO_PIN 1
#endif
#ifndef DT_SHT3XD_NAME
#define DT_SHT3XD_NAME ""
#define DT_SHT3XD_I2C_ADDR 0
#define DT_SHT3XD_I2C_MASTER_DEV_NAME ""
#define DT_SHT3XD_GPIO_ALERT_DEV_NAME ""
#define DT_SHT3XD_GPIO_ALERT_PIN 0
#endif
#endif /* CONFIG_HAS_DTS_I2C */
#if defined(CONFIG_HAS_DTS_SPI)