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

Receiving command from Android app. and handling in nrf51822

Hi Nordic community, I'm sending several packets from nrf51822 (custom board) to Android application. At the moment I want to configure the sensor by sending commands from android to nrf51822. I have a difficulty in how to receive the command over ble and send it to uart and handle. any recommendation is appreciated. please, the code for sending the data are attached. Best regards, Mostafa

void initAll()
{
		initMCU();
		Leds_init();
		Leds_setColor(1, 1, 1);
		Twiface_init();
		#if 0
		Twiface_scanForDevices(
			MCU_I2C_SDA, MCU_I2C_SCL, TWI_FREQUENCY_FREQUENCY_K100);
		Twiface_scanForDevices(
			MCU_BRD_RTS_I2C2_SDA, MCU_BRD_CTS_I2C2_SCL, TWI_FREQUENCY_FREQUENCY_K100);
		#endif
		initNrfSwModules();
		#if 1
		Sensors_init();
		initExtmem();
		//Datalog_init();
		Sampler_init();
		#endif
		Ble_control_init();   (service is initiliazed here)
		//Rtc_init();
		ssd1306_init();


////////////////////////////////
void Ble_control_init(void)
{
    ble_stack_init();
    device_manager_init(true);
    gap_params_init();
    advertising_init();
		initBatteryService();
		initDeviceInformationService();
		initDFUService();
    nus_services_init();
    conn_params_init();

    // Start execution.
    // Initialize advertising parameters (used when starting advertising).
    memset(&m_adv_params, 0, sizeof(m_adv_params));
    m_adv_params.type        = BLE_GAP_ADV_TYPE_ADV_IND;
    m_adv_params.p_peer_addr = NULL;                           // Undirected advertisement.
    m_adv_params.fp          = BLE_GAP_ADV_FP_ANY;
    m_adv_params.interval    = APP_ADV_INTERVAL;
    m_adv_params.timeout     = APP_ADV_TIMEOUT_IN_SECONDS;
		advertising_start();
    //err_code = sd_ble_gap_adv_start(&m_adv_params);
    //err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    //APP_ERROR_CHECK(err_code);
}
/////////////////////////////////////
void readNoise(bool isConnected)
{
	AistinItem ai;
	nrf_delay_ms(10);
	memset(withSizeof(ai.data._B750.noise), 0x00);
	Aistin_initItem(&ai, AISTIN_ID_B750_NOISE);
	sound_sensor_adc_start();
	ai.data._B750.noise[1] = soundLevel & 0xFF;
	ai.data._B750.noise[0] = (soundLevel >> 8) & 0xFF;
	ai.dataSize = sizeof(ai.data._B750);	
	if (isConnected) Protocol_sendBle(&ai);
////////////////////////////////////////////
void sendBle(uint8_t *data, int dataSize, uint16_t maxTimeToTry)
	{
		int tries = 0;
		int32_t err;
		uint16_t start = Timing_read32768Hz16();
		uint16_t elapsed;
		do {
			tries++;
			err = ble_nus_string_send(&m_nus, data, dataSize);
			if (err == NRF_SUCCESS) break;
			elapsed = Timing_read32768Hz16() - start;
			sd_app_evt_wait();
		} while (elapsed < maxTimeToTry);
		if (err != NRF_SUCCESS) {
			debugf("%d", tries);
		}
		else {
			if (tries > 1) debug(":");
		}
	}

//////////////////////////////////
void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
	{	
			debugln(__func__);
			Protocol_handleMessage(p_data, length);
	}
///////////////////////////////////////
void Protocol_handleMessage(uint8_t *msg, uint16_t len)
{
	if (len >= sizeof(AistinCommand) - 1) {
		debugln("*** cmd!");
		AistinCommand *ac = (AistinCommand*)msg;
		uint16_t id16 = (ac->dataID[0]<<8) | ac->dataID[1];
		if (id16 == AISTIN_ID_8607_LOGGER_CONFIG) {
			debuglnf("- log: %d", ac->data[0]);
			if (!(ac->data[0] & _AISTIN_8607bit__LOG) && Datalog_isLogging())
				Datalog_stopLogging();
			else
			if ((ac->data[0] & _AISTIN_8607bit__LOG) && !Datalog_isLogging())
				Datalog_startNewLog();
		}
	}
}
//////////////////

this is the code related to send sensor data and working well

Parents Reply
  • Hi Ketil, Thanks for the answer. The board that I'm using is custom. S110 is used for soft device and due to differnt Rx and Tx pin am not able to use examples and see the results. As I've chacked many examples, I need a sending and receiving data at for both which this example was not able to find. Rx =28 and Tx=29, Please how to change the example configuration to match it.

    how to use data_handler function for sending and as well receiving? Regards, Mostafa

Children
No Data
Related