Introduction: LinkIt One Control : the GATT Way
In my last instructable, I showed how to explore the power of BLE in LinkIt One. In this one, we will be using BLE GATT profiles to communicate with LinkIt One. Specifically we'll be using BLE GATT profiles to get battery information, control on board led and read analog channel from LinkIt One.
- LinkIt One
- Android device with Bluetooth 4.0 support
Step 1: Setting Up Your LinkIt One
Hardware connection is simple.
For each of the potentiometers, connect one end to VCC, other end to GND and middle pin to A0/A1/A2.
Optionally, you can edit IOService.ccp to include more Digital IO pins also.
Sketch to be uploaded to LInkIt One is attached. The main sketch is fairly simple. It just sets the GATT profiles in setup() using LGATTServer.begin() function and then in loop, we are calling LGATTServer.handleEvents() to process all the communication between android device and LinkIt One.
Other support files IOService, BATTService and ANLGService define the GATT profiles for controlling LED, reading battery state and analog pins respectively.
For GATT communications, there are GATT Services and Characteristics. You can think of Characteristics as the property we want to access from the development board and Services as a collection of some properties.
In this sketch, we are having three services (correspo)
- IOService (00001815-73F5-4BC4-A12F-17D1AD07A961)
- LED Control Characteristic (00002A56-73F5-4BC4-A12F-17D1AD07A961)
- BATTService (180F)
- Charge Level (2A19)
- ANLGService (00001817-73F5-4BC4-A12F-17D1AD07A961)
- Analog A0 (00002A58-73F5-4BC4-A12F-17D1AD07A961)
- Analog A1 (00002A60-73F5-4BC4-A12F-17D1AD07A961)
- Analog A2 (00002A62-73F5-4BC4-A12F-17D1AD07A961)
Some GATT Services are having standard UUIDs like the one BATTService and for other services you can assign custom UUIDs (like for IO and Analog). For more on GATT Specification, read this
Step 2: Power Up
Connect your LinkIt One to PC and open arduino Serial Monitor with 115200 bauds setting.
There is a 20sec delay before starting up to initialize Serial port of LinkIt One. At this time LED at pin 13 will be continuously blinking.
Once it pasts about 15 blinks, you can start your serial monitor. By this time, serial monitor will be initialized.
You will be able to see message as shown in screen shot.
Step 3: Starting Android App
Install and open nRF Master Control Panel in your android device.
Upon opening nRF MC, it will ask you to enable bluetooth in case it's not switched on.
Then it should list you LinkIt One as a device nearby. Note that it may take upto 45sec from LinkIt one reset to BL activation in linkit one.
Screenshot shows listed LinkIt One in my android device.
Step 4: Connecting to LinkIt One
Once your link it one is listed in nRF MCP, you can click "connect" button to establish a connection to it.
Screenshot 1 shows the app after connection. All services are expanded to get a detailed view.
Screenshot 2 shows the debug message from each of the Services in LinkIt One
Step 5: Read Battery Level
Now let's explore some features of the sketch.
First one is reading battery level.
You can see that nRF MCP has automatically detected Battery Service and labeled it properly. This is beacuse I'm using standard GATT UUID 0x180F for battery service and 0x2A19 for battery level characteristic.
Screenshot 1 shows nRF MCP after reading battery level.
Screenshot 2 shows arduino serial terminal with debug message.
Step 6: Controlling Onboard LED
Sketch include a GATT Service to control LED. You can see this service as an "Unknown Service" in nRF MCP with only one characteristic.
It supports both read and write. You can click DOWN arrow near the characteristic to read led status from board.
Also you can write new value to LED. Click on UP arrow, select Uint8 as datatype and type in '1'. Click "Send" when done. Now you can see onboard LED glowing. You can also send '0' to switch off the LED.
Screenshot 1 shows the listed characteristic of LED Service (note that UUID start with "00001815-")
Screenshot 2 shows how to send a new value to LinkIt One
Screenshot 3 shows serial monitor with messages of three actions.
Step 7: Reading Analog Values
To read analog values, just select the service starting with "00001817-" and you can see three characteristics listed, each corresponding to an analog pin A0/A1/A2.
It supports only reading from analog pin. Click in DOWN arrow button to read any analog pin.
For A0, UUID starts with "00002A58-". For A1, UUID starts with "00002A60-" and for A2, UUID starts with "00002A62-". It will display corresponding analog level in hex format (max 0x3FF).
Screenshot 1 shows Analog Read Service with three characteristics.
Screenshot 2 shows Characteristics after reading each of the values.
Screenshot 3 shows debug message in serial terminal.
That's all for this instructable.
We have a be nice policy.
Please be positive and constructive.