|
| 1 | +/********************************************************************* |
| 2 | + This is an example for our nRF52 based Bluefruit LE modules |
| 3 | +
|
| 4 | + Pick one up today in the adafruit shop! |
| 5 | +
|
| 6 | + Adafruit invests time and resources providing this open source code, |
| 7 | + please support Adafruit and open-source hardware by purchasing |
| 8 | + products from Adafruit! |
| 9 | +
|
| 10 | + MIT license, check LICENSE for more information |
| 11 | + All text above, and the splash screen below must be included in |
| 12 | + any redistribution |
| 13 | +*********************************************************************/ |
| 14 | + |
| 15 | +/* This sketch implement Arduino Science Journal firmware using Adafruit Bluefruit library |
| 16 | + * https://www.arduino.cc/education/science-journal. |
| 17 | + * Supported boards are: |
| 18 | + * - Circuit Playground Bluefruit : https://www.adafruit.com/product/4333 |
| 19 | + * - CLUE nRF52840 : https://www.adafruit.com/product/4500 |
| 20 | + * - Feather Sense : https://www.adafruit.com/product/4516 |
| 21 | + */ |
| 22 | + |
| 23 | +#include <bluefruit.h> |
| 24 | +#include <PDM.h> |
| 25 | + |
| 26 | +const int VERSION = 0x00000001; |
| 27 | +const float TEMPERATURE_CALIBRATION = -5.0; |
| 28 | + |
| 29 | +// 555a0002-val-467a-9538-01f0652c74e8" |
| 30 | +#define SCIENCE_KIT_UUID(val) \ |
| 31 | + (const uint8_t[]) { \ |
| 32 | + 0xe8, 0x74, 0x2c, 0x65, 0xf0, 0x01, 0x38, 0x95, \ |
| 33 | + 0x7a, 0x46, (uint8_t) (val & 0xff), (uint8_t) (val >> 8), 0x02, 0x00, 0x5a, 0x55 \ |
| 34 | + } |
| 35 | + |
| 36 | +BLEService service (SCIENCE_KIT_UUID(0x0000)); |
| 37 | +BLECharacteristic versionCharacteristic (SCIENCE_KIT_UUID(0x0001)); |
| 38 | +BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID(0x0011)); |
| 39 | +BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID(0x0012)); |
| 40 | +BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID(0x0013)); |
| 41 | +BLECharacteristic temperatureCharacteristic (SCIENCE_KIT_UUID(0x0014)); |
| 42 | +BLECharacteristic pressureCharacteristic (SCIENCE_KIT_UUID(0x0015)); |
| 43 | +BLECharacteristic humidityCharacteristic (SCIENCE_KIT_UUID(0x0016)); |
| 44 | +BLECharacteristic proximityCharacteristic (SCIENCE_KIT_UUID(0x0017)); |
| 45 | +BLECharacteristic colorCharacteristic (SCIENCE_KIT_UUID(0x0018)); |
| 46 | +BLECharacteristic soundPressureCharacteristic(SCIENCE_KIT_UUID(0x0019)); |
| 47 | + |
| 48 | + |
| 49 | +#if defined(ARDUINO_NRF52840_CIRCUITPLAY) |
| 50 | + |
| 51 | +#include <Adafruit_CircuitPlayground.h> |
| 52 | + |
| 53 | +#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) |
| 54 | + |
| 55 | +#include <Adafruit_APDS9960.h> |
| 56 | +#include <Adafruit_BMP280.h> |
| 57 | +#include <Adafruit_LIS3MDL.h> |
| 58 | +#include <Adafruit_LSM6DS33.h> |
| 59 | +#include <Adafruit_SHT31.h> |
| 60 | + |
| 61 | +Adafruit_LSM6DS33 lsm6ds33; // Gyro and Accel |
| 62 | +Adafruit_LIS3MDL lis3mdl; // Magnetometer |
| 63 | +Adafruit_APDS9960 apds9960; // Proximity, Light, Gesture, Color |
| 64 | +Adafruit_BMP280 bmp280; // Temperature, Barometric |
| 65 | +Adafruit_SHT31 sht30; // Humid |
| 66 | + |
| 67 | +#else |
| 68 | + |
| 69 | +#error "Board is not supported" |
| 70 | + |
| 71 | +#endif |
| 72 | + |
| 73 | +Adafruit_Sensor* accel_sensor; |
| 74 | + |
| 75 | +short soundSampleBuffer[256]; |
| 76 | + |
| 77 | +void onPDMdata() { |
| 78 | + // query the number of bytes available |
| 79 | + int bytesAvailable = PDM.available(); |
| 80 | + |
| 81 | + // read into the sample buffer |
| 82 | + PDM.read(soundSampleBuffer, bytesAvailable); |
| 83 | +} |
| 84 | + |
| 85 | +uint16_t getSoundAverage() { |
| 86 | + uint32_t avg = 0; |
| 87 | + for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) { |
| 88 | + avg += soundSampleBuffer[i]*soundSampleBuffer[i]; |
| 89 | + } |
| 90 | + return sqrt(avg); |
| 91 | +} |
| 92 | + |
| 93 | +void setupSensors(void) |
| 94 | +{ |
| 95 | +#if defined ARDUINO_NRF52840_CIRCUITPLAY |
| 96 | + CircuitPlayground.begin(); |
| 97 | + accel_sensor = &CircuitPlayground.lis; |
| 98 | + |
| 99 | +#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) |
| 100 | + |
| 101 | + #ifdef ARDUINO_NRF52840_CLUE |
| 102 | + // White LEDs for color sensing |
| 103 | + pinMode(PIN_LED2, OUTPUT); |
| 104 | + digitalWrite(PIN_LED2, LOW); |
| 105 | + #endif |
| 106 | + |
| 107 | + apds9960.begin(); |
| 108 | + bmp280.begin(); |
| 109 | + sht30.begin(0x44); |
| 110 | + lsm6ds33.begin_I2C(); |
| 111 | + lis3mdl.begin_I2C(); |
| 112 | + |
| 113 | + // set lowest range |
| 114 | + lsm6ds33.setAccelRange(LSM6DS_ACCEL_RANGE_2_G); |
| 115 | + lsm6ds33.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS); |
| 116 | + lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS); |
| 117 | + |
| 118 | + // set slightly above refresh rate |
| 119 | + lsm6ds33.setAccelDataRate(LSM6DS_RATE_104_HZ); |
| 120 | + lsm6ds33.setGyroDataRate(LSM6DS_RATE_104_HZ); |
| 121 | + lis3mdl.setDataRate(LIS3MDL_DATARATE_1000_HZ); |
| 122 | + lis3mdl.setPerformanceMode(LIS3MDL_MEDIUMMODE); |
| 123 | + lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE); |
| 124 | + |
| 125 | + // Increase I2C speed to 400 Khz |
| 126 | + Wire.setClock(400000); |
| 127 | + |
| 128 | + accel_sensor = lsm6ds33.getAccelerometerSensor(); |
| 129 | +#endif |
| 130 | + |
| 131 | + // 1 channel (mono mode) with 16 kHz sample rate |
| 132 | + PDM.onReceive(onPDMdata); |
| 133 | + PDM.begin(1, 16000); |
| 134 | +} |
| 135 | + |
| 136 | +void setupBLEScience(void) |
| 137 | +{ |
| 138 | + service.begin(); |
| 139 | + |
| 140 | + versionCharacteristic.setProperties(CHR_PROPS_READ); |
| 141 | + versionCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 142 | + versionCharacteristic.setFixedLen(4); |
| 143 | + versionCharacteristic.begin(); |
| 144 | + versionCharacteristic.write32(VERSION); |
| 145 | + |
| 146 | + accelerationCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 147 | + accelerationCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 148 | + accelerationCharacteristic.setFixedLen(3 * sizeof(float)); |
| 149 | + accelerationCharacteristic.begin(); |
| 150 | + |
| 151 | +#if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) |
| 152 | + gyroscopeCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 153 | + gyroscopeCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 154 | + gyroscopeCharacteristic.setFixedLen(3 * sizeof(float)); |
| 155 | + gyroscopeCharacteristic.begin(); |
| 156 | + |
| 157 | + magneticFieldCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 158 | + magneticFieldCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 159 | + magneticFieldCharacteristic.setFixedLen(3 * sizeof(float)); |
| 160 | + magneticFieldCharacteristic.begin(); |
| 161 | + |
| 162 | + pressureCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 163 | + pressureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 164 | + pressureCharacteristic.setFixedLen(sizeof(float)); |
| 165 | + pressureCharacteristic.begin(); |
| 166 | + |
| 167 | + humidityCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 168 | + humidityCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 169 | + humidityCharacteristic.setFixedLen(sizeof(float)); |
| 170 | + humidityCharacteristic.begin(); |
| 171 | + |
| 172 | + proximityCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 173 | + proximityCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 174 | + proximityCharacteristic.setFixedLen(sizeof(unsigned int)); |
| 175 | + proximityCharacteristic.setCccdWriteCallback(science_notify_callback); |
| 176 | + proximityCharacteristic.begin(); |
| 177 | +#endif |
| 178 | + |
| 179 | + temperatureCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 180 | + temperatureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 181 | + temperatureCharacteristic.setFixedLen(sizeof(float)); |
| 182 | + temperatureCharacteristic.begin(); |
| 183 | + |
| 184 | + colorCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 185 | + colorCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 186 | + colorCharacteristic.setFixedLen(4 * sizeof(int)); |
| 187 | + colorCharacteristic.setCccdWriteCallback(science_notify_callback); |
| 188 | + colorCharacteristic.begin(); |
| 189 | + |
| 190 | + soundPressureCharacteristic.setProperties(CHR_PROPS_NOTIFY); |
| 191 | + soundPressureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); |
| 192 | + soundPressureCharacteristic.setFixedLen(sizeof(unsigned short)); |
| 193 | + soundPressureCharacteristic.begin(); |
| 194 | +} |
| 195 | + |
| 196 | +void setup() |
| 197 | +{ |
| 198 | + Serial.begin(115200); |
| 199 | + Serial.println("Bluefruit Science Journal Example"); |
| 200 | + Serial.println("---------------------------------\n"); |
| 201 | + |
| 202 | + setupSensors(); |
| 203 | + |
| 204 | + // Config the peripheral connection with maximum bandwidth |
| 205 | + // more SRAM required by SoftDevice |
| 206 | + // Note: All config***() function must be called before begin() |
| 207 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 208 | + Bluefruit.configUuid128Count(15); |
| 209 | + |
| 210 | + Bluefruit.begin(); |
| 211 | + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values |
| 212 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 213 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 214 | + |
| 215 | + setupBLEScience(); |
| 216 | + |
| 217 | + // Set up and start advertising |
| 218 | + startAdv(); |
| 219 | +} |
| 220 | + |
| 221 | +void startAdv(void) |
| 222 | +{ |
| 223 | + // Advertising packet |
| 224 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 225 | + Bluefruit.Advertising.addTxPower(); |
| 226 | + |
| 227 | + // Include bleuart 128-bit uuid |
| 228 | + Bluefruit.Advertising.addService(service); |
| 229 | + |
| 230 | + // Secondary Scan Response packet (optional) |
| 231 | + // Since there is no room for 'Name' in Advertising packet |
| 232 | + Bluefruit.ScanResponse.addName(); |
| 233 | + |
| 234 | + /* Start Advertising |
| 235 | + * - Enable auto advertising if disconnected |
| 236 | + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 237 | + * - Timeout for fast mode is 30 seconds |
| 238 | + * - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 239 | + * |
| 240 | + * For recommended advertising interval |
| 241 | + * https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 242 | + */ |
| 243 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 244 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 245 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 246 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 247 | +} |
| 248 | + |
| 249 | +void loop() |
| 250 | +{ |
| 251 | + if ( Bluefruit.connected() ) |
| 252 | + { |
| 253 | + updateSubscribedCharacteristics(); |
| 254 | + delay(1000); |
| 255 | + } |
| 256 | +} |
| 257 | + |
| 258 | +void updateSubscribedCharacteristics(void) |
| 259 | +{ |
| 260 | + sensors_event_t event; |
| 261 | + |
| 262 | + if ( accelerationCharacteristic.notifyEnabled() ) |
| 263 | + { |
| 264 | + accel_sensor->getEvent(&event); |
| 265 | + accelerationCharacteristic.notify(event.data, accelerationCharacteristic.getMaxLen()); |
| 266 | + } |
| 267 | + |
| 268 | +#ifdef ARDUINO_NRF52840_CIRCUITPLAY |
| 269 | + if ( temperatureCharacteristic.notifyEnabled() ) |
| 270 | + { |
| 271 | + float temperature = CircuitPlayground.temperature(); |
| 272 | + temperatureCharacteristic.notify32(temperature); |
| 273 | + } |
| 274 | + |
| 275 | + if ( colorCharacteristic.notifyEnabled() ) |
| 276 | + { |
| 277 | + int color[4] = { 0 }; |
| 278 | + color[3] = CircuitPlayground.lightSensor(); |
| 279 | + colorCharacteristic.notify(color, colorCharacteristic.getMaxLen()); |
| 280 | + } |
| 281 | + |
| 282 | +#elif defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) |
| 283 | + if ( gyroscopeCharacteristic.notifyEnabled() ) |
| 284 | + { |
| 285 | + lsm6ds33.getGyroSensor()->getEvent(&event); |
| 286 | + |
| 287 | + // Convert gyro from Rad/s to Degree/s |
| 288 | + event.gyro.x *= SENSORS_RADS_TO_DPS; |
| 289 | + event.gyro.y *= SENSORS_RADS_TO_DPS; |
| 290 | + event.gyro.z *= SENSORS_RADS_TO_DPS; |
| 291 | + |
| 292 | + gyroscopeCharacteristic.notify(event.data, gyroscopeCharacteristic.getMaxLen()); |
| 293 | + } |
| 294 | + |
| 295 | + if ( magneticFieldCharacteristic.notifyEnabled() ) |
| 296 | + { |
| 297 | + lis3mdl.getEvent(&event); |
| 298 | + magneticFieldCharacteristic.notify(event.data, magneticFieldCharacteristic.getMaxLen()); |
| 299 | + } |
| 300 | + |
| 301 | + if ( proximityCharacteristic.notifyEnabled() ) |
| 302 | + { |
| 303 | + uint32_t proximity = 255 - apds9960.readProximity(); |
| 304 | + proximityCharacteristic.notify32(proximity); |
| 305 | + } |
| 306 | + |
| 307 | + if ( temperatureCharacteristic.notifyEnabled() || humidityCharacteristic.notifyEnabled() ) |
| 308 | + { |
| 309 | + float temperature = bmp280.readTemperature(); |
| 310 | + float temperatureCalibrated = temperature + TEMPERATURE_CALIBRATION; |
| 311 | + |
| 312 | + if ( temperatureCharacteristic.notifyEnabled() ) |
| 313 | + { |
| 314 | + temperatureCharacteristic.notify32(temperatureCalibrated); |
| 315 | + } |
| 316 | + |
| 317 | + if ( humidityCharacteristic.notifyEnabled() ) |
| 318 | + { |
| 319 | + float humidity = sht30.readHumidity(); |
| 320 | + float dp = temperature - ((100.0 - humidity) / 5.0); |
| 321 | + float humidityCalibrated = 100.0 - (5.0 * (temperatureCalibrated - dp)); |
| 322 | + humidityCharacteristic.notify32(humidityCalibrated); |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + if ( pressureCharacteristic.notifyEnabled() ) |
| 327 | + { |
| 328 | + float pressure = bmp280.readPressure() / 1000.0; // kilo pascal |
| 329 | + pressureCharacteristic.notify32(pressure); |
| 330 | + } |
| 331 | + |
| 332 | + if ( colorCharacteristic.notifyEnabled() ) |
| 333 | + { |
| 334 | + int color[4] = { 0 }; |
| 335 | + apds9960.getColorData((uint16_t*) &color[0], (uint16_t*) &color[1], (uint16_t*) &color[2], (uint16_t*) &color[3]); |
| 336 | + colorCharacteristic.notify(color, colorCharacteristic.getMaxLen()); |
| 337 | + } |
| 338 | +#endif |
| 339 | + |
| 340 | + if ( soundPressureCharacteristic.notifyEnabled() ) |
| 341 | + { |
| 342 | + uint16_t sound = getSoundAverage(); |
| 343 | + soundPressureCharacteristic.notify16(sound); |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +void science_notify_callback(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value) |
| 348 | +{ |
| 349 | + (void) conn_hdl; |
| 350 | + |
| 351 | + bool enabled = (value == 0x0001); |
| 352 | + (void) enabled; |
| 353 | + |
| 354 | +#if defined(ARDUINO_NRF52840_CLUE) || defined(ARDUINO_NRF52840_FEATHER_SENSE) |
| 355 | + if ( chr == &colorCharacteristic ) |
| 356 | + { |
| 357 | + apds9960.enableColor(enabled); |
| 358 | + |
| 359 | +#ifdef ARDUINO_NRF52840_CLUE |
| 360 | + digitalWrite(PIN_LED2, enabled); |
| 361 | +#endif |
| 362 | + } |
| 363 | + |
| 364 | + if ( chr == &proximityCharacteristic) |
| 365 | + { |
| 366 | + apds9960.enableProximity(enabled); |
| 367 | + } |
| 368 | +#endif |
| 369 | +} |
| 370 | + |
| 371 | +// callback invoked when central connects |
| 372 | +void connect_callback(uint16_t conn_handle) |
| 373 | +{ |
| 374 | + // Get the reference to current connection |
| 375 | + BLEConnection* connection = Bluefruit.Connection(conn_handle); |
| 376 | + |
| 377 | + char central_name[32] = { 0 }; |
| 378 | + connection->getPeerName(central_name, sizeof(central_name)); |
| 379 | + |
| 380 | + Serial.print("Connected to "); |
| 381 | + Serial.println(central_name); |
| 382 | +} |
| 383 | + |
| 384 | +/** |
| 385 | + * Callback invoked when a connection is dropped |
| 386 | + * @param conn_handle connection where this event happens |
| 387 | + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h |
| 388 | + */ |
| 389 | +void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 390 | +{ |
| 391 | + (void) conn_handle; |
| 392 | + (void) reason; |
| 393 | + |
| 394 | + Serial.println(); |
| 395 | + Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX); |
| 396 | +} |
0 commit comments