Skip to content

Commit 816d2c4

Browse files
pabigotMaureenHelm
authored andcommitted
samples: sensor: dht: add sample
Add a sample to test the Aosong temperature/humidity sensor. Signed-off-by: Peter Bigot <[email protected]>
1 parent 0cfac51 commit 816d2c4

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

samples/sensor/dht/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2019 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
cmake_minimum_required(VERSION 3.13.1)
8+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
9+
project(dht)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})

samples/sensor/dht/README.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.. _dht:
2+
3+
DHT: Aosong DHT Digital-output Humidity and Temperature Sensor
4+
##############################################################
5+
6+
Description
7+
***********
8+
9+
This sample application periodically (0.5 Hz) measures the ambient
10+
temperature and humidity. The result is written to the console.
11+
12+
Wiring
13+
*******
14+
15+
This sample uses an external breakout for the sensor. A devicetree
16+
overlay must be provided to identify the sensor variant and the GPIO
17+
used to control the sensor.
18+
19+
Building and Running
20+
********************
21+
22+
After providing a devicetree overlay that specifies the sensor location,
23+
build this sample app using:
24+
25+
.. zephyr-app-commands::
26+
:zephyr-app: samples/sensor/dht
27+
:board: nrf52_pca10040
28+
:goals: build flash
29+
30+
Sample Output
31+
=============
32+
33+
.. code-block:: console
34+
35+
*** Booting Zephyr OS build zephyr-v2.1.0-329-g38418b26c4cc ***
36+
[0:00:00.027]: 20.0 Cel ; 48.7 %RH
37+
[0:00:02.053]: 19.8 Cel ; 48.7 %RH
38+
[0:00:04.079]: 20.0 Cel ; 48.7 %RH
39+
[0:00:06.105]: 19.8 Cel ; 48.7 %RH
40+
[0:00:08.130]: 20.0 Cel ; 48.8 %RH
41+
[0:00:10.156]: 20.1 Cel ; 48.8 %RH
42+
[0:00:12.182]: 19.7 Cel ; 48.7 %RH
43+
[0:00:14.207]: 20.0 Cel ; 48.8 %RH
44+
45+
<repeats endlessly>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
dht22 {
9+
compatible = "aosong,dht";
10+
status = "okay";
11+
label = "DHT22";
12+
dio-gpios = <&gpio0 11 0>;
13+
dht22;
14+
};
15+
};

samples/sensor/dht/prj.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright (c) 2019 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
CONFIG_SENSOR=y
8+
CONFIG_GPIO=y
9+
CONFIG_DHT=y
10+
11+
# Need float format support
12+
CONFIG_NEWLIB_LIBC=y
13+
CONFIG_NEWLIB_LIBC_NANO=n
14+
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

samples/sensor/dht/sample.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (c) 2019 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
sample:
8+
name: DHT Sensor Sample
9+
tests:
10+
sample.sensor.dht:
11+
build_only: true
12+
platform_whitelist: nrf52_pca10040
13+
tags: sensors

samples/sensor/dht/src/main.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <device.h>
9+
#include <drivers/sensor.h>
10+
#include <stdio.h>
11+
12+
static const char *now_str(void)
13+
{
14+
static char buf[16]; /* ...HH:MM:SS.MMM */
15+
u32_t now = k_uptime_get_32();
16+
unsigned int ms = now % MSEC_PER_SEC;
17+
unsigned int s;
18+
unsigned int min;
19+
unsigned int h;
20+
21+
now /= MSEC_PER_SEC;
22+
s = now % 60U;
23+
now /= 60U;
24+
min = now % 60U;
25+
now /= 60U;
26+
h = now;
27+
28+
snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u",
29+
h, min, s, ms);
30+
return buf;
31+
}
32+
33+
void main(void)
34+
{
35+
const char *const label = DT_INST_0_AOSONG_DHT_LABEL;
36+
struct device *dht22 = device_get_binding(label);
37+
38+
if (!dht22) {
39+
printf("Failed to find sensor %s\n", label);
40+
return;
41+
}
42+
43+
while (true) {
44+
int rc = sensor_sample_fetch(dht22);
45+
46+
if (rc != 0) {
47+
printf("Sensor fetch failed: %d\n", rc);
48+
break;
49+
}
50+
51+
struct sensor_value temperature;
52+
struct sensor_value humidity;
53+
54+
rc = sensor_channel_get(dht22, SENSOR_CHAN_AMBIENT_TEMP,
55+
&temperature);
56+
if (rc == 0) {
57+
rc = sensor_channel_get(dht22, SENSOR_CHAN_HUMIDITY,
58+
&humidity);
59+
}
60+
if (rc != 0) {
61+
printf("get failed: %d\n", rc);
62+
break;
63+
}
64+
65+
printf("[%s]: %.1f Cel ; %.1f %%RH\n",
66+
now_str(),
67+
sensor_value_to_double(&temperature),
68+
sensor_value_to_double(&humidity));
69+
k_sleep(K_SECONDS(2));
70+
}
71+
}

0 commit comments

Comments
 (0)