Skip to content

Commit df60da7

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 258915c commit df60da7

3 files changed

Lines changed: 56 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
#
44
# This file contains selected Kconfig options for the application.
55

6+
CONFIG_LOG=y
67
CONFIG_SENSOR=y
78
CONFIG_BLINK=y
9+
10+
# Enable I2C and sensor subsystem for BME280
11+
CONFIG_I2C=y
12+
CONFIG_SENSOR=y
13+
CONFIG_BME280=y

app/src/main.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ 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;
23+
struct sensor_value temp, press, hum;
2524

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

28-
sensor = DEVICE_DT_GET(DT_NODELABEL(example_sensor));
27+
sensor = DEVICE_DT_GET(DT_NODELABEL(bme280));
2928
if (!device_is_ready(sensor)) {
30-
LOG_ERR("Sensor not ready");
29+
LOG_ERR("BME280 sensor not ready");
3130
return 0;
3231
}
3332

@@ -43,35 +42,36 @@ int main(void)
4342
return 0;
4443
}
4544

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

55-
ret = sensor_channel_get(sensor, SENSOR_CHAN_PROX, &val);
53+
ret = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &temp);
5654
if (ret < 0) {
57-
LOG_ERR("Could not get sample (%d)", ret);
58-
return 0;
55+
LOG_ERR("Could not get temperature (%d)", ret);
56+
continue;
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+
continue;
63+
}
6764

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

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

74-
k_sleep(K_MSEC(100));
74+
k_sleep(K_MSEC(1000));
7575
}
7676

7777
return 0;

0 commit comments

Comments
 (0)