Skip to content

Commit fa16e48

Browse files
committed
add Arduino Science Journal sketch
work with feather nrf52840 sense
1 parent 3cb7236 commit fa16e48

File tree

1 file changed

+357
-0
lines changed

1 file changed

+357
-0
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
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+
#define SCIENCE_KIT_UUID(val) \
30+
(const uint8_t[]) { \
31+
0xe8, 0x74, 0x2c, 0x65, 0xf0, 0x01, 0x38, 0x95, \
32+
0x7a, 0x46, (uint8_t) (val & 0xff), (uint8_t) (val >> 8), 0x02, 0x00, 0x5a, 0x55 \
33+
}
34+
35+
BLEService service (SCIENCE_KIT_UUID(0x0000));
36+
BLECharacteristic versionCharacteristic (SCIENCE_KIT_UUID(0x0001));
37+
BLECharacteristic accelerationCharacteristic (SCIENCE_KIT_UUID(0x0011));
38+
BLECharacteristic gyroscopeCharacteristic (SCIENCE_KIT_UUID(0x0012));
39+
BLECharacteristic magneticFieldCharacteristic(SCIENCE_KIT_UUID(0x0013));
40+
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+
#endif
68+
69+
Adafruit_Sensor* accel_sensor;
70+
71+
short soundSampleBuffer[256];
72+
73+
void onPDMdata() {
74+
// query the number of bytes available
75+
int bytesAvailable = PDM.available();
76+
77+
// read into the sample buffer
78+
PDM.read(soundSampleBuffer, bytesAvailable);
79+
}
80+
81+
uint16_t getSoundAverage() {
82+
uint32_t avg = 0;
83+
for (int i = 0; i < sizeof(soundSampleBuffer)/sizeof(soundSampleBuffer[0]); i++) {
84+
avg += soundSampleBuffer[i]*soundSampleBuffer[i];
85+
}
86+
return sqrt(avg);
87+
}
88+
89+
void setupSensors(void)
90+
{
91+
#if defined ARDUINO_NRF52840_CIRCUITPLAY
92+
CircuitPlayground.begin();
93+
accel_sensor = &CircuitPlayground.lis;
94+
95+
#else
96+
97+
#ifdef ARDUINO_NRF52840_CLUE
98+
// White LEDs for color sensing
99+
pinMode(PIN_LED2, OUTPUT);
100+
digitalWrite(PIN_LED2, LOW);
101+
#endif
102+
103+
apds9960.begin();
104+
apds9960.enableColor(true);
105+
apds9960.enableProximity(true);
106+
107+
bmp280.begin();
108+
sht30.begin(0x44);
109+
lsm6ds33.begin_I2C();
110+
lis3mdl.begin_I2C();
111+
112+
// set lowest range
113+
lsm6ds33.setAccelRange(LSM6DS_ACCEL_RANGE_2_G);
114+
lsm6ds33.setGyroRange(LSM6DS_GYRO_RANGE_250_DPS);
115+
lis3mdl.setRange(LIS3MDL_RANGE_4_GAUSS);
116+
117+
// set slightly above refresh rate
118+
lsm6ds33.setAccelDataRate(LSM6DS_RATE_104_HZ);
119+
lsm6ds33.setGyroDataRate(LSM6DS_RATE_104_HZ);
120+
lis3mdl.setDataRate(LIS3MDL_DATARATE_1000_HZ);
121+
lis3mdl.setPerformanceMode(LIS3MDL_MEDIUMMODE);
122+
lis3mdl.setOperationMode(LIS3MDL_CONTINUOUSMODE);
123+
124+
// Increase I2C speed to 400 Khz
125+
Wire.setClock(400000);
126+
127+
accel_sensor = lsm6ds33.getAccelerometerSensor();
128+
#endif
129+
130+
// 1 channel (mono mode) with 16 kHz sample rate
131+
PDM.onReceive(onPDMdata);
132+
PDM.begin(1, 16000);
133+
}
134+
135+
void setupBLEScience(void)
136+
{
137+
// Service
138+
service.begin();
139+
140+
// Version
141+
versionCharacteristic.setProperties(CHR_PROPS_READ);
142+
versionCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
143+
versionCharacteristic.setFixedLen(4);
144+
versionCharacteristic.begin();
145+
versionCharacteristic.write32(VERSION);
146+
147+
accelerationCharacteristic.setProperties(CHR_PROPS_NOTIFY);
148+
accelerationCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
149+
accelerationCharacteristic.setFixedLen(3 * sizeof(float));
150+
accelerationCharacteristic.begin();
151+
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+
temperatureCharacteristic.setProperties(CHR_PROPS_NOTIFY);
163+
temperatureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
164+
temperatureCharacteristic.setFixedLen(sizeof(float));
165+
temperatureCharacteristic.begin();
166+
167+
pressureCharacteristic.setProperties(CHR_PROPS_NOTIFY);
168+
pressureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
169+
pressureCharacteristic.setFixedLen(sizeof(float));
170+
pressureCharacteristic.begin();
171+
172+
humidityCharacteristic.setProperties(CHR_PROPS_NOTIFY);
173+
humidityCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
174+
humidityCharacteristic.setFixedLen(sizeof(float));
175+
humidityCharacteristic.begin();
176+
177+
proximityCharacteristic.setProperties(CHR_PROPS_NOTIFY);
178+
proximityCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
179+
proximityCharacteristic.setFixedLen(sizeof(unsigned int));
180+
proximityCharacteristic.begin();
181+
182+
colorCharacteristic.setProperties(CHR_PROPS_NOTIFY);
183+
colorCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
184+
colorCharacteristic.setFixedLen(4 * sizeof(int));
185+
colorCharacteristic.begin();
186+
187+
soundPressureCharacteristic.setProperties(CHR_PROPS_NOTIFY);
188+
soundPressureCharacteristic.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS);
189+
soundPressureCharacteristic.setFixedLen(sizeof(unsigned short));
190+
soundPressureCharacteristic.begin();
191+
}
192+
193+
void setup()
194+
{
195+
Serial.begin(115200);
196+
Serial.println("Bluefruit Science Journal Example");
197+
Serial.println("---------------------------------\n");
198+
199+
setupSensors();
200+
201+
// Config the peripheral connection with maximum bandwidth
202+
// more SRAM required by SoftDevice
203+
// Note: All config***() function must be called before begin()
204+
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
205+
Bluefruit.configUuid128Count(15);
206+
207+
Bluefruit.begin();
208+
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
209+
Bluefruit.Periph.setConnectCallback(connect_callback);
210+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
211+
212+
setupBLEScience();
213+
214+
// Set up and start advertising
215+
startAdv();
216+
}
217+
218+
void startAdv(void)
219+
{
220+
// Advertising packet
221+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
222+
Bluefruit.Advertising.addTxPower();
223+
224+
// Include bleuart 128-bit uuid
225+
Bluefruit.Advertising.addService(service);
226+
227+
// Secondary Scan Response packet (optional)
228+
// Since there is no room for 'Name' in Advertising packet
229+
Bluefruit.ScanResponse.addName();
230+
231+
/* Start Advertising
232+
* - Enable auto advertising if disconnected
233+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
234+
* - Timeout for fast mode is 30 seconds
235+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
236+
*
237+
* For recommended advertising interval
238+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
239+
*/
240+
Bluefruit.Advertising.restartOnDisconnect(true);
241+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
242+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
243+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
244+
}
245+
246+
void loop()
247+
{
248+
if ( Bluefruit.connected() )
249+
{
250+
updateSubscribedCharacteristics();
251+
delay(1000);
252+
}
253+
}
254+
255+
void updateSubscribedCharacteristics(void)
256+
{
257+
sensors_event_t event;
258+
259+
if ( accelerationCharacteristic.notifyEnabled() )
260+
{
261+
accel_sensor->getEvent(&event);
262+
accelerationCharacteristic.notify(event.data, accelerationCharacteristic.getMaxLen());
263+
}
264+
265+
if ( gyroscopeCharacteristic.notifyEnabled() )
266+
{
267+
lsm6ds33.getGyroSensor()->getEvent(&event);
268+
269+
// Convert gyro from Rad/s to Degree/s
270+
event.gyro.x *= SENSORS_RADS_TO_DPS;
271+
event.gyro.y *= SENSORS_RADS_TO_DPS;
272+
event.gyro.z *= SENSORS_RADS_TO_DPS;
273+
274+
gyroscopeCharacteristic.notify(event.data, gyroscopeCharacteristic.getMaxLen());
275+
}
276+
277+
if ( magneticFieldCharacteristic.notifyEnabled() )
278+
{
279+
lis3mdl.getEvent(&event);
280+
magneticFieldCharacteristic.notify(event.data, magneticFieldCharacteristic.getMaxLen());
281+
}
282+
283+
if ( soundPressureCharacteristic.notifyEnabled() )
284+
{
285+
uint16_t sound = getSoundAverage();
286+
soundPressureCharacteristic.notify16(sound);
287+
}
288+
289+
if ( proximityCharacteristic.notifyEnabled() )
290+
{
291+
uint32_t proximity = 255 - apds9960.readProximity();
292+
proximityCharacteristic.notify32(proximity);
293+
}
294+
295+
if ( temperatureCharacteristic.notifyEnabled() || humidityCharacteristic.notifyEnabled() )
296+
{
297+
float temperature = bmp280.readTemperature();
298+
float temperatureCalibrated = temperature + TEMPERATURE_CALIBRATION;
299+
300+
if ( temperatureCharacteristic.notifyEnabled() )
301+
{
302+
temperatureCharacteristic.notify32(temperatureCalibrated);
303+
}
304+
305+
if ( humidityCharacteristic.notifyEnabled() )
306+
{
307+
float humidity = sht30.readHumidity();
308+
float dp = temperature - ((100.0 - humidity) / 5.0);
309+
float humidityCalibrated = 100.0 - (5.0 * (temperatureCalibrated - dp));
310+
humidityCharacteristic.notify32(humidityCalibrated);
311+
}
312+
}
313+
314+
if ( pressureCharacteristic.notifyEnabled() )
315+
{
316+
float pressure = bmp280.readPressure();
317+
pressureCharacteristic.notify32(pressure);
318+
}
319+
320+
if ( colorCharacteristic.notifyEnabled() )
321+
{
322+
#ifdef ARDUINO_NRF52840_CLUE
323+
digitalWrite(PIN_LED2, enabled);
324+
#endif
325+
326+
int color[4] = { 0 };
327+
apds9960.getColorData((uint16_t*) &color[0], (uint16_t*) &color[1], (uint16_t*) &color[2], (uint16_t*) &color[3]);
328+
colorCharacteristic.notify(color, colorCharacteristic.getMaxLen());
329+
}
330+
}
331+
332+
// callback invoked when central connects
333+
void connect_callback(uint16_t conn_handle)
334+
{
335+
// Get the reference to current connection
336+
BLEConnection* connection = Bluefruit.Connection(conn_handle);
337+
338+
char central_name[32] = { 0 };
339+
connection->getPeerName(central_name, sizeof(central_name));
340+
341+
Serial.print("Connected to ");
342+
Serial.println(central_name);
343+
}
344+
345+
/**
346+
* Callback invoked when a connection is dropped
347+
* @param conn_handle connection where this event happens
348+
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
349+
*/
350+
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
351+
{
352+
(void) conn_handle;
353+
(void) reason;
354+
355+
Serial.println();
356+
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
357+
}

0 commit comments

Comments
 (0)