-
Notifications
You must be signed in to change notification settings - Fork 20
/
Nano33BLESensorExample_microphoneRMS.ino
141 lines (128 loc) · 6 KB
/
Nano33BLESensorExample_microphoneRMS.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Nano33BLESensorExample_microphoneRMS.ino
Copyright (c) 2020 Dale Giancono. All rights reserved..
This program is an example program showing some of the cababilities of the
Nano33BLESensor Library. In this case it outputs RMS microphone data and
proximity data from two of the Arduino Nano 33 BLE Sense's on board
sensors via serial in a format that can be displayed on the Arduino IDE
serial plotter. It also outputs the data via BLE in a string format that
can be viewed using a variety of BLE scanning software.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/*INCLUDES */
/*****************************************************************************/
#include "Arduino.h"
/* For the bluetooth funcionality */
#include <ArduinoBLE.h>
#include "Nano33BLEMicrophoneRMS.h"
/*****************************************************************************/
/*MACROS */
/*****************************************************************************/
/*
* We use strings to transmit the data via BLE, and this defines the buffer
* size used to transmit these strings. Only 20 bytes of data can be
* transmitted in one packet with BLE, so a size of 20 is chosen the the data
* can be displayed nicely in whatever application we are using to monitor the
* data.
*/
#define BLE_BUFFER_SIZES 20
/* Device name which can be scene in BLE scanning software. */
#define BLE_DEVICE_NAME "Arduino Nano 33 BLE Sense"
/* Local name which should pop up when scanning for BLE devices. */
#define BLE_LOCAL_NAME "MicrophoneRMS BLE"
/*****************************************************************************/
/*GLOBAL Data */
/*****************************************************************************/
/*
* Nano33BLEMicrophoneRMSData and Nano33BLEProximityData object which we will
* store data in each time we read the microphone and proximity data.
*/
Nano33BLEMicrophoneRMSData microphoneData;
/*
* Declares the BLEService and characteristics we will need for the BLE
* transfer. The UUID was randomly generated using one of the many online
* tools that exist. It was chosen to use BLECharacteristic instead of
* BLEIntCharacteristic was it is hard to view int data in most BLE
* scanning software. Strings can be viewed easiler enough. In an actual
* application you might want to transfer ints directly.
*/
BLEService BLESensors("590d65c7-3a0a-4023-a05a-6aaf2f22441c");
BLECharacteristic microphoneRMSBLE("000F", BLERead | BLENotify | BLEBroadcast, BLE_BUFFER_SIZES);
/* Common global buffer will be used to write to the BLE characteristics. */
char bleBuffer[BLE_BUFFER_SIZES];
/*****************************************************************************/
/*SETUP (Initialisation) */
/*****************************************************************************/
void setup()
{
/*
* Serial setup. This will be used to transmit data for viewing on serial
* plotter
*/
Serial.begin(115200);
while(!Serial);
/* BLE Setup. For information, search for the many ArduinoBLE examples.*/
if (!BLE.begin())
{
while (1);
}
else
{
BLE.setDeviceName(BLE_DEVICE_NAME);
BLE.setLocalName(BLE_LOCAL_NAME);
BLE.setAdvertisedService(BLESensors);
/* A seperate characteristic is used for each kind of data. */
BLESensors.addCharacteristic(microphoneRMSBLE);
BLE.addService(BLESensors);
BLE.advertise();
/*
* Initialises the microphone and proximity sensor, and starts the
* periodic reading of the sensor using a Mbed OS thread.
* The data is placed in a circular buffer and can be read whenever.
*/
MicrophoneRMS.begin();
/* Plots the legend on Serial Plotter */
Serial.println("MicrophoneRMS\r\n");
}
}
/*****************************************************************************/
/*LOOP (runtime super loop) */
/*****************************************************************************/
void loop()
{
BLEDevice central = BLE.central();
if(central)
{
int writeLength;
/*
* If a BLE device is connected, magnetic data will start being read,
* and the data will be written to each BLE characteristic. The same
* data will also be output through serial so it can be plotted using
* Serial Plotter.
*/
while(central.connected())
{
/*
* sprintf is used to convert the read integer value to a string
* which is stored in bleBuffer. This string is then written to
* the BLE characteristic.
*/
if(MicrophoneRMS.pop(microphoneData))
{
writeLength = sprintf(bleBuffer, "%d", microphoneData.RMSValue);
microphoneRMSBLE.writeValue((void*)bleBuffer, writeLength);
Serial.println(bleBuffer);
}
}
}
}