-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_fake_heartrate
123 lines (89 loc) · 4.58 KB
/
server_fake_heartrate
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
ESP32 BLE SERVER CODE Breakdown - Ravi
// These libraries are developed by Neil Kolban for ESP32 to be used in Arduino IDE
// Include these to create a server, utilize the BLE function
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h> // Helps control "Notify" on/off using the nrfConnect app.
byte flags = 0b00111110;
byte bpm;
byte heart[8] = { 0b00001110, 60, 0, 0, 0 , 0, 0, 0};
// Array of data sent to the receiving client
//Defined by the Bluetooth organization that these 8 bytes are for heart rate characteristics
byte hrmPos[1] = {2};
bool _BLEClientConnected = false; //Default value
#define heartRateService BLEUUID((uint16_t)0x180D) // A shorter way to define the heart rate service
//Once the service is defined, we define the characteristics it is going to display
//"0x2A37" comes from Bluetooth org chart.
BLECharacteristic heartRateMeasurementCharacteristics(BLEUUID((uint16_t)0x2A37), BLECharacteristic::PROPERTY_NOTIFY);
BLECharacteristic sensorPositionCharacteristic(BLEUUID((uint16_t)0x2A38), BLECharacteristic::PROPERTY_READ);
BLEDescriptor heartRateDescriptor(BLEUUID((uint16_t)0x2901));
BLEDescriptor sensorPositionDescriptor(BLEUUID((uint16_t)0x2901));
//Connected-Disconnected status change function
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
_BLEClientConnected = true;
};
void onDisconnect(BLEServer* pServer) {
_BLEClientConnected = false;
}
};
void InitBLE() {
BLEDevice::init("FT7"); // Decide device name, here "FT7"
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pHeart = pServer->createService(heartRateService);
pHeart->addCharacteristic(&heartRateMeasurementCharacteristics);
heartRateDescriptor.setValue("Rate from 0 to 200");
heartRateMeasurementCharacteristics.addDescriptor(&heartRateDescriptor);
heartRateMeasurementCharacteristics.addDescriptor(new BLE2902());
//BLE2902 allows us to stop receiving data from the BLE device thus conserving energy. Basically prevents constant "notify"
pHeart->addCharacteristic(&sensorPositionCharacteristic);
sensorPositionDescriptor.setValue("Position 0 - 6"); // User defined value to be displayed
sensorPositionCharacteristic.addDescriptor(&sensorPositionDescriptor); //these characteristics are defined earlier
pServer->getAdvertising()->addServiceUUID(heartRateService);
pHeart->start();
// Start advertising
pServer->getAdvertising()->start();
}
//Everything above is used to decide the BLE device name, what all to display to the phone
void setup() {
Serial.begin(115200);
Serial.println("Start");
InitBLE();
bpm = 1;
}
void loop() {
// put your main code here, to run repeatedly:
heart[1] = (byte)bpm;
int energyUsed = 3000;
heart[3] = energyUsed / 256;
heart[2] = energyUsed - (heart[2] * 256);
Serial.println(bpm);
// setting the value and giving the length of the array it seems
//Put the values here that you want to broadcast to the phone
heartRateMeasurementCharacteristics.setValue(heart, 8);
heartRateMeasurementCharacteristics.notify();
sensorPositionCharacteristic.setValue(hrmPos, 1);
bpm++;
delay(2000);
}
// Copyright <2017> <Andreas Spiess>
// Free to use
/*
*
* This sketch emulates parts of a Polar H7 Heart Rate Sensor.
* It exposes the Heart rate and the Sensor position characteristics
Copyright <2017> <Andreas Spiess>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Based on Neil Kolban's example file: https://github.com/nkolban/ESP32_BLE_Arduino
*/