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 <ian.d.morris@outlook.com>
This commit is contained in:
parent
4c92419546
commit
dfc747d53a
9
samples/sensor/dht_polling/CMakeLists.txt
Normal file
9
samples/sensor/dht_polling/CMakeLists.txt
Normal file
|
@ -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})
|
49
samples/sensor/dht_polling/README.rst
Normal file
49
samples/sensor/dht_polling/README.rst
Normal file
|
@ -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: <board to use>
|
||||
:goals: build flash
|
||||
:compact:
|
||||
|
||||
Sample Output
|
||||
=============
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
hs300x@44: temp is 25.31 °C humidity is 30.39 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.44 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.37 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.39 %RH
|
||||
hs300x@44: temp is 25.31 °C humidity is 30.37 %RH
|
||||
hs300x@44: temp is 25.31 °C humidity is 30.35 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.37 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.37 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.39 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.44 %RH
|
||||
hs300x@44: temp is 25.51 °C humidity is 30.53 %RH
|
22
samples/sensor/dht_polling/boards/nucleo_f401re.overlay
Normal file
22
samples/sensor/dht_polling/boards/nucleo_f401re.overlay
Normal file
|
@ -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>;
|
||||
};
|
||||
};
|
2
samples/sensor/dht_polling/prj.conf
Normal file
2
samples/sensor/dht_polling/prj.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_SENSOR=y
|
20
samples/sensor/dht_polling/sample.yaml
Normal file
20
samples/sensor/dht_polling/sample.yaml
Normal file
|
@ -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"
|
62
samples/sensor/dht_polling/src/main.c
Normal file
62
samples/sensor/dht_polling/src/main.c
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Ian Morris
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include <zephyr/sys/util_macro.h>
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#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.%02d °C humidity is %d.%02d %%RH\n",
|
||||
dev->name, temp.val1, temp.val2 / 10000,
|
||||
hum.val1, hum.val2 / 10000);
|
||||
}
|
||||
k_msleep(1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue