SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi360_aux_bmm350_bme688_iaq.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
97SensorIAQ air_quality(bhy);
98#endif
99
100
101#define BOSCH_SHUTTLE3_BHI360_BMM350C_BME688_IAQ
102
103#include <BoschFirmware.h>
104
105
106#if BHI360_IRQ > 0
107#define USING_SENSOR_IRQ_METHOD
108#endif
109
110#ifdef USING_SENSOR_IRQ_METHOD
111volatile bool isInterruptTriggered = false;
112
114{
116}
117#endif /*USING_SENSOR_IRQ_METHOD*/
118
119#ifndef USING_DATA_HELPER
120void parse_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
121{
122 float humidity = 0;
123 float temperature = 0;
124 float pressure = 0;
125 switch (sensor_id) {
126 case BoschSensorID::HUMIDITY:
127 bhy2_parse_humidity(data_ptr, &humidity);
128 Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
129 break;
130 case BoschSensorID::TEMPERATURE:
131 bhy2_parse_temperature_celsius(data_ptr, &temperature);
132 Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
133 break;
134 case BoschSensorID::BAROMETER:
135 bhy2_parse_pressure(data_ptr, &pressure);
136 Serial.print("pressure:"); Serial.print(pressure); Serial.println("Pa");
137 break;
138 case BoschSensorID::IAQ: {
139 /*SCALE FACTOR from BHI3 data sheet, IAQ data format*/
140 const float SCALE_IAQ_VOC = 100.0;
141 const float SCALE_IAQ_TEMP = 256.0;
142 const float SCALE_IAQ_HUMI = 500.0;
143 bhi360_event_data_iaq_output_t data{};
144 bhi360_event_data_parse_air_quality(data_ptr, &data);
145 Serial.print("ACCU:"); Serial.print(data.iaq_accuracy); Serial.print(" ");
146 Serial.print("IAQ:"); Serial.print(data.iaq); Serial.print(" ");
147 Serial.print("SIAQ:"); Serial.print(data.siaq); Serial.print(" ");
148 Serial.print("VOC:"); Serial.print(data.voc / SCALE_IAQ_VOC); Serial.print("ppm ");
149 Serial.print("CO2:"); Serial.print(data.co2); Serial.print("ppm ");
150 Serial.print("Temperature:"); Serial.print(data.comp_temperature / SCALE_IAQ_TEMP); Serial.print("*C ");
151 Serial.print("Humidity:"); Serial.print(data.comp_humidity / SCALE_IAQ_HUMI); Serial.print("% ");
152 Serial.print("GAS:"); Serial.print(data.raw_gas); Serial.println(" ohms ");
153 }
154 break;
155 default:
156 Serial.println("Unkown.");
157 break;
158 }
159}
160#endif
161
162// Firmware update progress callback
163void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
164{
165 float progress = (float)transferred / total * 100;
166 Serial.print("Upload progress: ");
167 Serial.print(progress);
168 Serial.println("%");
169}
170
171void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
172{
173 const char *sensorName = bhy.getSensorName(sensor_id);
174 Serial.print("Configure ");
175 Serial.print(sensorName);
176 Serial.print(" sensor ");
177 Serial.print(sample_rate, 2);
178 Serial.print(" HZ ");
179 if (rslt) {
180 Serial.print("successfully");
181 } else {
182 Serial.print("failed");
183 }
184 Serial.println();
185}
186
187
188void setup()
189{
190 Serial.begin(115200);
191 while (!Serial);
192
193 // Set the reset pin
194 bhy.setPins(BHI360_RST);
195
196 Serial.println("Initializing Sensors...");
197
198 // Set the firmware array address and firmware size
199 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size);
200
201 // Set the firmware update processing progress callback function
202 bhy.setUpdateProcessCallback(progress_callback, NULL);
203
204 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
205 // bhy.setMaxiTransferSize(256);
206
207 // Set the processing fifo data buffer size,The default size is 512 bytes.
208 // bhy.setProcessBufferSize(1024);
209
210#ifdef USE_I2C_INTERFACE
211 // Using I2C interface
212 // BHI360_SLAVE_ADDRESS_L = 0x28
213 // BHI360_SLAVE_ADDRESS_H = 0x29
214 if (!bhy.begin(Wire, BHI360_SLAVE_ADDRESS_L, BHI360_SDA, BHI360_SCL)) {
215 Serial.print("Failed to initialize sensor - error code:");
216 Serial.println(bhy.getError());
217 while (1) {
218 delay(1000);
219 }
220 }
221#endif
222
223#ifdef USE_SPI_INTERFACE
224 // Using SPI interface
225 if (!bhy.begin(SPI, BHI360_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
226 Serial.print("Failed to initialize sensor - error code:");
227 Serial.println(bhy.getError());
228 while (1) {
229 delay(1000);
230 }
231 }
232#endif
233
234 Serial.println("Initializing the sensor successfully!");
235
236 // Output all sensors info to Serial
237 BoschSensorInfo info = bhy.getSensorInfo();
238
239#ifdef ARDUINO
240 ArduinoStreamPrinter printer(Serial);
241 info.printInfo(printer);
242#else
243 info.printInfo([](const char *format, ...) -> int {
244 va_list args;
245 va_start(args, format);
246 int result = vprintf(format, args);
247 va_end(args);
248 return result;
249 });
250#endif
251
252 /*
253 * Enable BME688 function
254 * Function depends on BME688.
255 * If the hardware is not connected to BME688, the function cannot be used.
256 * * */
257#ifdef USING_DATA_HELPER
258 air_quality.enable();
259#else
260
261 float sample_rate = 1.0; // Invalid param,iaq sensor only be updated when there is a change.
262 uint32_t report_latency_ms = 0; // Invalid param,iaq sensor only be updated when there is a change.
263 bool rslt = false;
264 rslt = bhy.configure(BoschSensorID::IAQ, sample_rate, report_latency_ms);
265 printResult(BoschSensorID::IAQ, sample_rate, rslt);
266
267 // Register BME688 data parse callback function
268 bhy.onResultEvent(BoschSensorID::IAQ, parse_sensor_data);
269#endif
270
271#ifdef USING_SENSOR_IRQ_METHOD
272 // Set the specified pin (BHI360_IRQ) ​​to an input pin.
273 // This makes the pin ready to receive external signals.
274 // If the interrupt is already connected, if BHI360_IRQ is equal to -1 then the polling method will be used
275 pinMode(BHI360_IRQ, INPUT);
276
277 // Attach an interrupt service routine (ISR) to the specified pin (BHI360_IRQ).
278 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
279 attachInterrupt(BHI360_IRQ, dataReadyISR, RISING);
280#endif
281
282}
283
284
285void loop()
286{
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 uint32_t s;
304 uint32_t ns;
305 if (air_quality.hasUpdated()) {
306 air_quality.getLastTime(s, ns);
307 Serial.print("[T: ");
308 Serial.print(s);
309 Serial.print(".");
310 Serial.print(ns);
311 Serial.print("]:");
312 Serial.print("TEMP:"); Serial.print(air_quality.getCelsius()); Serial.print("°C");
313 Serial.print("ACCU:"); Serial.print(air_quality.getIAQAccuracy()); Serial.print(" ");
314 Serial.print("IAQ:"); Serial.print(air_quality.getIAQ()); Serial.print(" ");
315 Serial.print("SIAQ:"); Serial.print(air_quality.getSIAQ()); Serial.print(" ");
316 Serial.print("VOC:"); Serial.print(air_quality.getVOC()); Serial.print("ppm ");
317 Serial.print("CO2:"); Serial.print(air_quality.getCO2()); Serial.print("ppm ");
318 Serial.print("HUMI:"); Serial.println(air_quality.getHumidity());
319 Serial.print("GAS:"); Serial.print(air_quality.getRawGas()); Serial.print(" ohms ");
320 Serial.println();
321 }
322#endif
323 delay(50);
324}
325
326
327
@license MIT License
@license MIT License
SensorTemperature temperature(bhy)
SensorPressure pressure(bhy)
SensorHumidity humidity(bhy)
void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
SensorBHI360 bhy
volatile bool isInterruptTriggered
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
void parse_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)