Skip to content

Commit 21d657e

Browse files
committed
Initial version
0 parents  commit 21d657e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+5780
-0
lines changed

LICENSE

+504
Large diffs are not rendered by default.

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ArduinoBLE
2+
3+
Enables BLE connectivity on the Arduino MKR WiFi 1010 and Arduino UNO WiFi Rev.2.
4+
5+
This library currently supports creating a BLE peripheral.
6+
7+
Requires the NINA module to be running [Arduino NINA-W102 firmware](https://github.com/arduino/nina-fw) v1.2.0 or later.
8+
9+
## License
10+
11+
```
12+
Copyright (c) 2018 Arduino SA. All rights reserved.
13+
14+
This library is free software; you can redistribute it and/or
15+
modify it under the terms of the GNU Lesser General Public
16+
License as published by the Free Software Foundation; either
17+
version 2.1 of the License, or (at your option) any later version.
18+
19+
This library is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22+
Lesser General Public License for more details.
23+
24+
You should have received a copy of the GNU Lesser General Public
25+
License along with this library; if not, write to the Free Software
26+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Battery Monitor
3+
4+
This example creates a BLE peripheral with the standard battery service and
5+
level characteristic. The A0 pin is used to calculate the battery level.
6+
7+
The circuit:
8+
- Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include <ArduinoBLE.h>
14+
15+
// BLE Battery Service
16+
BLEService batteryService("180F");
17+
18+
// BLE Battery Level Characteristic
19+
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID
20+
BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
21+
22+
int oldBatteryLevel = 0; // last battery level reading from analog input
23+
long previousMillis = 0; // last time the battery level was checked, in ms
24+
25+
void setup() {
26+
Serial.begin(9600); // initialize serial communication
27+
while (!Serial);
28+
29+
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
30+
31+
// begin initialization
32+
if (!BLE.begin()) {
33+
Serial.println("starting BLE failed!");
34+
35+
while (1);
36+
}
37+
38+
/* Set a local name for the BLE device
39+
This name will appear in advertising packets
40+
and can be used by remote devices to identify this BLE device
41+
The name can be changed but maybe be truncated based on space left in advertisement packet
42+
*/
43+
BLE.setLocalName("BatteryMonitor");
44+
BLE.setAdvertisedService(batteryService); // add the service UUID
45+
batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
46+
BLE.addService(batteryService); // Add the battery service
47+
batteryLevelChar.writeValue(oldBatteryLevel); // set initial value for this characteristic
48+
49+
/* Start advertising BLE. It will start continuously transmitting BLE
50+
advertising packets and will be visible to remote BLE central devices
51+
until it receives a new connection */
52+
53+
// start advertising
54+
BLE.advertise();
55+
56+
Serial.println("Bluetooth device active, waiting for connections...");
57+
}
58+
59+
void loop() {
60+
// wait for a BLE central
61+
BLEDevice central = BLE.central();
62+
63+
// if a central is connected to the peripheral:
64+
if (central) {
65+
Serial.print("Connected to central: ");
66+
// print the central's BT address:
67+
Serial.println(central.address());
68+
// turn on the LED to indicate the connection:
69+
digitalWrite(LED_BUILTIN, HIGH);
70+
71+
// check the battery level every 200ms
72+
// while the central is connected:
73+
while (central.connected()) {
74+
long currentMillis = millis();
75+
// if 200ms have passed, check the battery level:
76+
if (currentMillis - previousMillis >= 200) {
77+
previousMillis = currentMillis;
78+
updateBatteryLevel();
79+
}
80+
}
81+
// when the central disconnects, turn off the LED:
82+
digitalWrite(LED_BUILTIN, LOW);
83+
Serial.print("Disconnected from central: ");
84+
Serial.println(central.address());
85+
}
86+
}
87+
88+
void updateBatteryLevel() {
89+
/* Read the current voltage level on the A0 analog input pin.
90+
This is used here to simulate the charge level of a battery.
91+
*/
92+
int battery = analogRead(A0);
93+
int batteryLevel = map(battery, 0, 1023, 0, 100);
94+
95+
if (batteryLevel != oldBatteryLevel) { // if the battery level has changed
96+
Serial.print("Battery Level % is now: "); // print it
97+
Serial.println(batteryLevel);
98+
batteryLevelChar.writeValue(batteryLevel); // and update the battery level characteristic
99+
oldBatteryLevel = batteryLevel; // save the level for next comparison
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Button LED
3+
4+
This example creates a BLE peripheral with service that contains a
5+
characteristic to control an LED and another characteristic that
6+
represents the state of the button.
7+
8+
The circuit:
9+
- Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
10+
- Button connected to pin 4
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <ArduinoBLE.h>
16+
17+
const int ledPin = LED_BUILTIN; // set ledPin to on-board LED
18+
const int buttonPin = 4; // set buttonPin to digital pin 4
19+
20+
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
21+
22+
// create switch characteristic and allow remote device to read and write
23+
BLEByteCharacteristic ledCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
24+
// create button characteristic and allow remote device to get notifications
25+
BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
while (!Serial);
30+
31+
pinMode(ledPin, OUTPUT); // use the LED as an output
32+
pinMode(buttonPin, INPUT); // use button pin as an input
33+
34+
// begin initialization
35+
if (!BLE.begin()) {
36+
Serial.println("starting BLE failed!");
37+
38+
while (1);
39+
}
40+
41+
// set the local name peripheral advertises
42+
BLE.setLocalName("ButtonLED");
43+
// set the UUID for the service this peripheral advertises:
44+
BLE.setAdvertisedService(ledService);
45+
46+
// add the characteristics to the service
47+
ledService.addCharacteristic(ledCharacteristic);
48+
ledService.addCharacteristic(buttonCharacteristic);
49+
50+
// add the service
51+
BLE.addService(ledService);
52+
53+
ledCharacteristic.writeValue(0);
54+
buttonCharacteristic.writeValue(0);
55+
56+
// start advertising
57+
BLE.advertise();
58+
59+
Serial.println("Bluetooth device active, waiting for connections...");
60+
}
61+
62+
void loop() {
63+
// poll for BLE events
64+
BLE.poll();
65+
66+
// read the current button pin state
67+
char buttonValue = digitalRead(buttonPin);
68+
69+
// has the value changed since the last read
70+
boolean buttonChanged = (buttonCharacteristic.value() != buttonValue);
71+
72+
if (buttonChanged) {
73+
// button state changed, update characteristics
74+
ledCharacteristic.writeValue(buttonValue);
75+
buttonCharacteristic.writeValue(buttonValue);
76+
}
77+
78+
if (ledCharacteristic.written() || buttonChanged) {
79+
// update LED, either central has written to characteristic or button state has changed
80+
if (ledCharacteristic.value()) {
81+
Serial.println("LED on");
82+
digitalWrite(ledPin, HIGH);
83+
} else {
84+
Serial.println("LED off");
85+
digitalWrite(ledPin, LOW);
86+
}
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Callback LED
3+
4+
This example creates a BLE peripheral with service that contains a
5+
characteristic to control an LED. The callback features of the
6+
library are used.
7+
8+
The circuit:
9+
- Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
10+
11+
This example code is in the public domain.
12+
*/
13+
14+
#include <ArduinoBLE.h>
15+
16+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
17+
18+
// create switch characteristic and allow remote device to read and write
19+
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
20+
21+
const int ledPin = LED_BUILTIN; // pin to use for the LED
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
while (!Serial);
26+
27+
pinMode(ledPin, OUTPUT); // use the LED pin as an output
28+
29+
// begin initialization
30+
if (!BLE.begin()) {
31+
Serial.println("starting BLE failed!");
32+
33+
while (1);
34+
}
35+
36+
// set the local name peripheral advertises
37+
BLE.setLocalName("LEDCallback");
38+
// set the UUID for the service this peripheral advertises
39+
BLE.setAdvertisedService(ledService);
40+
41+
// add the characteristic to the service
42+
ledService.addCharacteristic(switchCharacteristic);
43+
44+
// add service
45+
BLE.addService(ledService);
46+
47+
// assign event handlers for connected, disconnected to peripheral
48+
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
49+
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
50+
51+
// assign event handlers for characteristic
52+
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
53+
// set an initial value for the characteristic
54+
switchCharacteristic.setValue(0);
55+
56+
// start advertising
57+
BLE.advertise();
58+
59+
Serial.println(("Bluetooth device active, waiting for connections..."));
60+
}
61+
62+
void loop() {
63+
// poll for BLE events
64+
BLE.poll();
65+
}
66+
67+
void blePeripheralConnectHandler(BLEDevice central) {
68+
// central connected event handler
69+
Serial.print("Connected event, central: ");
70+
Serial.println(central.address());
71+
}
72+
73+
void blePeripheralDisconnectHandler(BLEDevice central) {
74+
// central disconnected event handler
75+
Serial.print("Disconnected event, central: ");
76+
Serial.println(central.address());
77+
}
78+
79+
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
80+
// central wrote new value to characteristic, update LED
81+
Serial.print("Characteristic event, written: ");
82+
83+
if (switchCharacteristic.value()) {
84+
Serial.println("LED on");
85+
digitalWrite(ledPin, HIGH);
86+
} else {
87+
Serial.println("LED off");
88+
digitalWrite(ledPin, LOW);
89+
}
90+
}

examples/Peripheral/LED/LED.ino

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
LED
3+
4+
This example creates a BLE peripheral with service that contains a
5+
characteristic to control an LED.
6+
7+
The circuit:
8+
- Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board
9+
10+
This example code is in the public domain.
11+
*/
12+
13+
#include <ArduinoBLE.h>
14+
15+
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
16+
17+
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
18+
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
19+
20+
const int ledPin = LED_BUILTIN; // pin to use for the LED
21+
22+
void setup() {
23+
Serial.begin(9600);
24+
while (!Serial);
25+
26+
// set LED pin to output mode
27+
pinMode(ledPin, OUTPUT);
28+
29+
// begin initialization
30+
if (!BLE.begin()) {
31+
Serial.println("starting BLE failed!");
32+
33+
while (1);
34+
}
35+
36+
// set advertised local name and service UUID:
37+
BLE.setLocalName("LED");
38+
BLE.setAdvertisedService(ledService);
39+
40+
// add the characteristic to the service
41+
ledService.addCharacteristic(switchCharacteristic);
42+
43+
// add service
44+
BLE.addService(ledService);
45+
46+
// set the initial value for the characeristic:
47+
switchCharacteristic.writeValue(0);
48+
49+
// start advertising
50+
BLE.advertise();
51+
52+
Serial.println("BLE LED Peripheral");
53+
}
54+
55+
void loop() {
56+
// listen for BLE peripherals to connect:
57+
BLEDevice central = BLE.central();
58+
59+
// if a central is connected to peripheral:
60+
if (central) {
61+
Serial.print("Connected to central: ");
62+
// print the central's MAC address:
63+
Serial.println(central.address());
64+
65+
// while the central is still connected to peripheral:
66+
while (central.connected()) {
67+
// if the remote device wrote to the characteristic,
68+
// use the value to control the LED:
69+
if (switchCharacteristic.written()) {
70+
if (switchCharacteristic.value()) { // any value other than 0
71+
Serial.println("LED on");
72+
digitalWrite(ledPin, HIGH); // will turn the LED on
73+
} else { // a 0 value
74+
Serial.println(F("LED off"));
75+
digitalWrite(ledPin, LOW); // will turn the LED off
76+
}
77+
}
78+
}
79+
80+
// when the central disconnects, print it out:
81+
Serial.print(F("Disconnected from central: "));
82+
Serial.println(central.address());
83+
}
84+
}

0 commit comments

Comments
 (0)