@@ -32,37 +32,49 @@ BLEService service(BLE_SENSE_UUID("0000"));
32
32
BLEUnsignedIntCharacteristic versionCharacteristic (BLE_SENSE_UUID(" 1001" ), BLERead);
33
33
BLEFloatCharacteristic temperatureCharacteristic (BLE_SENSE_UUID(" 2001" ), BLERead);
34
34
BLEUnsignedIntCharacteristic humidityCharacteristic (BLE_SENSE_UUID(" 3001" ), BLERead);
35
- BLEUnsignedIntCharacteristic pressureCharacteristic (BLE_SENSE_UUID(" 4001" ), BLERead);
35
+ BLEFloatCharacteristic pressureCharacteristic (BLE_SENSE_UUID(" 4001" ), BLERead);
36
36
37
- BLECharacteristic accelerometerCharacteristic (BLE_SENSE_UUID(" 5001" ), BLERead | BLENotify, 3 * sizeof(float )); // Array of 3 bytes, RGB
38
- BLECharacteristic gyroscopeCharacteristic (BLE_SENSE_UUID(" 6001" ), BLERead | BLENotify, 3 * sizeof(int16_t )); // Array of 3 bytes, RGB
37
+ BLECharacteristic accelerometerCharacteristic (BLE_SENSE_UUID(" 5001" ), BLERead | BLENotify, 3 * sizeof(float )); // Array of 3x 2 Bytes, XY
38
+ BLECharacteristic gyroscopeCharacteristic (BLE_SENSE_UUID(" 6001" ), BLERead | BLENotify, 3 * sizeof(int16_t )); // Array of 3x 2 Bytes, XYZ
39
+ BLECharacteristic quaternionCharacteristic (BLE_SENSE_UUID(" 7001" ), BLERead | BLENotify, 4 * sizeof(float )); // Array of 4x 2 Bytes, XYZW
39
40
40
- BLECharacteristic rgbLedCharacteristic (BLE_SENSE_UUID(" 7001" ), BLERead | BLEWrite, 3 * sizeof(byte)); // Array of 3 bytes, RGB
41
+ BLECharacteristic rgbLedCharacteristic (BLE_SENSE_UUID(" 8001" ), BLERead | BLEWrite, 3 * sizeof(byte)); // Array of 3 bytes, RGB
42
+
43
+ BLEFloatCharacteristic bsecCharacteristic (BLE_SENSE_UUID(" 9001" ), BLERead);
44
+ BLEIntCharacteristic co2Characteristic (BLE_SENSE_UUID(" 9002" ), BLERead);
45
+ BLEUnsignedIntCharacteristic gasCharacteristic (BLE_SENSE_UUID(" 9003" ), BLERead);
41
46
42
47
// String to calculate the local and device name
43
48
String name;
44
49
45
50
Sensor temperature (SENSOR_ID_TEMP);
46
51
Sensor humidity (SENSOR_ID_HUM);
47
52
Sensor pressure (SENSOR_ID_BARO);
53
+ Sensor gas (SENSOR_ID_GAS);
48
54
SensorXYZ gyroscope (SENSOR_ID_GYRO);
49
55
SensorXYZ accelerometer (SENSOR_ID_ACC);
56
+ SensorQuaternion quaternion (SENSOR_ID_RV);
57
+ SensorBSEC bsec (SENSOR_ID_BSEC);
50
58
51
59
void setup (){
52
60
Serial.begin (115200 );
53
61
54
- // while (!Serial);
55
- Serial.println (" Started" );
62
+ Serial.println (" Start" );
56
63
57
64
nicla::begin ();
58
65
nicla::leds.begin ();
59
66
nicla::leds.setColor (green);
67
+
68
+ // Sensors intialization
60
69
BHY2.begin ();
61
70
temperature.begin ();
62
71
humidity.begin ();
63
72
pressure.begin ();
64
73
gyroscope.begin ();
65
74
accelerometer.begin ();
75
+ quaternion.begin ();
76
+ bsec.begin ();
77
+ gas.begin ();
66
78
67
79
if (!BLE.begin ()){
68
80
Serial.println (" Failled to initialized BLE!" );
@@ -91,30 +103,38 @@ void setup(){
91
103
BLE.setDeviceName (name.c_str ());
92
104
BLE.setAdvertisedService (service);
93
105
106
+ // Add all the previously defined Characteristics
94
107
service.addCharacteristic (temperatureCharacteristic);
95
108
service.addCharacteristic (humidityCharacteristic);
96
109
service.addCharacteristic (pressureCharacteristic);
97
110
service.addCharacteristic (versionCharacteristic);
98
111
service.addCharacteristic (accelerometerCharacteristic);
99
112
service.addCharacteristic (gyroscopeCharacteristic);
113
+ service.addCharacteristic (quaternionCharacteristic);
114
+ service.addCharacteristic (bsecCharacteristic);
115
+ service.addCharacteristic (co2Characteristic);
116
+ service.addCharacteristic (gasCharacteristic);
100
117
service.addCharacteristic (rgbLedCharacteristic);
101
118
119
+ // Sensors event handlers
102
120
temperatureCharacteristic.setEventHandler (BLERead, onTemperatureCharacteristicRead);
103
121
humidityCharacteristic.setEventHandler (BLERead, onHumidityCharacteristicRead);
104
122
pressureCharacteristic.setEventHandler (BLERead, onPressureCharacteristicRead);
123
+ bsecCharacteristic.setEventHandler (BLERead, onBsecCharacteristicRead);
124
+ co2Characteristic.setEventHandler (BLERead, onCo2CharacteristicRead);
125
+ gasCharacteristic.setEventHandler (BLERead, onGasCharacteristicRead);
105
126
106
127
rgbLedCharacteristic.setEventHandler (BLEWritten, onRgbLedCharacteristicWrite);
107
128
108
129
versionCharacteristic.setValue (VERSION);
109
130
110
131
BLE.addService (service);
111
-
112
132
BLE.advertise ();
113
133
}
114
134
115
135
void loop (){
116
136
while (BLE.connected ()){
117
- BHY2.update (100 );
137
+ BHY2.update ();
118
138
119
139
if (gyroscopeCharacteristic.subscribed ()){
120
140
float x, y, z;
@@ -137,6 +157,18 @@ void loop(){
137
157
float accelerometerValues[] = {x, y, z};
138
158
accelerometerCharacteristic.writeValue (accelerometerValues, sizeof (accelerometerValues));
139
159
}
160
+
161
+ if (quaternionCharacteristic.subscribed ()){
162
+ float x, y, z, w;
163
+ x = quaternion.x ();
164
+ y = quaternion.y ();
165
+ z = quaternion.z ();
166
+ w = quaternion.w ();
167
+
168
+ float quaternionValues[] = {x,y,z,w};
169
+ quaternionCharacteristic.writeValue (quaternionValues, sizeof (quaternionValues));
170
+ }
171
+
140
172
}
141
173
}
142
174
@@ -151,10 +183,25 @@ void onHumidityCharacteristicRead(BLEDevice central, BLECharacteristic character
151
183
}
152
184
153
185
void onPressureCharacteristicRead (BLEDevice central, BLECharacteristic characteristic){
154
- uint8_t pressureValue = pressure.value ();
186
+ float pressureValue = pressure.value ();
155
187
pressureCharacteristic.writeValue (pressureValue);
156
188
}
157
189
190
+ void onBsecCharacteristicRead (BLEDevice central, BLECharacteristic characteristic){
191
+ float airQuality = float (bsec.iaq ());
192
+ bsecCharacteristic.writeValue (airQuality);
193
+ }
194
+
195
+ void onCo2CharacteristicRead (BLEDevice central, BLECharacteristic characteristic){
196
+ uint32_t co2 = bsec.co2_eq ();
197
+ co2Characteristic.writeValue (co2);
198
+ }
199
+
200
+ void onGasCharacteristicRead (BLEDevice central, BLECharacteristic characteristic){
201
+ unsigned int g = gas.value ();
202
+ gasCharacteristic.writeValue (g);
203
+ }
204
+
158
205
void onRgbLedCharacteristicWrite (BLEDevice central, BLECharacteristic characteristic){
159
206
byte r = rgbLedCharacteristic[0 ];
160
207
byte g = rgbLedCharacteristic[1 ];
0 commit comments