samples: sensors: add generic co2 sensor polling

Add polling sample for any CO2 sensor.

Signed-off-by: Jeppe Odgaard <jeppe.odgaard@prevas.dk>
This commit is contained in:
Jeppe Odgaard 2023-10-27 15:23:46 +02:00 committed by Anas Nashif
parent b0fdbce4df
commit 171dbd0435
7 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(co2_polling)
target_sources(app PRIVATE src/main.c)

View file

@ -0,0 +1,30 @@
.. co2:
Generic CO2 polling sample
##########################
Overview
********
A sensor sample that demonstrates how to poll a CO2 sensor.
Building and Running
********************
This sample reads the CO2 sensor and print the values continuously.
.. zephyr-app-commands::
:zephyr-app: samples/sensor/co2_polling
:board: <board to use>
:goals: build flash
:compact:
Sample Output
=============
.. code-block:: console
CO2 940 ppm
CO2 950 ppm
<repeats endlessly>

View file

@ -0,0 +1 @@
CONFIG_UART_INTERRUPT_DRIVEN=y

View file

@ -0,0 +1,15 @@
/ {
aliases {
co2 = &explorir_m;
};
};
&lpuart1 {
status = "okay";
current-speed = <9600>;
explorir_m: explorir_m {
compatible = "gss,explorir-m";
status = "okay";
};
};

View file

@ -0,0 +1,7 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_LOG=y
CONFIG_SHELL=y
CONFIG_SENSOR_SHELL=y
CONFIG_SERIAL=y
CONFIG_SENSOR=y
CONFIG_SENSOR_LOG_LEVEL_DBG=y

View file

@ -0,0 +1,8 @@
sample:
name: CO2 sensor sample
tests:
sample.sensor.co2:
harness: sensor
tags: sensors
filter: dt_alias_exists("co2")
depends_on: serial uart_interrupt_driven

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023, Vitrolife A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(MAIN);
int main(void)
{
struct sensor_value value;
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(co2));
if (!device_is_ready(dev)) {
LOG_ERR("%s is not ready", dev->name);
return 0;
}
while (1) {
if (sensor_sample_fetch(dev) == 0 &&
sensor_channel_get(dev, SENSOR_CHAN_CO2, &value) == 0) {
LOG_INF("CO2 %d ppm", value.val1);
}
k_msleep(1000);
}
return 0;
}