Hi,
I'm using the Nordic DK52 (52832) along with SDK 15.0 to prototype an application where I can send some data from a sensor attached via UART to an BLE-enabled application. I'm using nRF Toolbox with the Serial feature to test whether I'm getting any data sent from the sensor. I am using SESV4.22 for the development of the application.
I've started out with the ble_app_uart example and attempted to modify it to my needs. Below I'm providing code snippets where I changed the functions; everything else is unchanged:
//Send this to the sensor to initiate a measurement
static uint8_t const readLiveDataCmd[] = {0x16, 0x13, 0x2D, 0x10, 0x1F, 0x00, 0x7E};
//Don't care if data is sent to the board
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
/*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);
for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
{
do
{
err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
}
/*if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
{
while (app_uart_put('\n') == NRF_ERROR_BUSY);
}*/
//}
}
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint8_t index = 0;
float temperature = 0;
float pressure = 0;
uint8_t data[64];
uint32_t err_code;
switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++;
if ((data_array[index - 4] == (uint16_t) 0x10) && (data_array[index-3] == (uint16_t) 0x1F))
{
NRF_LOG_DEBUG("Got Sensor Readings");
NRF_LOG_HEXDUMP_DEBUG(data_array, index);
union {
char c[4];
float f;
} u;
u.c[0] = data_array[7];
u.c[1] = data_array[8];
u.c[2] = data_array[9];
u.c[3] = data_array[10];
pressure = u.f;
u.c[0] = data_array[11];
u.c[1] = data_array[12];
u.c[2] = data_array[13];
u.c[3] = data_array[14];
temperature = u.f;
/*Send the temp and pressure data to the BLE*/
do
{
sprintf((char*) data,"Temperature: %f; Pressure: %f", temperature, pressure);
uint16_t length = (uint16_t)sizeof(data);
err_code = ble_nus_data_send(&m_nus, data, &length, m_conn_handle);
if ( (err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_BUSY) &&
(err_code != NRF_ERROR_NOT_FOUND) )
{
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
index = 0;
}
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
//main
int main(void)
{
bool erase_bonds;
// Initialize.
uart_init();
log_init();
timers_init();
buttons_leds_init(&erase_bonds);
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
// Start execution.
//printf("\r\nUART started.\r\n");
NRF_LOG_INFO("Debug logging for UART over RTT started.");
advertising_start();
// Enter main loop.
for (;;)
{
//idle_state_handle();
uint32_t err_code;
for(uint8_t i = 0; i < sizeof(readLiveDataCmd); i++)
{
do
{
err_code = app_uart_put(readLiveDataCmd[i]);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
if(err_code == 0x04)
{
break;
}
else
{
APP_ERROR_CHECK(err_code);
}
}
} while (err_code == NRF_ERROR_BUSY);
}
}
}When I run the app as is, I can see the advertisement in the nRF Toolbox and I can connect to the board, but I get nothing transmitted from the sensor. When I try to debug the application, I get an error 0x04 returned a from the app_uart_put() function.
What did I miss in my program?