#include <LBLE.h>
#include <LBLEPeriphral.h>
#include <Servo.h>
Servo my_servo;
int servo_position = 90;
// Define a simple GATT service with only 1 characteristic
LBLEService servoService("19B10010-E8F2-537E-4F6C-D104768A1214");
LBLECharacteristicInt positionCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", LBLE_READ | LBLE_WRITE);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
// to check if USR button is pressed
pinMode(6, INPUT);
my_servo.attach(10);
my_servo.write(servo_position);
// Initialize BLE subsystem
LBLE.begin();
while (!LBLE.ready()) {
delay(100);
}
Serial.println("BLE ready");
Serial.print("Device Address = [");
Serial.print(LBLE.getDeviceAddress());
Serial.println("]");
// configure our advertisement data.
// In this case, we simply create an advertisement that represents an
// connectable device with a device name
LBLEAdvertisementData advertisement;
advertisement.configAsConnectableDevice("BLE Servo");
// Configure our device's Generic Access Profile's device name
// Ususally this is the same as the name in the advertisement data.
LBLEPeripheral.setName("BLE Servo");
// Add characteristics into servoService
servoService.addAttribute(positionCharacteristic);
// Add service to GATT server (peripheral)
LBLEPeripheral.addService(servoService);
// start the GATT server - it is now
// available to connect
LBLEPeripheral.begin();
// start advertisment
LBLEPeripheral.advertise(advertisement);
}
void loop() {
delay(300);
Serial.print("conected=");
Serial.println(LBLEPeripheral.connected());
if (digitalRead(6))
{
Serial.println("disconnect all!");
LBLEPeripheral.disconnectAll();
}
if (positionCharacteristic.isWritten()) {
servo_position = positionCharacteristic.getValue();
Serial.println(servo_position);
my_servo.write(servo_position);
}
}