diff --git a/samples/sensor/dht/CMakeLists.txt b/samples/sensor/dht/CMakeLists.txt new file mode 100644 index 00000000000..e67cb60cfbc --- /dev/null +++ b/samples/sensor/dht/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2019 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(dht) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/dht/README.rst b/samples/sensor/dht/README.rst new file mode 100644 index 00000000000..ed2e996d409 --- /dev/null +++ b/samples/sensor/dht/README.rst @@ -0,0 +1,45 @@ +.. _dht: + +DHT: Aosong DHT Digital-output Humidity and Temperature Sensor +############################################################## + +Description +*********** + +This sample application periodically (0.5 Hz) measures the ambient +temperature and humidity. The result is written to the console. + +Wiring +******* + +This sample uses an external breakout for the sensor. A devicetree +overlay must be provided to identify the sensor variant and the GPIO +used to control the sensor. + +Building and Running +******************** + +After providing a devicetree overlay that specifies the sensor location, +build this sample app using: + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/dht + :board: nrf52_pca10040 + :goals: build flash + +Sample Output +============= + +.. code-block:: console + + *** Booting Zephyr OS build zephyr-v2.1.0-329-g38418b26c4cc *** + [0:00:00.027]: 20.0 Cel ; 48.7 %RH + [0:00:02.053]: 19.8 Cel ; 48.7 %RH + [0:00:04.079]: 20.0 Cel ; 48.7 %RH + [0:00:06.105]: 19.8 Cel ; 48.7 %RH + [0:00:08.130]: 20.0 Cel ; 48.8 %RH + [0:00:10.156]: 20.1 Cel ; 48.8 %RH + [0:00:12.182]: 19.7 Cel ; 48.7 %RH + [0:00:14.207]: 20.0 Cel ; 48.8 %RH + + diff --git a/samples/sensor/dht/boards/nrf52_pca10040.overlay b/samples/sensor/dht/boards/nrf52_pca10040.overlay new file mode 100644 index 00000000000..45fbc9cf1a3 --- /dev/null +++ b/samples/sensor/dht/boards/nrf52_pca10040.overlay @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + dht22 { + compatible = "aosong,dht"; + status = "okay"; + label = "DHT22"; + dio-gpios = <&gpio0 11 0>; + dht22; + }; +}; diff --git a/samples/sensor/dht/prj.conf b/samples/sensor/dht/prj.conf new file mode 100644 index 00000000000..cb0f662d789 --- /dev/null +++ b/samples/sensor/dht/prj.conf @@ -0,0 +1,14 @@ +# +# Copyright (c) 2019 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# + +CONFIG_SENSOR=y +CONFIG_GPIO=y +CONFIG_DHT=y + +# Need float format support +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_NANO=n +CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y diff --git a/samples/sensor/dht/sample.yaml b/samples/sensor/dht/sample.yaml new file mode 100644 index 00000000000..953d2233b40 --- /dev/null +++ b/samples/sensor/dht/sample.yaml @@ -0,0 +1,13 @@ +# +# Copyright (c) 2019 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# + +sample: + name: DHT Sensor Sample +tests: + sample.sensor.dht: + build_only: true + platform_whitelist: nrf52_pca10040 + tags: sensors diff --git a/samples/sensor/dht/src/main.c b/samples/sensor/dht/src/main.c new file mode 100644 index 00000000000..cae431a87df --- /dev/null +++ b/samples/sensor/dht/src/main.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +static const char *now_str(void) +{ + static char buf[16]; /* ...HH:MM:SS.MMM */ + u32_t now = k_uptime_get_32(); + unsigned int ms = now % MSEC_PER_SEC; + unsigned int s; + unsigned int min; + unsigned int h; + + now /= MSEC_PER_SEC; + s = now % 60U; + now /= 60U; + min = now % 60U; + now /= 60U; + h = now; + + snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u", + h, min, s, ms); + return buf; +} + +void main(void) +{ + const char *const label = DT_INST_0_AOSONG_DHT_LABEL; + struct device *dht22 = device_get_binding(label); + + if (!dht22) { + printf("Failed to find sensor %s\n", label); + return; + } + + while (true) { + int rc = sensor_sample_fetch(dht22); + + if (rc != 0) { + printf("Sensor fetch failed: %d\n", rc); + break; + } + + struct sensor_value temperature; + struct sensor_value humidity; + + rc = sensor_channel_get(dht22, SENSOR_CHAN_AMBIENT_TEMP, + &temperature); + if (rc == 0) { + rc = sensor_channel_get(dht22, SENSOR_CHAN_HUMIDITY, + &humidity); + } + if (rc != 0) { + printf("get failed: %d\n", rc); + break; + } + + printf("[%s]: %.1f Cel ; %.1f %%RH\n", + now_str(), + sensor_value_to_double(&temperature), + sensor_value_to_double(&humidity)); + k_sleep(K_SECONDS(2)); + } +}