Toggling a gpio pin upon receiving a particular string from ble uart app
Toggling a gpio pin upon receiving a particular string from ble uart app
Hi,
The data sent from the smartphone is handled in the function called nus_data_handler()
, that you can find in main.c. In the code below, we look for the string “ON” and “OFF”, configure pin 4 as an output pin and set the pin high when we receive “ON” and set the pin low when we receive “OFF”. After that the data is sent to the UART module as normal.
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
{
static char uart_string[BLE_NUS_MAX_DATA_LEN];
memset(uart_string,0,BLE_NUS_MAX_DATA_LEN);
memcpy(uart_string, p_data, length);
if(strcmp(uart_string,"ON") == 0)
{
nrf_gpio_cfg_output(4);
nrf_gpio_pin_set(4);
}
if(strcmp(uart_string,"OFF") == 0)
{
nrf_gpio_cfg_output(4);
nrf_gpio_pin_clear(4);
}
for (uint32_t i = 0; i < length; i++)
{
while (app_uart_put(p_data[i]) != NRF_SUCCESS);
}
while (app_uart_put('\r') != NRF_SUCCESS);
while (app_uart_put('\n') != NRF_SUCCESS);
}