forked from sandeepmistry/arduino-BLEPeripheral
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp_soc.ino
79 lines (69 loc) · 2.22 KB
/
temp_soc.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
// Copyright (c) Bosch Software Innovations GmbH. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/*
* Example sketch for requesting the nRF SoC temperature and
* offer it via a temperature service charachteristic.
*/
#include <BLEPeripheral.h>
class MyBle : public BLEPeripheral {
public:
MyBle(unsigned char req, unsigned char rdy, unsigned char rst)
: BLEPeripheral(req, rdy, rst) {};
void init() {
addAttribute(srvTemp);
addAttribute(chrTemp);
addAttribute(dscTemp);
setDeviceName("Soc Temp"); // optional
setLocalName ("Soc Temp"); // max 20 char!
setAdvertisedServiceUuid(srvTemp.uuid()); // optional
};
private:
BLEService srvTemp = BLEService ("CCC0");
BLEFloatCharacteristic chrTemp = BLEFloatCharacteristic("CCC1", BLERead | BLENotify);
BLEDescriptor dscTemp = BLEDescriptor ("2901", "Temp. Celsius");
// overload callback:
void BLEDeviceTemperatureReceived(BLEDevice& device, float temp) {
if (chrTemp.value()!=temp) {
chrTemp.setValue(temp);
Serial.print("Temp.[C]: "); Serial.println(temp);
}
};
};
// for extern nRF8001 on shield/board
// enable SPI & define pins
//#include <SPI.h>
//#define BLE_REQ 10
//#define BLE_RDY 2
//#define BLE_RST 9
// for nrf51 or nRF52 variants:
#define BLE_REQ 0
#define BLE_RDY 0
#define BLE_RST 0
// create ble temperature instance, see pinouts above
MyBle ble(BLE_REQ, BLE_RDY, BLE_RST);
uint32_t timeStamp;
void handleBleConnect(BLECentral& central) {
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void handleBleDisconnect(BLECentral& central) {
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void setup() {
Serial.begin(115200);
ble.init();
ble.setEventHandler(BLEConnected, handleBleConnect);
ble.setEventHandler(BLEDisconnected, handleBleDisconnect);
ble.begin();
ble.requestTemperature();
timeStamp = millis();
Serial.println("... nRF BLE setup done!");
}
void loop() {
ble.poll();
if ((millis()-timeStamp)> 1000) { //msec
timeStamp = millis();
ble.requestTemperature();
}
}