SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_aux_bmm150_bme280.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <ImuDrv.hpp>
34
35
36// #define USE_I2C_INTERFACE true
37// #define USE_SPI_INTERFACE true
38
39#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
40#define USE_I2C_INTERFACE
41#warning "No interface type is selected, use I2C interface"
42#endif
43
44#if defined(USE_SPI_INTERFACE)
45#ifndef SPI_MOSI
46#define SPI_MOSI 33
47#endif
48
49#ifndef SPI_MISO
50#define SPI_MISO 34
51#endif
52
53#ifndef SPI_SCK
54#define SPI_SCK 35
55#endif
56
57// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI260_IRQ
59#define BHI260_IRQ 37
60#endif
61
62#ifndef BHI260_CS
63#define BHI260_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI260_SDA
69#define BHI260_SDA 2
70#endif
71
72#ifndef BHI260_SCL
73#define BHI260_SCL 3
74#endif
75
76// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI260_IRQ
78#define BHI260_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI260_RST
83#define BHI260_RST -1
84#endif
85
87
88/*
89* Define the USING_DATA_HELPER use of data assistants.
90* No callback function will be used. Data can be obtained directly through
91* the data assistant. Note that this method is not a thread-safe function.
92* Please pay attention to protecting data access security.
93* */
94#define USING_DATA_HELPER
95
96#ifdef USING_DATA_HELPER
97SensorTemperature temperature(bhy);
98SensorHumidity humidity(bhy);
99SensorPressure pressure(bhy);
100#endif
101
102// The firmware runs in RAM and will be lost if the power is off. The firmware will be loaded from RAM each time it is run.
103// #define BOSCH_BHI260_AUX_BEM280
104#define BOSCH_BHI260_AUX_BMM150_BEM280
105// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
106
107// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
108// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
109// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
110// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
111
112#include <BoschFirmware.h>
113
114// Force update of current firmware, whether it exists or not.
115// Only works when external SPI Flash is connected to BHI260.
116// After uploading firmware once, you can change this to false to speed up boot time.
118
119#if BHI260_IRQ > 0
120#define USING_SENSOR_IRQ_METHOD
121#endif
122
123#ifdef USING_SENSOR_IRQ_METHOD
124volatile bool isInterruptTriggered = false;
125
127{
129}
130#endif /*USING_SENSOR_IRQ_METHOD*/
131
132#ifndef USING_DATA_HELPER
133void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
134{
135 float humidity = 0;
136 float temperature = 0;
137 float pressure = 0;
138 switch (sensor_id) {
139 case BoschSensorID::HUMIDITY:
140 bhy2_parse_humidity(data_ptr, &humidity);
141 Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
142 break;
143 case BoschSensorID::TEMPERATURE:
144 bhy2_parse_temperature_celsius(data_ptr, &temperature);
145 Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
146 break;
147 case BoschSensorID::BAROMETER:
148 bhy2_parse_pressure(data_ptr, &pressure);
149 Serial.print("pressure:"); Serial.print(pressure); Serial.println("Pa");
150 break;
151 default:
152 Serial.println("Unkown.");
153 break;
154 }
155}
156#endif
157
158// Firmware update progress callback
159void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
160{
161 float progress = (float)transferred / total * 100;
162 Serial.print("Upload progress: ");
163 Serial.print(progress);
164 Serial.println("%");
165}
166
167void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
168{
169 const char *sensorName = bhy.getSensorName(sensor_id);
170 Serial.print("Configure ");
171 Serial.print(sensorName);
172 Serial.print(" sensor ");
173 Serial.print(sample_rate, 2);
174 Serial.print(" HZ ");
175 if (rslt) {
176 Serial.print("successfully");
177 } else {
178 Serial.print("failed");
179 }
180 Serial.println();
181}
182
183
184void setup()
185{
186 Serial.begin(115200);
187 while (!Serial);
188
189 // Set the reset pin
190 bhy.setPins(BHI260_RST);
191
192 Serial.println("Initializing Sensors...");
193
194 // Set the firmware array address and firmware size
195 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
196
197 // Set the firmware update processing progress callback function
198 // bhy.setUpdateProcessCallback(progress_callback, NULL);
199
200 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
201 // bhy.setMaxiTransferSize(256);
202
203 // Set the processing fifo data buffer size,The default size is 512 bytes.
204 // bhy.setProcessBufferSize(1024);
205
206 // Set to load firmware from flash
207 bhy.setBootFromFlash(bosch_firmware_type);
208
209#ifdef USE_I2C_INTERFACE
210 // Using I2C interface
211 // BHI260AP_SLAVE_ADDRESS_L = 0x28
212 // BHI260AP_SLAVE_ADDRESS_H = 0x29
213 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
214 Serial.print("Failed to initialize sensor - error code:");
215 Serial.println(bhy.getError());
216 while (1) {
217 delay(1000);
218 }
219 }
220#endif
221
222#ifdef USE_SPI_INTERFACE
223 // Using SPI interface
224 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
225 Serial.print("Failed to initialize sensor - error code:");
226 Serial.println(bhy.getError());
227 while (1) {
228 delay(1000);
229 }
230 }
231#endif
232
233 Serial.println("Initializing the sensor successfully!");
234
235 // Output all sensors info to Serial
236 BoschSensorInfo info = bhy.getSensorInfo();
237
238#ifdef ARDUINO
239 ArduinoStreamPrinter printer(Serial);
240 info.printInfo(printer);
241#else
242 info.printInfo([](const char *format, ...) -> int {
243 va_list args;
244 va_start(args, format);
245 int result = vprintf(format, args);
246 va_end(args);
247 return result;
248 });
249#endif
250
251 /*
252 * Enable monitoring.
253 * sample_rate ​​can only control the rate of the pressure sensor.
254 * Temperature and humidity will only be updated when there is a change.
255 * * */
256 float sample_rate = 2.0; /* Set pressure sensor to 2Hz update frequency */
257 uint32_t report_latency_ms = 0; /* Report immediately */
258
259 /*
260 * Enable BME280 function
261 * Function depends on BME280.
262 * If the hardware is not connected to BME280, the function cannot be used.
263 * * */
264#ifdef USING_DATA_HELPER
265 temperature.enable();
266 humidity.enable();
267 pressure.enable(sample_rate, report_latency_ms);
268#else
269 bool rslt = false;
270 rslt = bhy.configure(BoschSensorID::TEMPERATURE, sample_rate, report_latency_ms);
271 printResult(BoschSensorID::TEMPERATURE, sample_rate, rslt);
272 rslt = bhy.configure(BoschSensorID::BAROMETER, sample_rate, report_latency_ms);
273 printResult(BoschSensorID::BAROMETER, sample_rate, rslt);
274 rslt = bhy.configure(BoschSensorID::HUMIDITY, sample_rate, report_latency_ms);
275 printResult(BoschSensorID::HUMIDITY, sample_rate, rslt);
276 // Register BME280 data parse callback function
277 Serial.println("Register sensor result callback function");
278 bhy.onResultEvent(BoschSensorID::TEMPERATURE, parse_bme280_sensor_data);
279 bhy.onResultEvent(BoschSensorID::HUMIDITY, parse_bme280_sensor_data);
280 bhy.onResultEvent(BoschSensorID::BAROMETER, parse_bme280_sensor_data);
281#endif
282
283#ifdef USING_SENSOR_IRQ_METHOD
284 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
285 // This makes the pin ready to receive external signals.
286 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
287 pinMode(BHI260_IRQ, INPUT);
288
289 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
290 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
291 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
292#endif
293
294}
295
296
297void loop()
298{
299 uint32_t s;
300 uint32_t ns;
301
302#ifdef USING_SENSOR_IRQ_METHOD
304 isInterruptTriggered = false;
305#endif /*USING_SENSOR_IRQ_METHOD*/
306
307 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
308 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
309 */
310 bhy.update();
311
312#ifdef USING_SENSOR_IRQ_METHOD
313 }
314#endif /*USING_SENSOR_IRQ_METHOD*/
315
316#ifdef USING_DATA_HELPER
317 if (temperature.hasUpdated()) {
318 temperature.getLastTime(s, ns);
319#ifdef PLATFORM_HAS_PRINTF
320 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
321 Serial.printf("Temperature: %.2f *C %.2f *F %.2f K\n",
322 temperature.getCelsius(), temperature.getFahrenheit(), temperature.getKelvin());
323#else
324 Serial.print("[T: ");
325 Serial.print(s);
326 Serial.print(".");
327 Serial.print(ns);
328 Serial.print("]:");
329 Serial.print("Temperature: ");
330 Serial.print(temperature.getCelsius()); Serial.print(" *C");
331 Serial.print(temperature.getFahrenheit()); Serial.print(" *F");
332 Serial.print(temperature.getKelvin()); Serial.print(" *F");
333 Serial.println();
334#endif
335 }
336
337 if (humidity.hasUpdated()) {
338 humidity.getLastTime(s, ns);
339#ifdef PLATFORM_HAS_PRINTF
340 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
341 Serial.printf("Humidity: %.2f %%\n", humidity.getHumidity());
342#else
343 Serial.print("[T: ");
344 Serial.print(s);
345 Serial.print(".");
346 Serial.print(ns);
347 Serial.print("]:");
348 Serial.print("Humidity: ");
349 Serial.print(humidity.getHumidity()); Serial.print(" %");
350 Serial.println();
351#endif
352 }
353
354 if (pressure.hasUpdated()) {
355 pressure.getLastTime(s, ns);
356#ifdef PLATFORM_HAS_PRINTF
357 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
358 Serial.printf("Pressure: %.2f Pascal\n", pressure.getPressure());
359#else
360 Serial.print("[T: ");
361 Serial.print(s);
362 Serial.print(".");
363 Serial.print(ns);
364 Serial.print("]:");
365 Serial.print("Humidity: ");
366 Serial.print(pressure.getPressure()); Serial.print(" Pascal");
367 Serial.println();
368#endif
369 }
370#endif
371 delay(50);
372}
373
374
375
@license MIT License
@license MIT License
SensorBHI260AP bhy
void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
SensorTemperature temperature(bhy)
#define BHI260_SCL
volatile bool isInterruptTriggered
#define BHI260_IRQ
#define BHI260_SDA
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
SensorPressure pressure(bhy)
#define BHI260_RST
SensorHumidity humidity(bhy)
void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash