nRF_SDK_17.1.0 s140 pca10056 BLE_UART

Hello all,

I am trying to develop an application where the user will input some specific commands i.e hex value of 0x01 via a mobile application and then via bluetooth this command will be passed to the computer using UART.

I have compiled the ble app uart on my nrf52840 but i am struggling to modify the code for my application.

Could someone help me with this ? 

Thanks

Kleanthis

  • kleanthise said:
    Ideally yes i would like to use the unmodified BLE NUS service, check the contents of incoming packets and se the DEVICE_ACTIVATED variable in case the 0x01 command is received.

    Great, thank you for confirming this.
    In this case, I recommend that you just add this as a check in before the app_uart_put like I mentioned earlier. In your case I suppose it would look something like this:

    ..
    #define COMMAND_ACTIVATION 0x01
    ..
        if (p_evt->type == BLE_NUS_EVT_RX_DATA)
        {
            uint32_t err_code;
    
            NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
            NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
            
            if(p_evt->params.rx_data.p_data[0] == COMMAND_ACTIVATION)
            {
                m_device_state = 1;
            }
            
            if(m_device_state)
            {
                ..    
            }
            else
            {
                NRF_LOG_INFO("Device not activated.");
            }
    ..


    However, you should also make sure that your phone application is not sending any unexpected ASCII control characters, so that you can be sure that these specific command bytes will only ever be sent intentionally.

    kleanthise said:
    I have no uncommented the original code and i am back on the defaul ble_nus.c and h files. In the real case scenario i would like to be able to keep the original BLE NUS Service functionality alongside the activate command since i would be receiving commands from a pc using the uART protocol ( those will be an array of 0 and 1) and  i will just showing them on a mobile application

    Please take care to make sure that the NUS service ble_nus.c and ble_nus.h files are reverted to their original state. If you at any point later notice some unexpected behavior with the NUS service I would download a new SDK instance and replace these files with the unmodified ones, just to make sure that there are no unexpected changes.

    Best regards,
    Karl

  • Thank you very much . 

    This is exactly what i wanted to do. This will send the command the my device is now ready for operation. Now if i want to send that 0x01 command via UART to my PC. Shall i use the f unction uart_event_handle or is that possible again via nus_data_handler ? 

  • kleanthise said:

    Thank you very much . 

    This is exactly what i wanted to do.

    Great, I am happy to hear that!

    kleanthise said:
    Now if i want to send that 0x01 command via UART to my PC. Shall i use the f unction uart_event_handle or is that possible again via nus_data_handler ? 

    If you wish for the peripheral to output something through its UART you can use the app_uart_put function to do so.

    Best regards,
    Karl

  • Sorry for being so naive but when i try to run the code is giving me an error of 'm_device_state' undeclared (first use in this function) . Shall i declare that variable as a hex on the beginning of my code ? or i can assign it as a uint16_t m_device_state  ? 

  • kleanthise said:
    Sorry for being so naive but when i try to run the code is giving me an error of 'm_device_state' undeclared (first use in this function)

    No need to apologize! :) Yes, the code I provided was meant more as an example of how you could implement this functionality rather than an actual implementation - so some key parts, like the variable declaration, were not included.
    It could look something like this, someplace towards the top of your main.c file:

    static uint8_t m_device_state = 0;

    You will also need to implement a function or code section that resets this variable to 0 again, since the device probably should not remain activated for perpetuity when you have first activated it.
    You will need to determine what event or happening in your application that should set this to 0. Perhaps the _DISCONNECTED event, or a deactivate command, or both?

    Best regards,
    Karl

Related