-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_bme280.c
270 lines (241 loc) · 6.08 KB
/
check_bme280.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Compile:make
Run: ./check_bme280
This Demo is tested on Raspberry PI 3B+
you can use I2C or SPI interface to test this Demo
When you use I2C interface, the default Address in this demo is 0X77
When you use SPI interface, PIN 27 define SPI_CS
*/
#include "bme280.h"
#include <stdio.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
//Raspberry 3B+ platform's default SPI channel
#define channel 0
//Default write it to the register in one time
#define USESPISINGLEREADWRITE 0
#include <string.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
//Raspberry 3B+ platform's default I2C device file
#define IIC_Dev "/dev/i2c-1"
#define FAHRENHEIT 1
#define CELSIUS 2
#define KELVIN 3
#define DEFAULT_TEMP_SCALE FAHRENHEIT
#define MODE_TEMPERATURE 1
#define MODE_PRESSURE 2
#define MODE_MOISTURE 3
#define MODE_HUMIDITY 3
#undef DEBUG
int fd;
void user_delay_ms(uint32_t period)
{
usleep(period*1000);
}
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
write(fd, ®_addr, 1);
read(fd, data, len);
return 0;
}
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
int8_t *buf = alloca(len + 1);
buf[0] = reg_addr;
memcpy(buf + 1, data, len);
write(fd, buf, len + 1);
return 0;
}
void usage(const char *cmd) {
fprintf(stdout, "Usage: %s [-h] {-t|-p|-m} [-c <n>] [-w <n>] [-W <n>] [-C <n>]\n", cmd);
fprintf(stdout, " -h: print this help\n");
fprintf(stdout, " -t: check temperature\n");
fprintf(stdout, " -p: check pressure\n");
fprintf(stdout, " -m: check moisture\n");
fprintf(stdout, " -s <temperature scale>: fahrenheit, celsius, or kelvin\n");
fprintf(stdout, " -c <n>: numbers below this value are considered critical\n");
fprintf(stdout, " -w <n>: numbers below this value are considered a warning condition\n");
fprintf(stdout, " -W <n>: numbers above this value are considered a warning condition\n");
fprintf(stdout, " -C <n>: numbers above this value are considered critical\n");
}
int main(int argc, char* argv[])
{
struct bme280_dev dev = {
.dev_id = BME280_I2C_ADDR_SEC,
.intf = BME280_I2C_INTF,
.read = user_i2c_read,
.write = user_i2c_write,
.delay_ms = user_delay_ms,
.settings = {
.osr_h = BME280_OVERSAMPLING_1X,
.osr_p = BME280_OVERSAMPLING_16X,
.osr_t = BME280_OVERSAMPLING_2X,
.filter = BME280_FILTER_COEFF_16,
.standby_time = BME280_STANDBY_TIME_62_5_MS
}
};
uint8_t settings_sel = BME280_STANDBY_SEL | BME280_FILTER_SEL;
struct bme280_data comp_data;
signed char opt;
const char *perf = "temperature";
int8_t mode = 0;
float lcrit = -10000.0, lwarn = -10000.0, hcrit = 10000.0, hwarn = 10000.0, outval = -1.0;
int res = 0;
int scale = DEFAULT_TEMP_SCALE;
#ifdef DEBUG
fprintf(stdout, "Starting...\n");
#endif
while (-1 != (opt = getopt(argc, argv, "c:w:C:W:tpmhs:"))) {
#ifdef DEBUG
fprintf(stdout, "Handling option %c (%d)\n", opt, opt);
#endif
switch (opt) {
case 'c': /* low critical level */
if (sscanf(optarg, "%f", &lcrit) < 1) {
usage(argv[0]);
exit(1);
}
break;
case 'C': /* high critical level */
if (sscanf(optarg, "%f", &hcrit) < 1) {
usage(argv[0]);
exit(1);
}
break;
case 'w': /* low warning level */
if (sscanf(optarg, "%f", &lwarn) < 1) {
usage(argv[0]);
exit(1);
}
break;
case 'W': /* high warning level */
if (sscanf(optarg, "%f", &hwarn) < 1) {
usage(argv[0]);
exit(1);
}
break;
case 't': /* temperature */
mode = MODE_TEMPERATURE;
break;
case 'p': /* pressure */
mode = MODE_PRESSURE;
perf = "pressure";
break;
case 'm': /* moisture */
mode = MODE_MOISTURE;
perf = "humidity";
break;
case 's': /* temperature scale */
switch (optarg[0]) {
case 'f':
case 'F':
scale = FAHRENHEIT;
perf = "fahrenheit";
break;
case 'c':
case 'C':
scale = CELSIUS;
perf = "celsius";
break;
case 'k':
case 'K':
scale = KELVIN;
perf = "kelvin";
break;
default:
usage(argv[0]);
exit(1);
}
break;
case 'h': /* help */
usage(argv[0]);
exit(0);
}
}
#ifdef DEBUG
fprintf(stdout, "Past options parsing\n");
#endif
switch (mode) {
case 0:
usage(argv[0]);
exit(1);
case MODE_TEMPERATURE:
settings_sel |= BME280_OSR_TEMP_SEL;
break;
case MODE_PRESSURE:
settings_sel |= BME280_OSR_PRESS_SEL;
break;
case MODE_MOISTURE:
settings_sel |= BME280_OSR_HUM_SEL;
break;
default:
exit(1);
}
#ifdef DEBUG
fprintf(stdout, "Done setting the mode\n");
#endif
if ((fd = open(IIC_Dev, O_RDWR)) < 0) {
printf("Failed to open the i2c bus %s", argv[1]);
exit(1);
}
if (ioctl(fd, I2C_SLAVE, 0x77) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
#ifdef DEBUG
fprintf(stdout, "Device open\n");
#endif
bme280_init(&dev);
bme280_set_sensor_settings(settings_sel, &dev);
bme280_set_sensor_mode(BME280_NORMAL_MODE, &dev);
/* Delay while the sensor completes a measurement */
dev.delay_ms(70);
bme280_get_sensor_data(BME280_ALL, &comp_data, &dev);
#ifdef DEBUG
fprintf(stdout, "Got sensor data\n");
#endif
switch (mode) {
case MODE_TEMPERATURE: /* temperature */
switch (scale) {
case FAHRENHEIT:
outval = comp_data.temperature * 1.8 + 32.0;
fprintf(stdout, "%.1f°F", outval);
break;
case CELSIUS:
outval = comp_data.temperature;
fprintf(stdout, "%.1f°C", outval);
break;
case KELVIN:
outval = comp_data.temperature + 273.15;
fprintf(stdout, "%.1fK", outval);
}
break;
case MODE_PRESSURE:
outval = comp_data.pressure / 100.0;
fprintf(stdout, "%.0fmbs", outval);
break;
case MODE_MOISTURE:
outval = comp_data.humidity;
fprintf(stdout, "%.0f%%", outval);
}
if (outval < lcrit) {
fprintf(stdout, " (<%.1f)", lcrit);
res = 2;
} else if (outval < lwarn) {
fprintf(stdout, " (<%.1f)", lwarn);
res = 1;
} else if (outval > hwarn) {
fprintf(stdout, " (>%.1f)", hwarn);
res = 1;
} else if (outval > hcrit) {
fprintf(stdout, " (>%.1f)", hcrit);
res = 2;
}
fprintf(stdout, " | %s=%.2f\n", perf, outval);
return res;
}