-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_ble.ino
78 lines (56 loc) · 2.72 KB
/
arduino_ble.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
#include <ArduinoBLE.h>
BLEService newService("180A"); // creating the service
BLEUnsignedCharCharacteristic randomReading("2A58", BLERead | BLENotify); // creating the Analog Value characteristic
BLEByteCharacteristic switchChar("2A57", BLERead | BLEWrite); // creating the LED characteristic
const int ledPin = 2;
long previousMillis = 0;
void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial); //starts the program if we open the serial monitor.
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
pinMode(ledPin, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
//initialize ArduinoBLE library
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy failed!");
while (1);
}
BLE.setLocalName("MKR WiFi 1010"); //Setting a name that will appear when scanning for Bluetooth® devices
BLE.setAdvertisedService(newService);
newService.addCharacteristic(switchChar); //add characteristics to a service
newService.addCharacteristic(randomReading);
BLE.addService(newService); // adding the service
switchChar.writeValue(0); //set initial value for characteristics
randomReading.writeValue(0);
BLE.advertise(); //start advertising the service
Serial.println(" Bluetooth® device active, waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central(); // wait for a Bluetooth® Low Energy central
if (central) { // if a central is connected to the peripheral
Serial.print("Connected to central: ");
Serial.println(central.address()); // print the central's BT address
digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection
// check the battery level every 200ms
// while the central is connected:
while (central.connected()) {
long currentMillis = millis();
if (currentMillis - previousMillis >= 200) { // if 200ms have passed, we check the battery level
previousMillis = currentMillis;
int randomValue = analogRead(A1);
randomReading.writeValue(randomValue);
if (switchChar.written()) {
if (switchChar.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
}
digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}