Arduino 101 CurieBLE Battery Monitor
This tutorial shows one of the simplest things you can do with an Arduino 101 Bluetooth® Low Energy capabilities.
This tutorial shows one of the simplest things you can do with an Arduino 101's onboard Bluetooth® Low Energy capabilities. The sketch implements the standard Bluetooth® Low Energy "Battery Monitor" service. The Battery Monitor service reads battery level values over Bluetooth® Low Energy from your smartphone or tablet and displays them on the Serial Monitor of the Arduino Software (IDE). This is achieved with the Curie's Bluetooth® Low Energy library and a proper application on the smartphone or tablet.
Hardware Required
- 10k linear potentiometer 
- Smartphone or Tablet Android or iOS 
Software Required
The Circuit
image developed using Fritzing.
The board has an embedded Bluetooth® Low Energy module, therefore it is enough to connect the board to the computer and use the Serial Monitor to read the messages sent by the sketch. A potentiometer is connected to 3.3V, GND and A0 to simulate the charge of a battery.
Software Essentials
Libraries
CurieBLE.h is the library that gives access to all the parameters, features and functions of the Bluetooth® Low Energy module of the 101 board. With Bluetooth® Low Energy it is possible to connect to and communicate with smartphones, tablets and peripherals that support this standard. In this tutorial it is used to read the battery level of the device to which we connect as a peripheral.
updateBatteryLevel() - This function is called every 200 millliseconds from the main loop. It simulates the readiong of a battery with a potentiometer connected to A0. The value is first checked against the former value read. If it is changed, the code activates the printing on the Serial Monitor of the new value and updates the Battery characteristic with
batteryLevelChar.setValue(batteryLevel);Code
This sketch example partially implements the standard Bluetooth® Low-Energy Battery Service.
In the setup(), you initialize pin 13 as an output to drive the LED with
pinMode(13, OUTPUT);BLEPeripheral blePeripheral;For example,
blePeripheral.setLocalName("BatteryMonitorSketch");blePeripheral.setLocalName("BatteryMonitorSketch1");digitalWrite(13, HIGH);digitalWrite(13, LOW);1/*2
3 * Copyright (c) 2016 Intel Corporation.  All rights reserved.4
5 * See the bottom of this file for the license terms.6
7 */8
9#include <CurieBLE.h>10
11/*12
13   This sketch example partially implements the standard Bluetooth® Low-Energy Battery service.14
15   For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx16
17*/18
19/*  */20
21BLEPeripheral blePeripheral;       // BLE Peripheral Device (the board you're programming)22
23BLEService batteryService("180F"); // BLE Battery Service24
25// BLE Battery Level Characteristic"26
27BLEUnsignedCharCharacteristic batteryLevelChar("2A19",  // standard 16-bit characteristic UUID28
29    BLERead | BLENotify);     // remote clients will be able to30// get notifications if this characteristic changes31
32int oldBatteryLevel = 0;  // last battery level reading from analog input33long previousMillis = 0;  // last time the battery level was checked, in ms34
35void setup() {36
37  Serial.begin(9600);    // initialize serial communication38
39  pinMode(13, OUTPUT);   // initialize the LED on pin 13 to indicate when a central is connected40
41  /* Set a local name for the BLE device42
43     This name will appear in advertising packets44
45     and can be used by remote devices to identify this BLE device46
47     The name can be changed but maybe be truncated based on space left in advertisement packet */48
49  blePeripheral.setLocalName("BatteryMonitorSketch");50
51  blePeripheral.setAdvertisedServiceUuid(batteryService.uuid());  // add the service UUID52
53  blePeripheral.addAttribute(batteryService);   // Add the BLE Battery service54
55  blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic56
57  batteryLevelChar.setValue(oldBatteryLevel);   // initial value for this characteristic58
59  /* Now activate the BLE device.  It will start continuously transmitting BLE60
61     advertising packets and will be visible to remote BLE central devices62
63     until it receives a new connection */64
65  blePeripheral.begin();66
67  Serial.println(" Bluetooth® device active, waiting for connections...");68}69
70void loop() {71
72  // listen for BLE peripherals to connect:73
74  BLECentral central = blePeripheral.central();75
76  // if a central is connected to peripheral:77
78  if (central) {79
80    Serial.print("Connected to central: ");81
82    // print the central's MAC address:83
84    Serial.println(central.address());85
86    // turn on the LED to indicate the connection:87
88    digitalWrite(13, HIGH);89
90    // check the battery level every 200ms91
92    // as long as the central is still connected:93
94    while (central.connected()) {95
96      long currentMillis = millis();97
98      // if 200ms have passed, check the battery level:99
100      if (currentMillis - previousMillis >= 200) {101
102        previousMillis = currentMillis;103
104        updateBatteryLevel();105
106      }107
108    }109
110    // when the central disconnects, turn off the LED:111
112    digitalWrite(13, LOW);113
114    Serial.print("Disconnected from central: ");115
116    Serial.println(central.address());117
118  }119}120
121void updateBatteryLevel() {122
123  /* Read the current voltage level on the A0 analog input pin.124
125     This is used here to simulate the charge level of a battery.126
127  */128
129  int battery = analogRead(A0);130
131  int batteryLevel = map(battery, 0, 1023, 0, 100);132
133  if (batteryLevel != oldBatteryLevel) {      // if the battery level has changed134
135    Serial.print("Battery Level % is now: "); // print it136
137    Serial.println(batteryLevel);138
139    batteryLevelChar.setValue(batteryLevel);  // and update the battery level characteristic140
141    oldBatteryLevel = batteryLevel;           // save the level for next comparison142
143  }144}145
146/*147
148   Copyright (c) 2016 Intel Corporation.  All rights reserved.149
150   This library is free software; you can redistribute it and/or151
152   modify it under the terms of the GNU Lesser General Public153
154   License as published by the Free Software Foundation; either155
156   version 2.1 of the License, or (at your option) any later version.157
158   This library is distributed in the hope that it will be useful,159
160   but WITHOUT ANY WARRANTY; without even the implied warranty of161
162   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU163
164   Lesser General Public License for more details.165
166   You should have received a copy of the GNU Lesser General Public167
168   License along with this library; if not, write to the Free Software169
170   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA171
172*/Last revision 2016/04/05 by SM \
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.
 
  