-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathBLE.ino
136 lines (111 loc) · 3.31 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
BLE experiments
This example:
- reads the acceleration values from the LSM6DS3 sensor
- show the position data to the serial
- sends the data on BLE
- expose the LED status on the BLE
The circuit:
- Arduino Nano 33 IoT
created 7 Nov 2020
by Olivier Staquet
This example code is in the public domain.
*/
#include <Arduino_LSM6DS3.h>
#include <ArduinoBLE.h>
// Define custom BLE service for position (read-only)
BLEService posService("95ff7bf8-aa6f-4671-82d9-22a8931c5387");
BLEFloatCharacteristic posX("95ff7bf8-aa6f-4671-82d9-22a8931c5387", BLERead);
BLEFloatCharacteristic posY("f49caa00-17f8-4e92-b5fd-d27137ca4515", BLERead);
BLEFloatCharacteristic posZ("84f9b003-6d14-44d7-8db1-d574d29c10c3", BLERead);
// Define custom BLE service for LED management (read/write)
BLEService ledService("daaac223-ea6d-411f-8d8e-32bfb46d4bad");
BLEByteCharacteristic led("40369706-e126-4563-b7ae-3a34b45b3ab8", BLERead | BLEWrite);
void setup() {
// Initialize internal LED (for visual debugging)
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// Initialize Serial connection
Serial.begin(115200);
// Initialize IMU
if (!IMU.begin()) {
// Failed to initialize IMU, blink the internal LED
Serial.println("Failed initializing IMU");
while (1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
// Initialize BLE
if(!BLE.begin()) {
// Failed to initialize BLE, blink the internal LED
Serial.println("Failed initializing BLE");
while (1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
// Set advertised local name and services UUID
BLE.setDeviceName("Arduino Nano 33 IoT");
BLE.setLocalName("BLE Experiment");
posService.addCharacteristic(posX);
posService.addCharacteristic(posY);
posService.addCharacteristic(posZ);
BLE.addService(posService);
ledService.addCharacteristic(led);
BLE.addService(ledService);
// Set default values for characteristics
posX.writeValue((float)0.0);
posY.writeValue((float)0.0);
posZ.writeValue((float)0.0);
led.writeValue(0);
// Start advertising
BLE.advertise();
}
void loop() {
// Listen for BLE
BLEDevice central = BLE.central();
// If a central is connected
if(central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
}
// While central is still connected...
while(central.connected()) {
float x, y, z;
byte ledValue = 0x0;
// Read gyroscope values
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(x, y, z);
}
// Display gyroscope values on serial
Serial.print("Position ");
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print(",");
Serial.println(z);
Serial.print("LED is ");
Serial.println(ledValue);
// Write values on BLE
posX.writeValue(x);
posY.writeValue(y);
posZ.writeValue(z);
led.readValue(ledValue);
// Read values on BLE
if(ledValue == 0x0) {
digitalWrite(LED_BUILTIN, LOW);
} else {
digitalWrite(LED_BUILTIN, HIGH);
}
delay(1000);
}
// when the central disconnects, print it out:
Serial.print("Disconnected from central: ");
Serial.println(central.address());
delay(1000);
}