/
Maja_Client.ino
271 lines (225 loc) · 6.87 KB
/
Maja_Client.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//BLuetooth libraries
#include "BLEDevice.h"
#include "BLECharacteristic.h"
//LED configuration
const int ledR1 = 15;
const int ledChannelR1 = 0;
const int ledG1 = 2;
const int ledChannelG1 = 1;
const int ledB1 = 4;
const int ledChannelB1 = 2;
const int ledR2 = 21;
const int ledChannelR2 = 3;
const int ledG2 = 22;
const int ledChannelG2 = 4;
const int ledB2 = 23;
const int ledChannelB2 = 5;
const int ledR3 = 13;
const int ledChannelR3 = 6;
const int ledG3 = 12;
const int ledChannelG3 = 7;
const int ledB3 = 14;
const int ledChannelB3 = 8;
const int freq = 5000;
const int resolution = 8;
const int buttonInputPin = 33;
const int pipser=5;
//Bluetooth settings
BLEUUID serviceUUID("caccbe7e-7191-4f78-92f6-c25f67120eab");
BLEUUID charUUID("6fd78f9f-fd43-460f-8cf2-c405c25c2b04");
static BLEScan* pBLEScan;
static BLEAddress* pAddress;
static BLEClient* pClient;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static boolean doConnect = false;
static boolean connected = false;
//glucoseData structure
struct glucoseData{
uint16_t timestamp;
uint16_t value;
};
//Data buffer settings
int bufferSize=300;
glucoseData* buffer=new glucoseData[bufferSize];
int bufferPosition=0;
int bufferedElements=0;
static bool newData=false; //Static for the acces from another Thread
//Reads the glucoseData from the BLE Server and stores it into the buffer for later use
void readDataIntoBuffer(){
newData=false;
std::string s=pRemoteCharacteristic->readValue();
uint32_t* ptr=(uint32_t*) s.c_str();
int numData=*ptr;
for(int i=0;i<numData ;i++){
glucoseData data;
uint32_t* dataptr=(uint32_t*) &data;
*dataptr=(uint32_t) ptr[i+1];
buffer[bufferPosition]=data;
bufferPosition=(bufferPosition+1)%bufferSize;
if(bufferedElements<bufferSize){
bufferedElements++;
}
Serial.println("Data read: ");
Serial.print("Timestamp: ");
Serial.println(data.timestamp);
Serial.print("Value: ");
Serial.println(data.value);
Serial.println("");
}
}
//Returns the latest glucoseData
glucoseData getLatestData(){
if(bufferedElements>0){
return buffer[bufferPosition-1];
}
glucoseData data;
data.timestamp=0;
data.value=0;
return data;
}
//BLE Callback that gets triggered when new NFC data has been read
static void notifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
Serial.println("Notify received!");
newData=true;
}
//BLE Callback that checks if a scanned device is the Libre Sensor
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
const char* serverName="MajaNFCServer";
if (!strcmp(advertisedDevice.getName().c_str(), serverName)) {
Serial.println("Found our device!");
advertisedDevice.getScan()->stop();
pAddress = new BLEAddress(advertisedDevice.getAddress());
doConnect = true;
}
}
};
//Sets the color of the leds
void setColor(byte r, byte g, byte b){
ledcWrite(ledChannelR1, r);
ledcWrite(ledChannelR2, r);
ledcWrite(ledChannelR3, r);
ledcWrite(ledChannelG1, g);
ledcWrite(ledChannelG2, g);
ledcWrite(ledChannelG3, g);
ledcWrite(ledChannelB1, b);
ledcWrite(ledChannelB2, b);
ledcWrite(ledChannelB3, b);
}
//Sets the color depending on the given glucose value
void setColorByGlucose(int glucose){
//Mittlerer Wert = 105
//Hoher Wert = 140
//Niedriger Wert = 70
float colorValue = fabs(105-glucose)/35.0f;
if(colorValue>1){
colorValue=1;
}
byte red = (byte) (255.0f * colorValue);
Serial.print("RED: ");
Serial.println(red);
byte green = (byte) (255.0f * (1.0f-colorValue));
Serial.print("GREEN: ");
Serial.println(green);
setColor(red, green, 0);
}
//Manages the BLE connection
bool isConnected(){
if(connected){
if(pClient->isConnected()){
return true;
}
else{
Serial.println("BLE Connection Lost!");
pClient->disconnect();
setColor(0, 0, 255);
connected=false;
}
}
if(!doConnect){
if(pBLEScan==nullptr){
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
}
pBLEScan->start(30);
}
if(doConnect){
doConnect=false;
Serial.print("Connecting to: ");
Serial.println(pAddress->toString().c_str());
if(pClient==nullptr){
pClient = BLEDevice::createClient();
}
pClient->connect(*pAddress);
Serial.println(" - Connected to server");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service");
return false;
}
Serial.println(" - Found our service");
pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic");
return false;
}
pRemoteCharacteristic->registerForNotify(notifyCallback);
Serial.println(" - Found our characteristic");
connected=true;
newData=true;
setColor(255, 255, 255);
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
//BLE Startup
Serial.println("Starting BLE Client");
BLEDevice::init("MajaClient");
Serial.println("BLE Client Started");
//Pin setups
ledcSetup(ledChannelR1, freq, resolution);
ledcAttachPin(ledR1, ledChannelR1);
ledcSetup(ledChannelG1, freq, resolution);
ledcAttachPin(ledG1, ledChannelG1);
ledcSetup(ledChannelB1, freq, resolution);
ledcAttachPin(ledB1, ledChannelB1);
ledcSetup(ledChannelR2, freq, resolution);
ledcAttachPin(ledR2, ledChannelR2);
ledcSetup(ledChannelG2, freq, resolution);
ledcAttachPin(ledG2, ledChannelG2);
ledcSetup(ledChannelB2, freq, resolution);
ledcAttachPin(ledB2, ledChannelB2);
ledcSetup(ledChannelR3, freq, resolution);
ledcAttachPin(ledR3, ledChannelR3);
ledcSetup(ledChannelG3, freq, resolution);
ledcAttachPin(ledG3, ledChannelG3);
ledcSetup(ledChannelB3, freq, resolution);
ledcAttachPin(ledB3, ledChannelB3);
pinMode(buttonInputPin, INPUT);
setColor(0, 0, 255);
}
const int checkCycle=20;
int counter=0;
void loop() {
if(counter == checkCycle){
counter=0;
if(isConnected()){
if(newData){
readDataIntoBuffer();
if(bufferedElements>0){
setColorByGlucose(getLatestData().value);
}
}
}
}
if(digitalRead(buttonInputPin) == HIGH){
setColor(0, 0, 0);
}
counter++;
delay(50);
}