From 4e99762dcc740b964cad118ad89902a3e781d4ce Mon Sep 17 00:00:00 2001 From: Ian Morris Date: Fri, 22 Sep 2023 17:38:07 -0700 Subject: [PATCH] samples: sensor: dht_polling: Add generic dht sample application This simple application periodically prints the temperature and humidity measured by one or more digital humidity/temperature sensors. Signed-off-by: Ian Morris --- samples/sensor/dht_polling/CMakeLists.txt | 9 +++ samples/sensor/dht_polling/README.rst | 49 +++++++++++++++ .../dht_polling/boards/nucleo_f401re.overlay | 22 +++++++ samples/sensor/dht_polling/prj.conf | 2 + samples/sensor/dht_polling/sample.yaml | 20 ++++++ samples/sensor/dht_polling/src/main.c | 61 +++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 samples/sensor/dht_polling/CMakeLists.txt create mode 100644 samples/sensor/dht_polling/README.rst create mode 100644 samples/sensor/dht_polling/boards/nucleo_f401re.overlay create mode 100644 samples/sensor/dht_polling/prj.conf create mode 100644 samples/sensor/dht_polling/sample.yaml create mode 100644 samples/sensor/dht_polling/src/main.c diff --git a/samples/sensor/dht_polling/CMakeLists.txt b/samples/sensor/dht_polling/CMakeLists.txt new file mode 100644 index 000000000000000..31b4352da9d70f1 --- /dev/null +++ b/samples/sensor/dht_polling/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright (c) 2023 Ian Morris +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(dht_polling) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/dht_polling/README.rst b/samples/sensor/dht_polling/README.rst new file mode 100644 index 000000000000000..f2479419c784961 --- /dev/null +++ b/samples/sensor/dht_polling/README.rst @@ -0,0 +1,49 @@ +.. _dht_polling: + +Generic Digital Humidity Temperature sensor polling sample +########################################################## + +Overview +******** + +This sample application demonstrates how to use digital humidity temperature +sensors. + +Building and Running +******************** + +This sample supports up to 10 humidity/temperature sensors. Each sensor needs to +be aliased as ``dhtN`` where ``N`` goes from ``0`` to ``9``. For example: + +.. code-block:: devicetree + + / { + aliases { + dht0 = &hs300x; + }; + }; + +Make sure the aliases are in devicetree, then build and run with: + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/dht_polling + :board: + :goals: build flash + :compact: + +Sample Output +============= + +.. code-block:: console + + hs300x@44: temp is 25.31129 °C humidity is 30.391259 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.446194 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.372947 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.391259 %RH + hs300x@44: temp is 25.31129 °C humidity is 30.372947 %RH + hs300x@44: temp is 25.31129 °C humidity is 30.354635 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.372947 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.372947 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.391259 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.446194 %RH + hs300x@44: temp is 25.51272 °C humidity is 30.531648 %RH diff --git a/samples/sensor/dht_polling/boards/nucleo_f401re.overlay b/samples/sensor/dht_polling/boards/nucleo_f401re.overlay new file mode 100644 index 000000000000000..cb381b5ba3a0f0c --- /dev/null +++ b/samples/sensor/dht_polling/boards/nucleo_f401re.overlay @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Ian Morris + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + dht0 = &hs300x; + }; +}; + + &i2c1 { + status = "okay"; + + hs300x: hs300x@44 { + compatible = "renesas,hs300x"; + reg = <0x44>; + #address-cells = <1>; + #size-cells = <0>; + }; +}; diff --git a/samples/sensor/dht_polling/prj.conf b/samples/sensor/dht_polling/prj.conf new file mode 100644 index 000000000000000..7d668be1f0b1957 --- /dev/null +++ b/samples/sensor/dht_polling/prj.conf @@ -0,0 +1,2 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_SENSOR=y diff --git a/samples/sensor/dht_polling/sample.yaml b/samples/sensor/dht_polling/sample.yaml new file mode 100644 index 000000000000000..8177e636d21670f --- /dev/null +++ b/samples/sensor/dht_polling/sample.yaml @@ -0,0 +1,20 @@ +# +# Copyright (c) 2023 Ian Morris +# +# SPDX-License-Identifier: Apache-2.0 +# + +sample: + description: Digital Humidity Temperature polling sample + name: DHT polling sample +tests: + sample.sensor.dht_polling: + tags: sensors + filter: dt_alias_exists("dht0") + integration_platforms: + - nucleo_f401re + harness: console + harness_config: + type: one_line + regex: + - "[0-9A-Za-z_,+-.]*@[0-9A-Fa-f]*: temp is (.*) °C humidity is (.*) %RH" diff --git a/samples/sensor/dht_polling/src/main.c b/samples/sensor/dht_polling/src/main.c new file mode 100644 index 000000000000000..c08865a1783264a --- /dev/null +++ b/samples/sensor/dht_polling/src/main.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Ian Morris + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include +#include +#include + +#define DHT_ALIAS(i) DT_ALIAS(_CONCAT(dht, i)) +#define DHT_DEVICE(i, _) \ + IF_ENABLED(DT_NODE_EXISTS(DHT_ALIAS(i)), (DEVICE_DT_GET(DHT_ALIAS(i)),)) + +/* Support up to 10 temperature/humidity sensors */ +static const struct device *const sensors[] = {LISTIFY(10, DHT_DEVICE, ())}; + +int main(void) +{ + int rc; + + for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) { + if (!device_is_ready(sensors[i])) { + printk("sensor: device %s not ready.\n", sensors[i]->name); + return 0; + } + } + + while (1) { + for (size_t i = 0; i < ARRAY_SIZE(sensors); i++) { + struct device *dev = (struct device *)sensors[i]; + + rc = sensor_sample_fetch(dev); + if (rc < 0) { + printk("%s: sensor_sample_fetch() failed: %d\n", dev->name, rc); + return rc; + } + + struct sensor_value temp; + struct sensor_value hum; + + 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("get failed: %d\n", rc); + break; + } + + printk("%16s: temp is %d.%d °C humidity is %d.%d %%RH\n", dev->name, + temp.val1, temp.val2, hum.val1, hum.val2); + } + k_msleep(1000); + } + return 0; +}