In this blog, I will be configuring the PCoC 6 kit to communicate with my Android phone over BLE.
Understanding BLE
If you would like to learn more in-depth on what BLE is about, this article by Adafruit is very comprehensive
However, to keep things short, I will not cover the theory.
We will be simply changing the GATT settings in order to create Services and Characteristics today. Within the characteristic, we can set values. In our case, the value will be the gesture that we want to communicate over to the app.
There are many more settings which you can explore in ModusToolbox.
Adding Bluetooth characteristics
Under your Project Navigator, open up the configurator tool. Do this by double clicking the design.cybt file.
If you go to GATT settings, you will see that some settings have been done by the example template already.
There is a service called CapSense and it has Button and Slider characteristics under it.
I will add my own custom service and characteristic. Click on the Add symbol on the top left and add the following.
- Create new service : MyService
- Create new characteristic under the service : Gesture
When you create the service, the UUIDs are auto-generated for you. I left it as it is, but you can choose to modify it as well.
In order to make the Gesture characteristic contain our gesture text, I enabled the following settings:
- Properties > Read & Notify : for the device to be able to receive notifications when the value has changed.
- Add > Client Characteristic Configuration : required when you use notifications, the tool also prompts you to enable it if you forget
- Format > uint8[] > Length 20 : the gesture will be a string, so I chose it to be a byte array with a length of 20 (the length is an arbitrary choice)
As of now, we have the following services and characteristics:
CapSense (Service) | 0003CAB5-0000-1000-8000-00805F9B0131 |
---|---|
Button (Char) | 0003CAA3-0000-1000-8000-00805F9B0131 |
Slider (Char) | 0003CAA2-0000-1000-8000-00805F9B0131 |
MyService (Service) | EF603193-7EC1-4825-93DC-A3C43DD45F05 |
---|---|
Gesture (Char) | EF603194-7EC1-4825-93DC-A3C43DD45F05 |
Click on save, and the project code will be auto updated when we compile.
Modify code to talk to BLE
In the code, I created a new Queue in order to relay information (from the accelerometer task running the inference to the BLE task)
In this code snippet, I find the maximum value of classification which will be the detected gesture, I then send it over the queue to be handled by the BLE task.
Next, upon receiving item from the queue, the BLE task will send a notification to the BLE API to update the value with the gesture that we have detected.
See the detailed changes here:
Testing BLE communication device
On an Android phone, I installed the LightBlue app to debug the BLE communication
Connect to the device and confirm that our new service and characteristic shows up.
Click on the Gesture characteristic, and subscribe to the value notifications. Choose UTF-8 String as the format to see the text.
The detected gesture will update every second through BLE
Integrating with a Web app
I will be trying a new feature which is the Web Bluetooth API. I chose it because I wanted it to have the maximum compatibility with my laptop PC and also my Android phone.
This is how the app looks like, coded in standard HTML, CSS and JS.
If you are interested, I have also open-sourced it here: https://github.com/zst123/Element14_Low-Power-IoT/blob/master/mywebapp.html
Upon clicking "Connect BLE", a list of devices will be shown and it can pair with the PSoC 6 device. Information such as gesture detection and CapSense slider is transmitted over BLE to the web app.
This is a demonstration video:
Conclusion
As you can see, the ModusToolbox configuration tool has made it very quick to get started. After setting up everything in the GUI, all the setup code is auto-generated for you. Therefore, only a few chucks of code are needed to create an application.
Coming to the end of the challenge, I will be wrapping up next week in the final blog. Thank you for following me through this project!