SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_aux_bmm350_bmp580_bme688.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 BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI360_IRQ
59#define BHI360_IRQ 37
60#endif
61
62#ifndef BHI360_CS
63#define BHI360_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI360_SDA
69#define BHI360_SDA 2
70#endif
71
72#ifndef BHI360_SCL
73#define BHI360_SCL 3
74#endif
75
76// If BHI360_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI360_IRQ
78#define BHI360_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI360_RST
83#define BHI360_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
103#define BOSCH_SHUTTLE3_BHI360_BMM350C_BMP580_BME688
104
105#include <BoschFirmware.h>
106
107
108#if BHI360_IRQ > 0
109#define USING_SENSOR_IRQ_METHOD
110#endif
111
112#ifdef USING_SENSOR_IRQ_METHOD
113volatile bool isInterruptTriggered = false;
114
116{
118}
119#endif /*USING_SENSOR_IRQ_METHOD*/
120
121#ifndef USING_DATA_HELPER
122void parse_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
123{
124 float humidity = 0;
125 float temperature = 0;
126 float pressure = 0;
127 switch (sensor_id) {
128 case BoschSensorID::HUMIDITY:
129 bhy2_parse_humidity(data_ptr, &humidity);
130 Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
131 break;
132 case BoschSensorID::TEMPERATURE:
133 bhy2_parse_temperature_celsius(data_ptr, &temperature);
134 Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
135 break;
136 case BoschSensorID::BAROMETER:
137 bhy2_parse_pressure(data_ptr, &pressure);
138 Serial.print("pressure:"); Serial.print(pressure); Serial.println("Pa");
139 break;
140 default:
141 Serial.println("Unkown.");
142 break;
143 }
144}
145#endif
146
147// Firmware update progress callback
148void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
149{
150 float progress = (float)transferred / total * 100;
151 Serial.print("Upload progress: ");
152 Serial.print(progress);
153 Serial.println("%");
154}
155
156void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
157{
158 const char *sensorName = bhy.getSensorName(sensor_id);
159 Serial.print("Configure ");
160 Serial.print(sensorName);
161 Serial.print(" sensor ");
162 Serial.print(sample_rate, 2);
163 Serial.print(" HZ ");
164 if (rslt) {
165 Serial.print("successfully");
166 } else {
167 Serial.print("failed");
168 }
169 Serial.println();
170}
171
172
173void setup()
174{
175 Serial.begin(115200);
176 while (!Serial);
177
178 // Set the reset pin
179 bhy.setPins(BHI360_RST);
180
181 Serial.println("Initializing Sensors...");
182
183 // Set the firmware array address and firmware size
184 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
185
186 // Set the firmware update processing progress callback function
187 bhy.setUpdateProcessCallback(progress_callback, NULL);
188
189 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
190 // bhy.setMaxiTransferSize(256);
191
192 // Set the processing fifo data buffer size,The default size is 512 bytes.
193 // bhy.setProcessBufferSize(1024);
194
195#ifdef USE_I2C_INTERFACE
196 // Using I2C interface
197 // BHI360_SLAVE_ADDRESS_L = 0x28
198 // BHI360_SLAVE_ADDRESS_H = 0x29
199 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
200 Serial.print("Failed to initialize sensor - error code:");
201 Serial.println(bhy.getError());
202 while (1) {
203 delay(1000);
204 }
205 }
206#endif
207
208#ifdef USE_SPI_INTERFACE
209 // Using SPI interface
210 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
211 Serial.print("Failed to initialize sensor - error code:");
212 Serial.println(bhy.getError());
213 while (1) {
214 delay(1000);
215 }
216 }
217#endif
218
219 Serial.println("Initializing the sensor successfully!");
220
221 // Output all sensors info to Serial
222 BoschSensorInfo info = bhy.getSensorInfo();
223
224#ifdef ARDUINO
225 ArduinoStreamPrinter printer(Serial);
226 info.printInfo(printer);
227#else
228 info.printInfo([](const char *format, ...) -> int {
229 va_list args;
230 va_start(args, format);
231 int result = vprintf(format, args);
232 va_end(args);
233 return result;
234 });
235#endif
236
237 /*
238 * Enable monitoring.
239 * sample_rate ​​can only control the rate of the pressure sensor.
240 * Temperature and humidity will only be updated when there is a change.
241 * * */
242 float sample_rate = 2.0; /* Set pressure sensor to 2Hz update frequency */
243 uint32_t report_latency_ms = 0; /* Report immediately */
244
245 /*
246 * Enable BME280 function
247 * Function depends on BME280.
248 * If the hardware is not connected to BME280, the function cannot be used.
249 * * */
250#ifdef USING_DATA_HELPER
251 temperature.enable();
252 humidity.enable();
253 pressure.enable(sample_rate, report_latency_ms);
254#else
255 bool rslt = false;
256 rslt = bhy.configure(BoschSensorID::TEMPERATURE, sample_rate, report_latency_ms);
257 printResult(BoschSensorID::TEMPERATURE, sample_rate, rslt);
258 rslt = bhy.configure(BoschSensorID::BAROMETER, sample_rate, report_latency_ms);
259 printResult(BoschSensorID::BAROMETER, sample_rate, rslt);
260 rslt = bhy.configure(BoschSensorID::HUMIDITY, sample_rate, report_latency_ms);
261 printResult(BoschSensorID::HUMIDITY, sample_rate, rslt);
262 // Register BME280 data parse callback function
263 Serial.println("Register sensor result callback function");
264 bhy.onResultEvent(BoschSensorID::TEMPERATURE, parse_sensor_data);
265 bhy.onResultEvent(BoschSensorID::HUMIDITY, parse_sensor_data);
266 bhy.onResultEvent(BoschSensorID::BAROMETER, parse_sensor_data);
267#endif
268
269#ifdef USING_SENSOR_IRQ_METHOD
270 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
271 // This makes the pin ready to receive external signals.
272 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
273 pinMode(BHI360_IRQ, INPUT);
274
275 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
276 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
277 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
278#endif
279
280}
281
282
283void loop()
284{
285 uint32_t s;
286 uint32_t ns;
287
288#ifdef USING_SENSOR_IRQ_METHOD
290 isInterruptTriggered = false;
291#endif /*USING_SENSOR_IRQ_METHOD*/
292
293 /* If the interrupt is connected to the sensor and BHI360_IRQ is not equal to -1,
294 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
295 */
296 bhy.update();
297
298#ifdef USING_SENSOR_IRQ_METHOD
299 }
300#endif /*USING_SENSOR_IRQ_METHOD*/
301
302#ifdef USING_DATA_HELPER
303 if (temperature.hasUpdated()) {
304 temperature.getLastTime(s, ns);
305#ifdef PLATFORM_HAS_PRINTF
306 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
307 Serial.printf("Temperature: %.2f *C %.2f *F %.2f K\n",
308 temperature.getCelsius(), temperature.getFahrenheit(), temperature.getKelvin());
309#else
310 Serial.print("[T: ");
311 Serial.print(s);
312 Serial.print(".");
313 Serial.print(ns);
314 Serial.print("]:");
315 Serial.print("Temperature: ");
316 Serial.print(temperature.getCelsius()); Serial.print(" *C");
317 Serial.print(temperature.getFahrenheit()); Serial.print(" *F");
318 Serial.print(temperature.getKelvin()); Serial.print(" *F");
319 Serial.println();
320#endif
321 }
322
323 if (humidity.hasUpdated()) {
324 humidity.getLastTime(s, ns);
325#ifdef PLATFORM_HAS_PRINTF
326 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
327 Serial.printf("Humidity: %.2f %%\n", humidity.getHumidity());
328#else
329 Serial.print("[T: ");
330 Serial.print(s);
331 Serial.print(".");
332 Serial.print(ns);
333 Serial.print("]:");
334 Serial.print("Humidity: ");
335 Serial.print(humidity.getHumidity()); Serial.print(" %");
336 Serial.println();
337#endif
338 }
339
340 if (pressure.hasUpdated()) {
341 pressure.getLastTime(s, ns);
342#ifdef PLATFORM_HAS_PRINTF
343 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] ", s, ns);
344 Serial.printf("Pressure: %.2f Pascal %.2f hPa %.2f kPa\n", pressure.getPressure(), pressure.getPressureHPa(), pressure.getPressureKPa());
345#else
346 Serial.print("[T: ");
347 Serial.print(s);
348 Serial.print(".");
349 Serial.print(ns);
350 Serial.print("]:");
351 Serial.print("Pressure: ");
352 Serial.print(pressure.getPressure()); Serial.print(" Pascal");
353 Serial.println();
354#endif
355 }
356#endif
357 delay(50);
358}
359
360
361
@license MIT License
@license MIT License
void parse_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
SensorTemperature temperature(bhy)
volatile bool isInterruptTriggered
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
SensorPressure pressure(bhy)
SensorHumidity humidity(bhy)