samples: sensor: dht: add sample
Add a sample to test the Aosong temperature/humidity sensor. Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
parent
0cfac519cc
commit
816d2c47a4
6 changed files with 170 additions and 0 deletions
12
samples/sensor/dht/CMakeLists.txt
Normal file
12
samples/sensor/dht/CMakeLists.txt
Normal file
|
@ -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})
|
45
samples/sensor/dht/README.rst
Normal file
45
samples/sensor/dht/README.rst
Normal file
|
@ -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
|
||||
|
||||
<repeats endlessly>
|
15
samples/sensor/dht/boards/nrf52_pca10040.overlay
Normal file
15
samples/sensor/dht/boards/nrf52_pca10040.overlay
Normal file
|
@ -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;
|
||||
};
|
||||
};
|
14
samples/sensor/dht/prj.conf
Normal file
14
samples/sensor/dht/prj.conf
Normal file
|
@ -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
|
13
samples/sensor/dht/sample.yaml
Normal file
13
samples/sensor/dht/sample.yaml
Normal file
|
@ -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
|
71
samples/sensor/dht/src/main.c
Normal file
71
samples/sensor/dht/src/main.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <drivers/sensor.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue