1 /*
2 * Copyright (c) 2012-2014 Wind River Systems, Inc.
3 * Copyright (c) 2021 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/kernel.h>
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/sensor.h>
12
13 /*
14 * Get a device structure from a devicetree node with compatible
15 * "bosch,bme280". (If there are multiple, just pick one.)
16 */
get_bme280_device(void)17 static const struct device *get_bme280_device(void)
18 {
19 const struct device *const dev = DEVICE_DT_GET_ANY(bosch_bme280);
20
21 if (dev == NULL) {
22 /* No such node, or the node does not have status "okay". */
23 printk("\nError: no device found.\n");
24 return NULL;
25 }
26
27 if (!device_is_ready(dev)) {
28 printk("\nError: Device \"%s\" is not ready; "
29 "check the driver initialization logs for errors.\n",
30 dev->name);
31 return NULL;
32 }
33
34 printk("Found device \"%s\", getting sensor data\n", dev->name);
35 return dev;
36 }
37
main(void)38 int main(void)
39 {
40 const struct device *dev = get_bme280_device();
41
42 if (dev == NULL) {
43 return 0;
44 }
45
46 while (1) {
47 struct sensor_value temp, press, humidity;
48
49 sensor_sample_fetch(dev);
50 sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
51 sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press);
52 sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity);
53
54 printk("temp: %d.%06d; press: %d.%06d; humidity: %d.%06d\n",
55 temp.val1, temp.val2, press.val1, press.val2,
56 humidity.val1, humidity.val2);
57
58 k_sleep(K_MSEC(1000));
59 }
60 return 0;
61 }
62