Build a BLE service
HOMEWORK
Use sensors and actuators of your choice
Build a BLE service with characteristics with read, write and notify properties.
For this assignment, I wanted to experiment with the bluetooth and a servo motor. I wanted to create a door lock that could be controlled using the accelerometer on the Nano 33 IOT and my phone. I could be sitting on my bed, and unlocking and locking my door by pulling the level of my servo on various degree angles.
Using the phone:
doorlock_ble
At first, I set up the Arduino BLE and Servo library. I give a UUID for my characteristic with two values 'BLEREAD' and 'BLEWrite'. For the value of read, the central requests the data from the peripheral, and the peripheral then grants the request. And to write, the central requests the peripheral for confirmation, which once given, the central can write commands to the peripheral.
I then set up the initial position to int pos = 0 and attach my servo PWM pin to 9. The advertised local name is "Servo" and I add a characteristic and a service to the characteristic. The initial value for the characteristic is set to 0.
So to begin with, my servo could be on servo.write (0) when the door is un-clocked, and when I hit 1 on my hex-code on the LightBlue app, the servo lever could rotate using servo.write(180) and lock the door.
Nano_accel
Using accelerometer:
To add an element to the assignment, I wanted to use the accelerometer on my Nano 33 IOT to control the door lock. On using the accelerometer by itself in code on the LightBlue app, I learnt that the Y axis was the point I wanted to experiment with. And so I decided to use the accelerometer on the board itself to sync with the movements on the Y axis. I again set up the Arduino BLE and Servo library and added the BLEIntCharacteristic CharacteristicY. I then set up the local name and added the service UUID and added the characteristic Y to the service with an initial value of 0. I then wrote the condition of
if (y > -0.8) {
myservo.write(0);;
} else if (y < 0.9) {
myservo.write(180);
}