Skip to content

Commit

Permalink
samples: sensor: dht_polling: Add generic dht sample application
Browse files Browse the repository at this point in the history
This simple application periodically prints the temperature and humidity
measured by one or more digital humidity/temperature sensors.

Signed-off-by: Ian Morris <[email protected]>
  • Loading branch information
iandmorris committed Oct 27, 2023
1 parent a322208 commit 2b310a7
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/sensor/dht_polling/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 49 additions & 0 deletions samples/sensor/dht_polling/README.rst
Original file line number Diff line number Diff line change
@@ -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.31129 oC humidity is 30.391259 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.446194 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.372947 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.391259 %RH
hs300x@44: temp is 25.31129 oC humidity is 30.372947 %RH
hs300x@44: temp is 25.31129 oC humidity is 30.354635 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.372947 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.372947 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.391259 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.446194 %RH
hs300x@44: temp is 25.51272 oC humidity is 30.531648 %RH
22 changes: 22 additions & 0 deletions samples/sensor/dht_polling/boards/nucleo_f401re.overlay
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions samples/sensor/dht_polling/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SENSOR=y
20 changes: 20 additions & 0 deletions samples/sensor/dht_polling/sample.yaml
Original file line number Diff line number Diff line change
@@ -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 (.*) oC humidity is (.*) %RH"
61 changes: 61 additions & 0 deletions samples/sensor/dht_polling/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.%d oC humidity is %d.%d %%RH\n", dev->name,
temp.val1, temp.val2, hum.val1, hum.val2);
}
k_msleep(1000);
}
return 0;
}

0 comments on commit 2b310a7

Please sign in to comment.