blob: 57388587da2316569f585282e0782c719e92bc0d [file] [log] [blame]
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>
/*
* Get a device structure from a devicetree node with compatible
* "bosch,bme280". (If there are multiple, just pick one.)
*/
static const struct device *get_bme280_device(void)
{
const struct device *const dev = DEVICE_DT_GET_ANY(bosch_bme280);
if (dev == NULL) {
/* No such node, or the node does not have status "okay". */
printk("\nError: no device found.\n");
return NULL;
}
if (!device_is_ready(dev)) {
printk("\nError: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.\n",
dev->name);
return NULL;
}
printk("Found device \"%s\", getting sensor data\n", dev->name);
return dev;
}
void main(void)
{
const struct device *dev = get_bme280_device();
if (dev == NULL) {
return;
}
while (1) {
struct sensor_value temp, press, humidity;
sensor_sample_fetch(dev);
sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
temp.val1, temp.val2, press.val1, press.val2,
humidity.val1, humidity.val2);
k_sleep(K_MSEC(1000));
}
}