Skip to content

Commit aaa3a78

Browse files
committed
Update Sketch adding more sensors
1 parent 0f1f292 commit aaa3a78

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

NiclaSenseME-dashboard/NiclaSenseME/NiclaSenseME.ino

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,49 @@ BLEService service(BLE_SENSE_UUID("0000"));
3232
BLEUnsignedIntCharacteristic versionCharacteristic(BLE_SENSE_UUID("1001"), BLERead);
3333
BLEFloatCharacteristic temperatureCharacteristic(BLE_SENSE_UUID("2001"), BLERead);
3434
BLEUnsignedIntCharacteristic humidityCharacteristic(BLE_SENSE_UUID("3001"), BLERead);
35-
BLEUnsignedIntCharacteristic pressureCharacteristic(BLE_SENSE_UUID("4001"), BLERead);
35+
BLEFloatCharacteristic pressureCharacteristic(BLE_SENSE_UUID("4001"), BLERead);
3636

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
3940

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);
4146

4247
// String to calculate the local and device name
4348
String name;
4449

4550
Sensor temperature(SENSOR_ID_TEMP);
4651
Sensor humidity(SENSOR_ID_HUM);
4752
Sensor pressure(SENSOR_ID_BARO);
53+
Sensor gas(SENSOR_ID_GAS);
4854
SensorXYZ gyroscope(SENSOR_ID_GYRO);
4955
SensorXYZ accelerometer(SENSOR_ID_ACC);
56+
SensorQuaternion quaternion(SENSOR_ID_RV);
57+
SensorBSEC bsec(SENSOR_ID_BSEC);
5058

5159
void setup(){
5260
Serial.begin(115200);
5361

54-
// while (!Serial);
55-
Serial.println("Started");
62+
Serial.println("Start");
5663

5764
nicla::begin();
5865
nicla::leds.begin();
5966
nicla::leds.setColor(green);
67+
68+
//Sensors intialization
6069
BHY2.begin();
6170
temperature.begin();
6271
humidity.begin();
6372
pressure.begin();
6473
gyroscope.begin();
6574
accelerometer.begin();
75+
quaternion.begin();
76+
bsec.begin();
77+
gas.begin();
6678

6779
if (!BLE.begin()){
6880
Serial.println("Failled to initialized BLE!");
@@ -91,30 +103,38 @@ void setup(){
91103
BLE.setDeviceName(name.c_str());
92104
BLE.setAdvertisedService(service);
93105

106+
// Add all the previously defined Characteristics
94107
service.addCharacteristic(temperatureCharacteristic);
95108
service.addCharacteristic(humidityCharacteristic);
96109
service.addCharacteristic(pressureCharacteristic);
97110
service.addCharacteristic(versionCharacteristic);
98111
service.addCharacteristic(accelerometerCharacteristic);
99112
service.addCharacteristic(gyroscopeCharacteristic);
113+
service.addCharacteristic(quaternionCharacteristic);
114+
service.addCharacteristic(bsecCharacteristic);
115+
service.addCharacteristic(co2Characteristic);
116+
service.addCharacteristic(gasCharacteristic);
100117
service.addCharacteristic(rgbLedCharacteristic);
101118

119+
// Sensors event handlers
102120
temperatureCharacteristic.setEventHandler(BLERead, onTemperatureCharacteristicRead);
103121
humidityCharacteristic.setEventHandler(BLERead, onHumidityCharacteristicRead);
104122
pressureCharacteristic.setEventHandler(BLERead, onPressureCharacteristicRead);
123+
bsecCharacteristic.setEventHandler(BLERead, onBsecCharacteristicRead);
124+
co2Characteristic.setEventHandler(BLERead, onCo2CharacteristicRead);
125+
gasCharacteristic.setEventHandler(BLERead, onGasCharacteristicRead);
105126

106127
rgbLedCharacteristic.setEventHandler(BLEWritten, onRgbLedCharacteristicWrite);
107128

108129
versionCharacteristic.setValue(VERSION);
109130

110131
BLE.addService(service);
111-
112132
BLE.advertise();
113133
}
114134

115135
void loop(){
116136
while (BLE.connected()){
117-
BHY2.update(100);
137+
BHY2.update();
118138

119139
if (gyroscopeCharacteristic.subscribed()){
120140
float x, y, z;
@@ -137,6 +157,18 @@ void loop(){
137157
float accelerometerValues[] = {x, y, z};
138158
accelerometerCharacteristic.writeValue(accelerometerValues, sizeof(accelerometerValues));
139159
}
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+
140172
}
141173
}
142174

@@ -151,10 +183,25 @@ void onHumidityCharacteristicRead(BLEDevice central, BLECharacteristic character
151183
}
152184

153185
void onPressureCharacteristicRead(BLEDevice central, BLECharacteristic characteristic){
154-
uint8_t pressureValue = pressure.value();
186+
float pressureValue = pressure.value();
155187
pressureCharacteristic.writeValue(pressureValue);
156188
}
157189

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+
158205
void onRgbLedCharacteristicWrite(BLEDevice central, BLECharacteristic characteristic){
159206
byte r = rgbLedCharacteristic[0];
160207
byte g = rgbLedCharacteristic[1];

0 commit comments

Comments
 (0)