Toggling an external LED via BLE

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Toggling an external LED via BLE

771 Views
johanandrade
Contributor III

Hey folks,

Trying to get a starting point for a bluetooth project. I want to build a project toggling on/off an external LED connected to my R41Z (based of KW41Z board) using an Android phone app.

  • R41Z board
  • External LED wired to PTB18 GPIO
  • SDK v2.2 (4-16-18) with bluetooth v.1.2.5 stack
  • Android phone app built using Android Studios 3.1.3

My questions are:

  • Should I use the beacon bluetooth project as a base? If not, what would you suggest to start with?
  • If using the beacon project, which files should I be looking at modifying for the LED functionality, signal advertising, receiving the message from the phone app, etc.?
  • When it comes to the phone app, are there certain IDs that are within the beacon project that have to be used on the code for the phone app? I see the file BLE_SIG_DEFINES_H has GATT declarations but I'm not too familiar yet how these play a role within the phone app.

Thanks.

Labels (2)
0 Kudos
2 Replies

561 Views
johanandrade
Contributor III

Getting started with the following setup for the external LED. I have the beacon bluetooth stack loaded on the R41Z board.

gpio_pins.c

/* Declare Output GPIO pins */
gpioOutputPinConfig_t ledPins[] = {
{

.

.

.

{
.gpioPort = gpioPort_C_c,
.gpioPin = 18,
.outputLogic = 1,
.slewRate = pinSlewRate_Slow_c,
.driveStrength = pinDriveStrength_High_c
}
};

gpioOutputPinConfig_t appExternalLed = {
.gpioPort = gpioPort_B_c,
.gpioPin = 18,
.outputLogic = 1,
.slewRate = pinSlewRate_Slow_c,
.driveStrength = pinDriveStrength_Low_c
};

-------

gpio_pins.h

.

.

.

extern gpioInputPinConfig_t switchPins[];
extern gpioOutputPinConfig_t ledPins[];
extern gpioOutputPinConfig_t appExternalLed;

------

pin_mux.c

#include "gpio_pins.h"

/*FUNCTION**********************************************************************
*
* Function Name : APP_InitExternalLED
* Description : Configures gpio output for external LEDs
* Called in LED.c file to initialize
*
*END**************************************************************************/

void APP_InitExternalLED(void) {
GpioOutputPinInit(&appExternalLed, 1);

GpioClearPinOutput(&appExternalLed); // LED is off at start up
}

------

pin_mux.h

/*!
* @brief Configures pin routing and optionally pin electrical features.
*
*/
void APP_InitExternalLed(void);

-------

LED.c

/******************************************************************************
******************************************************************************
* Public functions
******************************************************************************
*****************************************************************************/

/******************************************************************************
* Name: LED_Init
* Description: Initialize the LED module
* Parameters: -
* Return: -
******************************************************************************/
void LED_Init
(
void
)
{
APP_InitExternalLED();
.

.

.

BOARD_InitLEDs();
(void)GpioOutputPinInit(ledPins, gLEDsOnTargetBoardCnt_c);

.

.

I think that sets up my LED connection and initialization at PTB18. I'm trying to understand how I can detect the advertisement from the board on a custom BLE mobile app. The only way I can see the device from my mobile phone is using the IoT Toolbox app NXP and booting up the Beacon program within. What would I need to modify in the beacon software stack to see the BLE advertisement on a custom BLE mobile app?

I have a template BLE app "BluetoothLeGatt" provided from Android Studios which just does a scan of nearby devices and currently nothing shows up.

pastedImage_1.png

I also have a LEDController Sample App but it first needs to pair to the R41Z device before I can use the on/off functionality.

pastedImage_2.png

pastedImage_4.png

After I am able to pair the devices the next step would be to read the message of a "HIGH" or "LOW" signal sent from the mobile app.

The functions in the LEDControllerSample app sending these commands are defined as following:

private void turnOffLed()
{
    if (btSocket!=null)
    {
        try
        {
            btSocket.getOutputStream().write("0".toString().getBytes());
        }
        catch (IOException e)
        {
            msg("Error");
        }
    }
}

private void turnOnLed()
{
    if (btSocket!=null)
    {
        try
        {
            btSocket.getOutputStream().write("1".toString().getBytes());
        }
        catch (IOException e)
        {
            msg("Error");
        }
    }
}

Also, in the source code of the mobile app there is this line:

static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

Does this UUID need to be changed to the matching R41Z board?

0 Kudos

561 Views
mario_castaneda
NXP TechSupport
NXP TechSupport

Hi Johan,

 

  • Should I use the beacon Bluetooth project as a base? If not, what would you suggest to start with?

It will depend on your application, as you know the beacon is sending advertisements packets and the central device, in this case, the Phone will only scan that packets without a connection.

  • If using the beacon project, which files should I be looking at modifying for the LED functionality, signal advertising, receiving the message from the phone app, etc.?

Yes, you can create your own custom advertisement packet 

  • When it comes to the phone app, are there certain IDs that are within the beacon project that has to be used on the code for the phone app? I see the file BLE_SIG_DEFINES_H has GATT declarations but I'm not too familiar yet how these play a role within the phone app.

The same, it will depend on your custom advertisement, what do you want to send and the way that you will process that information in your app.

  • Does this UUID need to be changed to the matching R41Z board?

it will depend on your phone app, if the application is scanning just that particular UUID, the R41Z should be the same.

Regards,

Mario

0 Kudos