-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCometBlue.cpp
522 lines (466 loc) · 14.8 KB
/
CometBlue.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "CometBlue.h"
/**************************************************************************/
/*!
@brief class constructor
@param mac Bluetooth MAC address of comet blue device
*/
/**************************************************************************/
CometBlue::CometBlue(std::string mac) : _deviceAddress(mac) {
//
}
/**************************************************************************/
/*!
@brief inits bluetooth and connect to comet blue
@param deviceName Name of bluetooth device
@param pin Device Pin
*/
/**************************************************************************/
bool CometBlue::connect(std::string deviceName, uint32_t pin) {
// check if connected
if (_connected) {
return true;
}
// connect to device
BLEDevice::init(deviceName);
_client = BLEDevice::createClient();
if (!_client->connect(_deviceAddress)) {
return false;
}
// check service uuid
_remoteService = _client->getService(BLEUUID(SERVICE_UUID));
if (_remoteService == nullptr) {
_client->disconnect();
return false;
}
// authenticate
BLERemoteCharacteristic* pRemotePin = _remoteService->getCharacteristic(BLEUUID(PIN_UUID));
if (pRemotePin == nullptr) {
_client->disconnect();
return false;
}
uint8_t pinencoded[4] = {(uint8_t)(pin & 0xFF), (uint8_t)((pin >> 8) & 0xFF), (uint8_t)((pin >> 16) & 0xFF), (uint8_t)((pin >> 24) & 0xFF)};
pRemotePin->writeValue(pinencoded, sizeof(pinencoded), true);
// verify if successful
std::string value = _remoteService->getValue(BLEUUID(BAT_UUID));
if (value.length() == 0) {
_client->disconnect();
return false;
}
_connected = true;
return true;
}
/**************************************************************************/
/*!
@brief disconnects from bluetooth device
*/
/**************************************************************************/
void CometBlue::disconnect(void) {
// check if connected
if (!_connected) {
return;
}
_client->disconnect();
_connected = false;
_client = NULL;
_remoteService = NULL;
}
/**************************************************************************/
/*!
@brief checks if connected to device and connection still open
@return true if connected to device
*/
/**************************************************************************/
bool CometBlue::isConnected(void) {
// check if connected
if (_connected) {
// check if connection still open
if(_client->isConnected()) {
return true;
}
disconnect();
return false;
}
return false;
}
/**************************************************************************/
/*!
@brief Get device name
@return Device name
*/
/**************************************************************************/
String CometBlue::getDeviceName(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get service
BLERemoteService* deviceName = _client->getService(BLEUUID("00001800-0000-1000-8000-00805f9b34fb"));
if (deviceName == nullptr) {
return "ERROR";
}
// get value
std::string value = deviceName->getValue(BLEUUID("00002a00-0000-1000-8000-00805f9b34fb"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get device model number
@return Model number
*/
/**************************************************************************/
String CometBlue::getModelNumber(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get service
BLERemoteService* deviceName = _client->getService(BLEUUID(SERVICE_INFO));
if (deviceName == nullptr) {
return "ERROR";
}
// get value
std::string value = deviceName->getValue(BLEUUID("00002a24-0000-1000-8000-00805f9b34fb"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get device firmware revision
@return Firmware revision
*/
/**************************************************************************/
String CometBlue::getFirmwareRevision(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get service
BLERemoteService* deviceName = _client->getService(BLEUUID(SERVICE_INFO));
if (deviceName == nullptr) {
return "ERROR";
}
// get value
std::string value = deviceName->getValue(BLEUUID("00002a26-0000-1000-8000-00805f9b34fb"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get device firmware revision
@return Firmware revision
*/
/**************************************************************************/
String CometBlue::getFirmwareRevision2(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get value
std::string value = _remoteService->getValue(BLEUUID("47e9ee2d-47e9-11e4-8939-164230d1df67"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get device software revision
@return Software revision
*/
/**************************************************************************/
String CometBlue::getSoftwareRevision(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get service
BLERemoteService* deviceName = _client->getService(BLEUUID(SERVICE_INFO));
if (deviceName == nullptr) {
return "ERROR";
}
// get value
std::string value = deviceName->getValue(BLEUUID("00002a28-0000-1000-8000-00805f9b34fb"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get device manufacturer name
@return Manufacturer name
*/
/**************************************************************************/
String CometBlue::getManufacturerName(void) {
// check if connected
if (!isConnected()) {
return "NOT CONNECTED";
}
// get service
BLERemoteService* deviceName = _client->getService(BLEUUID(SERVICE_INFO));
if (deviceName == nullptr) {
return "ERROR";
}
// get value
std::string value = deviceName->getValue(BLEUUID("00002a29-0000-1000-8000-00805f9b34fb"));
if (value.length() == 0) {
return "ERROR";
}
return value.c_str();
}
/**************************************************************************/
/*!
@brief Get signal strength to bluetooth device
@return Signal strength percentage [0-100]
*/
/**************************************************************************/
uint8_t CometBlue::getSignalStrength(void) {
// check if connected
if (!isConnected()) {
return 0;
}
int rssi = _client->getRssi();
if (rssi >= -50) {
return 100;
} else if (rssi <= -100) {
return 0;
}
return 2 * (rssi + 100);
}
/**************************************************************************/
/*!
@brief Get battery charge
@return Battery charge percentage [0-100]
*/
/**************************************************************************/
uint8_t CometBlue::getBattery(void) {
// check if connected
if (!isConnected()) {
return 0;
}
// get value
std::string value = _remoteService->getValue(BLEUUID(BAT_UUID));
if (value.length() != 1) {
return 0;
}
return value[0];
}
/**************************************************************************/
/*!
@brief Get device date and time in UTC
@return time_t struct
*/
/**************************************************************************/
time_t CometBlue::getDateTime(void) {
// check if connected
if (!isConnected()) {
return time(NULL);
}
// get value
std::string value = _remoteService->getValue(BLEUUID(DATETIME_UUID));
if (value.length() != 5) {
return time(NULL);
}
// decode date
struct tm devicetime;
devicetime.tm_sec = 0;
devicetime.tm_min = value[0] - 1;
devicetime.tm_hour = value[1] - 1;
devicetime.tm_mday = value[2];
devicetime.tm_mon = value[3] - 1;
devicetime.tm_year = value[4] + 100;
return mktime(&devicetime);
}
/**************************************************************************/
/*!
@brief Get LCD timeout
@return LCDTimeout struct
*/
/**************************************************************************/
LCDTimeout CometBlue::getLCDTimeout(void) {
LCDTimeout ret;
// check if connected
if (!isConnected()) {
return ret;
}
// get value
std::string value = _remoteService->getValue(BLEUUID("47e9ee2e-47e9-11e4-8939-164230d1df67"));
if (value.length() != 2) {
return ret;
}
ret = {value[0], value[1]};
return ret;
}
/**************************************************************************/
/*!
@brief Get device status, bit-codes:
childlock 0x80
manual mode 0x1
adapting 0x400
not ready 0x200
installing 0x400 | 0x200 | 0x100
motor moving 0x100
antifrost activated 0x10
satisfied 0x80000
low battery 0x800
@return status
*/
/**************************************************************************/
uint32_t CometBlue::getStatus(void) {
// check if connected
if (!isConnected()) {
return 0;
}
// get value
std::string value = _remoteService->getValue(BLEUUID("47e9ee2a-47e9-11e4-8939-164230d1df67"));
if (value.length() != 3) {
return 0;
}
uint32_t result = (((uint32_t)value[2]) << 16) | (((uint32_t)value[1]) << 8) | ((uint32_t)value[0]);
return result;
}
/**************************************************************************/
/*!
@brief Get temperature data from device
@return Temperatures struct
*/
/**************************************************************************/
Temperatures CometBlue::getTemperatures(void) {
Temperatures ret;
// check if connected
if (!isConnected()) {
return ret;
}
// get value
std::string value = _remoteService->getValue(BLEUUID("47e9ee2b-47e9-11e4-8939-164230d1df67"));
if (value.length() != 7) {
return ret;
}
ret = {value[0] / 2.0, value[1] / 2.0 , value[2] / 2.0, value[3] / 2.0, value[4] / 2.0, value[5], value[6]};
return ret;
}
/**************************************************************************/
/*!
@brief Sets new device pin
@param pin New pin
@return true if successful
*/
/**************************************************************************/
bool CometBlue::setPin(uint32_t pin) {
// check if connected
if (!isConnected()) {
return false;
}
// get characteristic
BLERemoteCharacteristic* pRemotePin = _remoteService->getCharacteristic(BLEUUID(PIN_UUID));
if (pRemotePin == nullptr) {
return false;
}
// write data
uint8_t pinencoded[4] = {(uint8_t)(pin & 0xFF), (uint8_t)((pin >> 8) & 0xFF), (uint8_t)((pin >> 16) & 0xFF), (uint8_t)((pin >> 24) & 0xFF)};
pRemotePin->writeValue(pinencoded, sizeof(pinencoded), true);
return true;
}
/**************************************************************************/
/*!
@brief Sets device date and time in UTC
@param time time_t struct
@return true if successful
*/
/**************************************************************************/
bool CometBlue::setDateTime(time_t time) {
// check if connected
if (!isConnected()) {
return false;
}
// get characteristic
BLERemoteCharacteristic* pDateTime = _remoteService->getCharacteristic(BLEUUID(DATETIME_UUID));
if (pDateTime == nullptr) {
return false;
}
// write data
tm dt = *localtime(&time);
uint8_t datetime[5] = {dt.tm_min + 1, dt.tm_hour + 1, dt.tm_mday, dt.tm_mon + 1, dt.tm_year - 100};
pDateTime->writeValue(datetime, sizeof(datetime), true);
return true;
}
/**************************************************************************/
/*!
@brief Sets device lcd timeout
@param timeout in seconds
@return true if successful
*/
/**************************************************************************/
bool CometBlue::setLCDTimeout(uint8_t timeout) {
// check if connected
if (!isConnected()) {
return false;
}
// get characteristic
BLERemoteCharacteristic* pLCDTimeout = _remoteService->getCharacteristic(BLEUUID("47e9ee2e-47e9-11e4-8939-164230d1df67"));
if (pLCDTimeout == nullptr) {
return false;
}
// write data
uint8_t timeoutencoded[2] = {timeout, 0};
pLCDTimeout->writeValue(timeoutencoded, sizeof(timeoutencoded), true);
return true;
}
/**************************************************************************/
/*!
@brief Sets device status code
@param status Value
@return true if successful
*/
/**************************************************************************/
bool CometBlue::setStatus(uint32_t status) {
// check if connected
if (!isConnected()) {
return false;
}
// get characteristic
BLERemoteCharacteristic* pStatus = _remoteService->getCharacteristic(BLEUUID("47e9ee2a-47e9-11e4-8939-164230d1df67"));
if (pStatus == nullptr) {
return false;
}
// write data
uint8_t statusencoded[3] = {(uint8_t)(status & 0xFF), (uint8_t)((status >> 8) & 0xFF), (uint8_t)((status >> 16) & 0xFF)};
pStatus->writeValue(statusencoded, sizeof(statusencoded), true);
return true;
}
/**************************************************************************/
/*!
@brief Sets device temperatures
@param temp Temperatures struct
@return true if successful
*/
/**************************************************************************/
bool CometBlue::setTemperatures(Temperatures temp) {
// check if connected
if (!isConnected()) {
return false;
}
// get characteristic
BLERemoteCharacteristic* pTemp = _remoteService->getCharacteristic(BLEUUID(TEMP_UUID));
if (pTemp == nullptr) {
return false;
}
// write data
uint8_t tempencoded[7] = {128, temp.manual_temp * 2, temp.target_temp_low * 2, temp.target_temp_high * 2, temp.offset_temp * 2, temp.window_open_detect, temp.window_open_minutes};
pTemp->writeValue(tempencoded, sizeof(tempencoded), true);
return true;
}
String CometBlue::convertString(std::string str) {
String ret = "";
for (uint8_t i=0; i<str.length(); i++) {
ret += String(str[i], DEC) + " ";
}
return ret;
}