This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52832 turn on specific led with specific button press on any of 3 slave boards and action on master board

I have developing one project in that i am using nRF52832 and SD 15.0. Here is the current application developing:

  1. There are 3 ble app_blinky(ble peripheral) example operated on battery and it connected to multilink central.
  2. Lets say ble peripheral name A, B, C and central device is master.
  3. Each ble peripheral- A interface simply 2 push switch and whenever it press digital signal goes to connected central and then ON particular GPIO LED.
  4. Same point two for other B and C ble peripheral. Therefore total LEDs on central side has 6 for each switch.
  5. Now i am able to connect 3 ble peripheral to central but not able to ON particular LED controlled by ble peripheral slave.

Problems:

  1. When switch press of device A , B or C then how i know in central whether it coming from A or B or C. I know connection handle link for each connection but i can do it will you please provide me program snippet for understanding.
  2. Once all switches (Total 6) press at same time then it will wake up all 3 peripherals A, B and C connect to central device. Want to ON all 6 LEDs in central at same time frame. How i can do it.

Thanks......

Parents
  • Dear Edvin! Thank you..

    I actually have 3 slave boards and 1 master board.Master board has 4 leds  and 4 buttons .Whereas each slave board has 2 leds and 2 buttons.What I want is pressing button1 from slave 1 makes led1 to glow on master board,likewise,button2 on slave2 will glow led2 on master board and so on.And on master board side if i press button1, led1 of slave board will glow and so on.

    Problem:

    The issue that I'm facing is I am not getting the point,how to recognize each slave on master side  and also even i button2 on slave board to glow another led,how to dintinguish between pressed buttons on master boards.

    Kind regards,

    Muhammad

  • in ble_app_blinky, look for the button_event_handler, and what it sends. In the unmodified example, it will only send either 0x00 or 0x01 depending on whether the button is pressed or not. If you want to include more buttons, you must also include the pin number in this data. You only have 1 byte (uint8_t), so maybe you can send a button matrix:

    button 1 pressed: 0x01 = 0b00000001

    button 2 pressed: 0x02 = 0b00000010

    button1 and 2 pressed: 0x03 = 0b00000011

    On the central, ble_app_multilink_central you must interpret this data on the lbs_c_evt_handler() -> case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:

    button_state is the byte that you send from the peripheral (ble_app_blinky). Toggle the led according to this byte. To see which peripheral that sent the message, look at the conn_handle (connection handle), as I described in the previous reply. Each connected peripheral will have their own conn_handle, which you can find in the event: p_lbs_c_evt->conn_handle;

Reply
  • in ble_app_blinky, look for the button_event_handler, and what it sends. In the unmodified example, it will only send either 0x00 or 0x01 depending on whether the button is pressed or not. If you want to include more buttons, you must also include the pin number in this data. You only have 1 byte (uint8_t), so maybe you can send a button matrix:

    button 1 pressed: 0x01 = 0b00000001

    button 2 pressed: 0x02 = 0b00000010

    button1 and 2 pressed: 0x03 = 0b00000011

    On the central, ble_app_multilink_central you must interpret this data on the lbs_c_evt_handler() -> case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:

    button_state is the byte that you send from the peripheral (ble_app_blinky). Toggle the led according to this byte. To see which peripheral that sent the message, look at the conn_handle (connection handle), as I described in the previous reply. Each connected peripheral will have their own conn_handle, which you can find in the event: p_lbs_c_evt->conn_handle;

Children
  • Sorry I couldn't come earlier..

    Dear Edvin,

                       I got what you described,but can you please tell more in detail about this handling.How will I be able to get that button value and how I assign that in the central connection.I am totally new,I need to know little more.I hope,you will make me clear.

    I got to this lbs_c_evt_handler() -> case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:

    But what more to do with it,suppose I got some value (That i even don't know where  to get,if i add extra button),but suppose I got this,then what shoud I do with it in this event handler.

    Thank you!

    With regards

  • you must study the function that is called when you press a button on the ble_app_blinky example. This functino is called button_event_handler.

    The function that actually sends the information over BLE is ble_lbs_on_button_change(m_conn_handle, &m_lbs, button_action);

    Where m_conn_handle is just the connection handle to the central. m_lbs is the service that you are updating, and button_action is the actual data that you are sending. This is the one you want to modify if you want to add more buttons.

    So. In button_event_handler, the reason you get the event LEDBUTTON_BUTTON when you press the button is that this button is set up in buttons_init():

        static app_button_cfg_t buttons[] =
        {
            {LEDBUTTON_BUTTON, false, BUTTON_PULL, button_event_handler}
        };

    If you want to add more buttons, just add lines with the buttons:

        static app_button_cfg_t buttons[] =
        {
            {LEDBUTTON_BUTTON, false, BUTTON_PULL, button_event_handler},
            {LEDBUTTON_BUTTON2, false, BUTTON_PULL, button_event_handler}
        };

    and add this case 

    LEDBUTTON_BUTTON2

    in button_event handler. Note that LEDBUTTON_BUTTON is defined as BSP_BUTTON_0 -> BUTTON_1 (in pca10040.h) -> 13

    If you define LEDBUTTON_BUTTON2 as BSP_BUTTON_1, it will be the second button.

    Suggestion for button_event_handler:

    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
        uint8_t button_state;
    
        switch (pin_no)
        {
            case LEDBUTTON_BUTTON:
            case LEDBUTTON_BUTTON2:
            
            if(button_action)       //meaning a button is pressed
            {
                button_state |= 1 << (pin_no - LEDBUTTON_BUTTON);
            }
            else                    //meaning a button is released
            {
                button_state &= ~(1 << pin_no - LEDBUTTON_BUTTON);
            }
            
                NRF_LOG_INFO("Send button state change.");
                err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, button_state);
                if (err_code != NRF_SUCCESS &&
                    err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
                    err_code != NRF_ERROR_INVALID_STATE &&
                    err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
                {
                    APP_ERROR_CHECK(err_code);
                }
                break;
    
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    }

    If there are a lot of new terms for you, such as peripheral and central I suggest you read up on BLE first.

    Then, in the ble_app_multilink_central example, you must check the lbs_c_evt_handler(). You will now see that the button states in the BLE_LBS_C_EVT_BUTTON_NOTIFICATION event, you will now see that the p_lbs_c_evt->params.button.button_state equals either 0, 1, 2 or 3, depending on the button states on the connected devices. You can also see that the conn_handle depends on which connected board you press the buttons on.

    So if you want to do something when all connected devices press both buttons, you must ensure that you have button_state = 3 on all connected devices (all conn_handles).

    You can experiment by watching the log on the central:

    NRF_LOG_INFO("Link 0x%x, button state changed on peer to 0x%x, p_lbs_c_evt->conn_handle, p_lbs_c_evt->params.button.button_state);

    This will tell you the conn_handle, and the button_state whenever it changes on one of the connected devices.

    Best regards,

    Edvin

  • Dear Edvin,Thank you so much! It worked for me...

Related