Skip to content

samples: sensor: pressure_polling: Log altitude only if the sensor supports it #93607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions samples/sensor/pressure_polling/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Overview
This sample application periodically reads the sensor
temperature and pressure, displaying the
values on the console along with a timestamp since startup.
It also displays the estimated altitude if floating point is supported.
It also displays the estimated altitude if floating point is supported
and the sensor provides altitude data.

Wiring
*******
Expand Down Expand Up @@ -47,14 +48,14 @@ Sample Output
## Default configuration

Found device "icp101xx@63", getting sensor data
[00:00:00.266,479] <inf> PRESSURE_POLLING: Starting pressure and altitude polling sample.
[00:00:00.273,803] <inf> PRESSURE_POLLING: temp 25.49 Cel, pressure 96.271438 kPa, altitude 447.208465 m
[00:00:00.280,914] <inf> PRESSURE_POLLING: temp 25.50 Cel, pressure 96.271331 kPa, altitude 447.234161 m
[00:00:00.288,024] <inf> PRESSURE_POLLING: temp 25.49 Cel, pressure 96.266685 kPa, altitude 447.636077 m
[00:00:00.295,135] <inf> PRESSURE_POLLING: temp 25.50 Cel, pressure 96.267951 kPa, altitude 447.537078 m
[00:00:00.302,246] <inf> PRESSURE_POLLING: temp 25.51 Cel, pressure 96.268577 kPa, altitude 447.488281 m
[00:00:00.309,356] <inf> PRESSURE_POLLING: temp 25.50 Cel, pressure 96.269340 kPa, altitude 447.414978 m
[00:00:00.316,467] <inf> PRESSURE_POLLING: temp 25.50 Cel, pressure 96.268562 kPa, altitude 447.473663 m
[00:00:00.323,547] <inf> PRESSURE_POLLING: temp 25.50 Cel, pressure 96.267341 kPa, altitude 447.596496 m
Starting pressure and altitude polling sample.
temp 25.49 Cel, pressure 96.271438 kPa, altitude 447.208465 m
temp 25.50 Cel, pressure 96.271331 kPa, altitude 447.234161 m
temp 25.49 Cel, pressure 96.266685 kPa, altitude 447.636077 m
temp 25.50 Cel, pressure 96.267951 kPa, altitude 447.537078 m
temp 25.51 Cel, pressure 96.268577 kPa, altitude 447.488281 m
temp 25.50 Cel, pressure 96.269340 kPa, altitude 447.414978 m
temp 25.50 Cel, pressure 96.268562 kPa, altitude 447.473663 m
temp 25.50 Cel, pressure 96.267341 kPa, altitude 447.596496 m

<repeats endlessly>
21 changes: 12 additions & 9 deletions samples/sensor/pressure_polling/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(PRESSURE_POLLING, CONFIG_SENSOR_LOG_LEVEL);

/*
* Get a device structure from a devicetree node from alias
Expand All @@ -35,6 +32,7 @@ static const struct device *get_pressure_sensor_device(void)
int main(void)
{
const struct device *dev = get_pressure_sensor_device();
int ret;

if (dev == NULL) {
return 0;
Expand All @@ -43,19 +41,24 @@ int main(void)
struct sensor_value temperature;
struct sensor_value altitude;

LOG_INF("Starting pressure and altitude polling sample.\n");
printk("Starting pressure, temperature and altitude polling sample.\n");

while (1) {
if (sensor_sample_fetch_chan(dev, SENSOR_CHAN_ALL) == 0) {
sensor_channel_get(dev, SENSOR_CHAN_PRESS, &pressure);
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temperature);
sensor_channel_get(dev, SENSOR_CHAN_ALTITUDE, &altitude);
ret = sensor_channel_get(dev, SENSOR_CHAN_ALTITUDE, &altitude);

LOG_INF("temp %.2f Cel, pressure %f kPa, altitude %f m",
sensor_value_to_double(&temperature),
sensor_value_to_double(&pressure),
sensor_value_to_double(&altitude));
printk("temp %.2f Cel, pressure %f kPa",
sensor_value_to_double(&temperature),
sensor_value_to_double(&pressure));
if (ret == 0) {
printk(", altitude %f m", sensor_value_to_double(&altitude));
}
printk("\n");
}

k_msleep(1000);
}
return 0;
}