Skip to content

Commit d0198f3

Browse files
committed
app: add fetching data from bme280 sensor
- Remove example_sensor node and add BME280 sensor to nrf9160dk_nrf9160.overlay with correct I2C2 pinctrl and address - Enable logging in prj.conf - Update main.c to use BME280 sensor, fetch and log temperature, pressure, and humidity - Remove unused variables and legacy proximity logic Refs: #4 Signed-off-by: Natalia Pluta <pluta.natalia.m@gmail.com>
1 parent 1a30c44 commit d0198f3

3 files changed

Lines changed: 49 additions & 26 deletions

File tree

app/boards/nrf9160dk_nrf9160.overlay

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,36 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
/ {
7-
example_sensor: example-sensor {
8-
compatible = "zephyr,example-sensor";
9-
input-gpios = <&gpio0 11 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
6+
&pinctrl {
7+
i2c2_default_alt: i2c2_default_alt {
8+
group1 {
9+
psels = <NRF_PSEL(TWIM_SDA, 0, 30)>,
10+
<NRF_PSEL(TWIM_SCL, 0, 31)>;
11+
};
12+
};
13+
14+
i2c2_sleep_alt: i2c2_sleep_alt {
15+
group1 {
16+
psels = <NRF_PSEL(TWIM_SDA, 0, 30)>,
17+
<NRF_PSEL(TWIM_SCL, 0, 31)>;
18+
low-power-enable;
19+
};
20+
};
21+
};
22+
23+
&i2c2 {
24+
status = "okay";
25+
pinctrl-0 = <&i2c2_default_alt>;
26+
pinctrl-1 = <&i2c2_sleep_alt>;
27+
pinctrl-names = "default", "sleep";
28+
bme280: bme280@77 {
29+
compatible = "bosch,bme280";
30+
reg = <0x77>;
31+
status = "okay";
1032
};
33+
};
1134

35+
/ {
1236
blink_led: blink-led {
1337
compatible = "blink-gpio-led";
1438
led-gpios = <&gpio0 5 GPIO_ACTIVE_HIGH>; // Green LED 4 on the nRF9160 DK

app/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#
44
# This file contains selected Kconfig options for the application.
55

6+
CONFIG_LOG=y
67
CONFIG_SENSOR=y
78
CONFIG_BLINK=y

app/src/main.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
1919
int main(void)
2020
{
2121
int ret;
22-
unsigned int period_ms = BLINK_PERIOD_MS_MAX;
2322
const struct device *sensor, *blink;
24-
struct sensor_value last_val = {0}, val;
2523

26-
printk("Zephyr Fire Detection System %s\n", APP_VERSION_STRING);
24+
LOG_INF("Zephyr Fire Detection System %s", APP_VERSION_STRING);
2725

28-
sensor = DEVICE_DT_GET(DT_NODELABEL(example_sensor));
26+
sensor = DEVICE_DT_GET(DT_NODELABEL(bme280));
2927
if (!device_is_ready(sensor)) {
30-
LOG_ERR("Sensor not ready");
28+
LOG_ERR("BME280 sensor not ready");
3129
return 0;
3230
}
3331

@@ -43,35 +41,35 @@ int main(void)
4341
return 0;
4442
}
4543

46-
printk("Use the sensor to change LED blinking period\n");
47-
4844
while (1) {
4945
ret = sensor_sample_fetch(sensor);
5046
if (ret < 0) {
51-
LOG_ERR("Could not fetch sample (%d)", ret);
52-
return 0;
47+
LOG_ERR("Could not fetch BME280 sample (%d)", ret);
48+
k_sleep(K_MSEC(1000));
49+
continue;
5350
}
5451

55-
ret = sensor_channel_get(sensor, SENSOR_CHAN_PROX, &val);
52+
struct sensor_value temp, press, hum;
53+
54+
ret = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp);
5655
if (ret < 0) {
57-
LOG_ERR("Could not get sample (%d)", ret);
58-
return 0;
56+
LOG_ERR("Could not get temperature (%d)", ret);
5957
}
6058

61-
if ((last_val.val1 == 0) && (val.val1 == 1)) {
62-
if (period_ms == 0U) {
63-
period_ms = BLINK_PERIOD_MS_MAX;
64-
} else {
65-
period_ms -= BLINK_PERIOD_MS_STEP;
66-
}
59+
ret = sensor_channel_get(sensor, SENSOR_CHAN_PRESS, &press);
60+
if (ret < 0) {
61+
LOG_ERR("Could not get pressure (%d)", ret);
62+
}
6763

68-
printk("Proximity detected, setting LED period to %u ms\n", period_ms);
69-
blink_set_period_ms(blink, period_ms);
64+
ret = sensor_channel_get(sensor, SENSOR_CHAN_HUMIDITY, &hum);
65+
if (ret < 0) {
66+
LOG_ERR("Could not get humidity (%d)", ret);
7067
}
7168

72-
last_val = val;
69+
LOG_INF("BME280: Temp: %d.%06d C, Press: %d.%06d kPa, Hum: %d.%06d %%", temp.val1,
70+
temp.val2, press.val1, press.val2, hum.val1, hum.val2);
7371

74-
k_sleep(K_MSEC(100));
72+
k_sleep(K_MSEC(1000));
7573
}
7674

7775
return 0;

0 commit comments

Comments
 (0)