3.2.1 BLE Sensor App
WBZ451 Curiosity Board
Devices (Device): PIC32CX1012BZ25048(MCU) on WBZ451 Module
Devices (Used, On-Board): Analog Temperature Sensor, RGB LED, User Button, User LED, USB-UART converter
Hardware Requirement
Tool | Qty |
WBZ451 Curiosity Board | 1 |
Micro usb cable | 1 |
Andrioid/iOS Smartphone | 1 |
Software Requirement
TeratermSmartphone App
Microchip Bluetooth Data (MBD) iOS/Android app available in stores.Programming the Precompiled hex file or Application Example
Programming the hex file using MPLABX IPE
-
Precompiled Hex file is located in
"<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\advanced_applications\ble_sensor\precompiled_hex\ble_sensor.X.production.signed.unified_gpiobased.hex"
folder - For more details on the steps, go
to Programming A Device Note: Ensure to choose the correct Device and Tool information.
Programming the Application using MPLABX IDE
-
Follow steps mentioned inRunning a Precompiled Example.
-
Open and program the Application Example
"ble_sensor.x" located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\advanced_applications\ble_sensor\firmware"
using MPLABX IDE
For more details on how to find the Harmony Content Path, refer to Installing MCC Pluggin
Demo Description
This application demonstrates the capability of WBZ451 module to connect to a mobile phone through Bluetooth Low Energy(BLE). The RGB LED on the Curiosity board can be controlled by mobile app. The WBZ451 device will also report the temperature data periodically to mobile phone through Bluetooth low energy (BLE).-
The WBZ451 module will be a BLE peripheral device and will advertise on startup. The user can initiate the connection through mobile application. The advertisement payload holds the temperature information and the RGB ON/OFF status.
-
Uses "BLE Sensor" sub app from the Microchip Bluetooth Data (MBD) mobile app for BLE demonstration.
-
Application is supported by iOS and Andrioid OS
-
Available in respective app stores/play stores
-
-
When Connected(ING) to the application the BLUE color User LED turns on
-
Blinks the "User LED" with 500 ms blink interval when the device sending advertisement packets
-
Once connected, the "User LED" turns on solid
-
-
From the Smartphone App the following actions can be performed
-
The RGB LED can be switched On/Off from MBD mobile app.
-
When LED is switched On, the RGB color can be changed from mobile app color wheel.
- The RGB color value is received as HSV (Hue, Saturation, Value) from mobile app through TRPStransparent profile and service
-
The HSV value is converted to RGB equivalent value in the device. The corresponding PWM duty cycle for R,G,B will be calculated and the PWM pulse is provided on R,G,B LEDs.
-
-
From the WBZ451 Module, perform the following actions:
-
The RGB LED can be switched on/off by pressing the On board "User Button"
-
When the User Button is pressed and released the RGB LED is switched ON with default color GREEN or the last stored color.
-
When the User Button is pressed and released again, then the RGB LED is toggled from the previous state.
-
-
Read the temperature sensor every 1 second once, and send the temperature value to mobile app when the temperature changes about 1 ℃
-
Testing
- Power Up: Connect the Curiosity board using Micro USB to PC. Power Supply Green LED will turn on when connected to PC.
UART Console Behavior:
- The application activity is shown as "Console Log" through on board UART-USB converter
- Open Terminal (eg: Tera Term) to look for these logs - UART baud settings: 115200 8-N-1 without flow control
BLE Not Connected Behavior
- The Blue "User LED" will be blinking
- Scanned from mobile app as "BLE_Sensor_XXXX", where 'XXXX' is a uniqu number associated with this board. Launch MBD mobile app and follow the below screenshots
-
BLE Connected Behavior (Connected to "BLE Sensor" MBD Mobile Application). The Blue "User LED" will be solid On when connected.
Firmware Version
- Bluetooth SIG defined "Device Info Service" is implemented in the device to share the device information like "firmware version", "manufacture name", etc.,
- Verify the firmware version shown on console with MBD app
- The BLE Sensor mobile app will show the temperature (deg C) and LED status received from device, as well as allow the user to vary the RGB color and brightness
-
The RGB LED can also be switched On/Off by pressing the On board "User Button (SW2)". The On/Off LED status will also be reported to mobile app if connected. When the "User Button (SW2)" is pressed and released, RGB LED is switched ON. When pressed and released again, the state will be toggled from the previous state.
Protocol Exchange
For more details on the communication protocol exchange between BLE sensor mobile app (BLE central) and
WBZ451 Module (BLE peripheral), refer to Protocol ExchangeApplication Flow Diagram
Expanding/Customising the Application
- The protocol parser is
implemented in
app_trps.c
andapp_trps.h
- Request and Response: when the user wants to get and modify data on the device.
- Notify: when there is a new data to be sent to mobile app.
- The protocol commands are defined
in
app_ble_sensor.h
and related sensor functionalities are implemented in app_ble_sensor.c- Request and Response: When a command request is received from mobile app, app_trps.c parse the data and forward to specific command callback handler in app_ble_sensor.c
- Notify: When specific Sensor data to be sent to mobile app, app_ble_sensor.c places the data in data base and calls for notify handler in app_trps.c (/ref APP_TRPS_SendNotification())
Example: To add a command to get temperature in Fahrenheit.
Complete the SDK Setup and open the "ble_sensor.x" located in"<Harmony Content
Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\advanced_applications\ble_sensor\firmware"
using MPLABX IDEChanges to -
app_ble_sensor.h
- Define request, response, size of response payload (not including size of length byte and response ID)
- Define a new control command ID
- Define a new control command response
- Define the length for the new response command
- Inside the existing APP_TRPS_SensorData_T structure, add the new data structure which holds the response data
- Increase the list size BLE_SENSOR_CMD_RESP_LST_SIZE by one
- Add new command request/response set in BLE_SENSOR_DEFINE_CTRL_CMD_RESP() list
Changes to - app_ble_sensor.c
- Read the temperature periodically and store in above defined data structure. Add the below code in App_TRPS_Sensor_TimerHandler()
- After doing the above changes follow the instruction in select option Build Project in IDE to compile the application example
-
Select option Run Project in IDE to program the target – the onboard debugger will program the example application
Testing with MBD mobile app
The BLE Sensor sub app in MBD mobile app is designed only to work with specific functionality. So, if new command is added in BLE sensor application, BLE Sensor mobile app will not know about it. To test the above newly added code, "BLE Smart" sub app in MBD can be used. Follow the below screenshots after launching MBD mobile app.
Using the MPLAB Code Configurator
MPLABX Harmony provides the MPLAB® Code Configurator (MCC) tool, a set of modular device and middleware libraries, and numerous example applications, all of which are designed to help developers to quickly and easily develop powerful and efficient embedded software.- Open the MCC tool from MPLABX
- Once the MCC tool is launched, the "Project Graph" shows the list of MCC components used in the specific project
- Clicking on the specific component block will allow to change the configuration of that component. eg:SERCOM0 is shown in the screenshot.
- New component can also be added by drag and drop from "Device Resources" to "Project Graph". Then configure the component as in step #3. eg: SERCOM1 can be added into "Project Graph"
- Once all the required components and configurations are done, generate a code . For more details on how to generate a code, refer to 14.2 MPLAB Code Configurator(MCC) Code Generation.