|
25 | 25 | /* BLE Peripheral Device (this Intel Curie device) */ |
26 | 26 | BLEPeripheral blePeripheral; |
27 | 27 |
|
28 | | -/* UUID for Battery service */ |
29 | | -#define SERVICE_UUID_BATTERY "180F" |
30 | | -/* UUID for Battery Level characteristic */ |
31 | | -#define CHAR_UUID_BATTERY_LEVEL "2A19" |
32 | | - |
33 | | -/* Serial port to use for printing informational messages to the user */ |
34 | | -#define LOG_SERIAL Serial |
35 | | - |
36 | | -/* For convenience, this macro will invoke a specified function call and will |
37 | | - * check the status value returned to ensure it is successful. If not, it will |
38 | | - * print an error message to the serial port and will return from the current function |
39 | | - */ |
40 | | -#define CHECK_STATUS(op) \ |
41 | | - do { \ |
42 | | - BleStatus status = op; \ |
43 | | - if (BLE_STATUS_SUCCESS != status) { \ |
44 | | - LOG_SERIAL.print(#op" returned error status: "); \ |
45 | | - LOG_SERIAL.println(status); \ |
46 | | - return; \ |
47 | | - } \ |
48 | | - } while(0) |
49 | | - |
50 | 28 | /* BLE Battery Service */ |
51 | | -BLEService battSvc(SERVICE_UUID_BATTERY); |
| 29 | +BLEService battSvc("180F"); |
52 | 30 |
|
53 | 31 | /* BLE Battery Level Characteristic */ |
54 | | -BLEUnsignedCharCharacteristic battLvlChar(CHAR_UUID_BATTERY_LEVEL, /* standard 16-bit characteristic UUID */ |
| 32 | +BLEUnsignedCharCharacteristic battLvlChar("2A19", /* standard 16-bit characteristic UUID */ |
55 | 33 | BLERead | BLENotify /* remote clients will be able to get notifications if this characteristic changes */ |
56 | 34 | ); |
57 | 35 |
|
58 | 36 | /* Variable to keep track of last battery level reading from analog input */ |
59 | | -unsigned char oldBattLvl = 0; |
60 | | - |
61 | | -/* This function will be called when a BLE GAP event is detected by the |
62 | | - * Intel Curie BLE device */ |
63 | | -void blePeripheralConnectedEventCb(BLECentral &bleCentral) |
64 | | -{ |
65 | | - /* We've got a new connection. Lets print the MAC address of the remote device */ |
66 | | - LOG_SERIAL.println("Got CONNECTED event"); |
67 | | - LOG_SERIAL.println(bleCentral.address()); |
68 | | -} |
69 | | - |
70 | | -void blePeripheralDisconnectedEventCb(BLECentral &bleCentral) |
71 | | -{ |
72 | | - LOG_SERIAL.println("Got DISCONNECTED event"); |
73 | | -} |
| 37 | +uint8_t oldBattLvl = 0; |
| 38 | +unsigned long previousMillis = 0; |
74 | 39 |
|
75 | 40 | void setup() { |
| 41 | + Serial.begin(9600); |
| 42 | + |
76 | 43 | pinMode(13, OUTPUT); |
77 | | - LOG_SERIAL.begin(9600); |
78 | 44 |
|
79 | 45 | /* Set a name for the BLE device |
80 | 46 | * We give it an arbitrary name which will appear in advertising packets |
81 | 47 | * and can be used by remote peers to identify this BLE device |
82 | 48 | * The name can be changed but must not exceed 20 characters in length */ |
83 | | - CHECK_STATUS(blePeripheral.setLocalName("AE_BATTMON")); |
84 | | - |
85 | | - /* Set a function to be called whenever a BLE GAP event occurs */ |
86 | | - blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectedEventCb); |
87 | | - blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectedEventCb); |
88 | | - |
89 | | - CHECK_STATUS(blePeripheral.setAdvertisedServiceUuid(battSvc.uuid())); |
| 49 | + blePeripheral.setLocalName("AE_BATTMON"); |
| 50 | + blePeripheral.setAdvertisedServiceUuid(battSvc.uuid()); |
90 | 51 |
|
91 | 52 | /* Add the BLE Battery service, and include the UUID in BLE advertising data */ |
92 | | - CHECK_STATUS(blePeripheral.addAttribute(battSvc)); |
| 53 | + blePeripheral.addAttribute(battSvc); |
93 | 54 |
|
94 | 55 | /* This service will have just one characteristic that reflects the current |
95 | 56 | * percentage-charge level of the "battery" */ |
96 | | - CHECK_STATUS(blePeripheral.addAttribute(battLvlChar)); |
| 57 | + blePeripheral.addAttribute(battLvlChar); |
97 | 58 |
|
98 | 59 | /* Set an initial value for this characteristic; refreshed later the loop() function */ |
99 | | - CHECK_STATUS(battLvlChar.setValue(oldBattLvl)); |
| 60 | + battLvlChar.setValue(oldBattLvl); |
100 | 61 |
|
101 | 62 | /* Now activate the BLE device. It will start continuously transmitting BLE |
102 | 63 | * advertising packets and thus become visible to remote BLE central devices |
103 | 64 | * (e.g smartphones) until it receives a new connection */ |
104 | | - CHECK_STATUS(blePeripheral.begin()); |
105 | | - LOG_SERIAL.println("Bluetooth device active, waiting for connections..."); |
| 65 | + blePeripheral.begin(); |
| 66 | + Serial.println("Bluetooth device active, waiting for connections..."); |
106 | 67 | } |
107 | 68 |
|
108 | 69 | void loop() { |
109 | | - static int ledState; |
| 70 | + BLECentral central = blePeripheral.central(); |
110 | 71 |
|
111 | | - blePeripheral.poll(); |
| 72 | + if (central) { |
| 73 | + // central connected to peripheral |
| 74 | + Serial.print(F("Connected to central: ")); |
| 75 | + Serial.println(central.address()); |
112 | 76 |
|
113 | | - /* Blink the on-board LED (just to show some activity) */ |
114 | | - digitalWrite(13, ledState ? HIGH : LOW); |
115 | | - ledState = !ledState; |
| 77 | + digitalWrite(13, HIGH); |
116 | 78 |
|
| 79 | + while (central.connected()) { |
| 80 | + // central still connected to peripheral |
| 81 | + |
| 82 | + unsigned long currentMillis = millis(); |
| 83 | + |
| 84 | + if (currentMillis - previousMillis >= 200) { |
| 85 | + previousMillis = currentMillis; |
| 86 | + updateBatteryLevel(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + digitalWrite(13, LOW); |
| 91 | + |
| 92 | + // central disconnected |
| 93 | + Serial.print(F("Disconnected from central: ")); |
| 94 | + Serial.println(central.address()); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void updateBatteryLevel() { |
117 | 99 | /* Read the current voltage level on the A0 analog input pin. |
118 | 100 | * This is used here to simulate the charge level of a "battery". |
119 | 101 | * The following tutorial shows how a potentiometer could be used |
120 | 102 | * to vary the voltage on an analog input pin: |
121 | 103 | * https://www.arduino.cc/en/Tutorial/Potentiometer |
122 | 104 | */ |
123 | | - unsigned char battLvl = map(analogRead(A0), 0, 1023, 0, 100); |
| 105 | + uint8_t battLvl = map(analogRead(A0), 0, 1023, 0, 100); |
124 | 106 |
|
125 | 107 | if (battLvl != oldBattLvl) { |
126 | | - LOG_SERIAL.print("Battery Level % is now: "); |
127 | | - LOG_SERIAL.println(battLvl); |
128 | | - |
| 108 | + Serial.print("Battery Level % is now: "); |
| 109 | + Serial.println(battLvl); |
| 110 | + |
129 | 111 | /* If the voltage level has changed, we update the value of the |
130 | 112 | * Battery Level BLE characteristic. Because we have enabled |
131 | 113 | * notifications for this characteristic, the remote device can |
132 | 114 | * receive automatic updates when this value is changed. */ |
133 | | - CHECK_STATUS(battLvlChar.setValue(battLvl)); |
| 115 | + battLvlChar.setValue(battLvl); |
134 | 116 | oldBattLvl = battLvl; |
135 | 117 | } |
136 | | - |
137 | | - /* Repeat the loop every 200ms - can be changed if desired */ |
138 | | - delay(200); |
139 | 118 | } |
0 commit comments