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

Cant send 20 bytes of data correctly

I have been dealing with this for 6 months and im running low on time to finish this project please help me. I want to send 20 bytes of data to a application that i made for my phone with ble. I get the data from sensor's with twi if i did this project in two parts .First i made a project for twi and it worked everything was fine but when i use the same TWI code for ble data sending the TWI part does not work it does not read data at timeout_handler part .My main issue today is that i can send 20 bytes of data correctly and cant update it .I can see everything ( service ,character..) but i cant update the data inside the character. Like it sends random data every time at the begining and does not update by my sensor.

--------------------my data descriptions----------------------------------------

static uint8_t m_custom_value = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};

static uint16_t fall;

--------------------- this is my timeout handler ---------------------------------

static void notification_timeout_handler(void * p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code;
bsp_board_led_on(3);
nrf_delay_ms(100);
bsp_board_led_off(3);

MPU6050_FallDet(fall);
m_custom_value = fall;



err_code = ble_cus_custom_value_update(&m_cus, m_custom_value);
APP_ERROR_CHECK(err_code);
}


--------------------- this is value_update function---------------------------------

uint32_t ble_cus_custom_value_update(ble_cus_t * p_cus, uint8_t custom_value)
{
NRF_LOG_INFO("In ble_cus_custom_value_update. \r\n");
if (p_cus == NULL)
{
return NRF_ERROR_NULL;
}

uint32_t err_code = NRF_SUCCESS;
ble_gatts_value_t gatts_value;

// Initialize value struct.
memset(&gatts_value, 0, sizeof(gatts_value));

gatts_value.len = sizeof(custom_value);
gatts_value.offset = 0;
gatts_value.p_value = &custom_value;

// Update database.
err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
p_cus->custom_value_handles.value_handle,
&gatts_value);
if (err_code != NRF_SUCCESS)
{
return err_code;
}

// Send value if connected and notifying.
if ((p_cus->conn_handle != BLE_CONN_HANDLE_INVALID))
{
ble_gatts_hvx_params_t hvx_params;

memset(&hvx_params, 0, sizeof(hvx_params));

hvx_params.handle = p_cus->custom_value_handles.value_handle;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
hvx_params.offset = gatts_value.offset;
hvx_params.p_len = &gatts_value.len;
hvx_params.p_data = gatts_value.p_value;

err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code);
}
else
{
err_code = NRF_ERROR_INVALID_STATE;
NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n");
}


return err_code;
}


-------------------------------------main.c file-------------------------------------


void MPU6050_FallDet(int16_t *Fall){

static int16_t ACCValue[3], GYROValue[3];
static int16_t VAccValue[3], VGyroValue[3];
static int16_t RAccValue[3], RGyroValue[3];
static int16_t cosAccValue[3], cosGyroValue[3];
static int16_t RACC,RACC2;

MPU6050_ReadAcc(&ACCValue[0], &ACCValue[1], &ACCValue[2]);

VAccValue[0]=ACCValue[0]*3.3/1023;
VAccValue[1]=ACCValue[1]*3.3/1023;
VAccValue[2]=ACCValue[2]*3.3/1023;

RAccValue[0]=(VAccValue[0]-1.65)/0.47;
RAccValue[1]=(VAccValue[1]-1.65)/0.47;
RAccValue[2]=(VAccValue[2]-1.65)/0.47;

RACC=RAccValue[0]^2+RAccValue[1]^2+RAccValue[2]^2;
RACC2=RACC^2;

cosAccValue[0]=RAccValue[0]/RACC2;
cosAccValue[1]=RAccValue[1]/RACC2;
cosAccValue[2]=RAccValue[2]/RACC2;


NRF_LOG_INFO("MPU6050 :COS: x = %d y = %d z = %d", cosAccValue[0], cosAccValue[1], cosAccValue[2]);
NRF_LOG_FLUSH();

if( cosAccValue[1] == 1|cosAccValue[1] == -1){
nrf_delay_ms(10);
if( cosAccValue[1] == 1|cosAccValue[1] == -1){
*Fall = 2;
}}
if( cosAccValue[2] == 1|cosAccValue[2] == -1){
nrf_delay_ms(10);
if( cosAccValue[2] == 1|cosAccValue[2] == -1){
nrf_delay_ms(10);
if( cosAccValue[2] == 1|cosAccValue[2] == -1){
*Fall = 1; }}}

return ;
}

---------------------------------------------------------------

maybe i wasnt clear enough but please try to help me i cant think any other think to do i am out of ideas i looked every post at the devzone about ble and i cant solve this.

  • Hello,

    I see that you have created multiple tickets on this subject, and that you are getting help from my colleague Kenneth on this matter, so I will close this ticket as a duplicate.
    I also notice that it seems your code has changed somewhat since you posted this ticket, so the following might not still apply, but still could be useful for you to see:

    Looking briefly thought the provided code I thought I should mention that you seem to make (at least) two mistakes:

    --------------------my data descriptions----------------------------------------

    First of all, your array initialization is off. You need to tell the compiler to allocate enough memory for the array. This might be the reason why you are seeing 'random data' when trying to use this later on in your application.
    Secondly, your MPU6050_FallDet expects a uint16_t pointer argument, but you are doing pass by value in your call to MPU6050_FallDet in notification_timeout_handler. So the Fall value is never updated or changed outside the scope of the function.

    You are also using a lot of magic numbers in your code. This drastically decreases readability and maintainability, and you might want to change this in the future.
    Instead you should make use of defines and macros for all these numbers. It will make it easier to understand what you are accessing and or doing in your expressions, and it will make it possible to change it later on without requiring significant work.

    Keep in mind that I just looked briefly through the provided code, since you are already working with my colleague Kenneth on this, and these are just my initial thought.

    Good luck with your development!

    Best regards,
    Karl

  • I am trying to move this as quickly as i can so i am runing 2 projects at once so please dont see this as duplicate i really need all the help i can get this is another project that i am working with they are kind of similar but different projects.

    static uint8_t m_custom_value[17] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
    I decleared the function like this and added the pointers so my timeout_handler became something like this 
    static void notification_timeout_handler(void * p_context)
    {
        UNUSED_PARAMETER(p_context);
        ret_code_t err_code;
        bsp_board_led_on(3);
        nrf_delay_ms(100);
        bsp_board_led_off(3);
      
      MPU6050_FallDet(*fall);
      m_custom_value[0] = *fall;
    
    
    
        err_code = ble_cus_custom_value_update(&m_cus, m_custom_value);
        APP_ERROR_CHECK(err_code);
    }
    
    this time it connects to nrf connect application and when i try to enable notifications on it allows that but when i hit the read button near the notifications to read the value that i send it just disconnects and breaks down so i have to restart it again.

    I am thankfull for all your help and i will try to use less magic numbers i am learning to write code with this project so every information is helpfull. Please let me know anything comes on your mind about this notification and read issue . 

    Thanks again

    Best regards,

    Efe

  • Hello Efe,

    kefom said:
    i am runing 2 projects at once so please dont see this as duplicate i really need all the help i can get this is another project that i am working with they are kind of similar but different projects.

    Ok, I suppose that is fine then, as long as the questions and issues differ. The descriptions of the problem and title etc. seemed almost identical, but I suppose the issue you describe in your most recent comment is different.

    kefom said:
    I decleared the function like this and added the pointers so my timeout_handler became something like this 

    Glad to see that you fixed your array initialization!
    You are again confusing pointers, references and values in your notification_timeout_handler.
    It might help for you to try and have the values printed to the logger, so that you could see their actual contents and compare it to what you attempted to send. You are updating the first element of the m_custom_value array to contain the pointer to fall(not the current value of Fall, since Fall is just a variable). Furthermore, in your call to ble_cus_custom_value_update you are providing m_cusom_value as your second argument - since this is an array, you are actually just passing the pointer to the first memory allocation for this array, i.e not a value.
    Your ble_cus_custom_value_update function takes a uint8_t as its second argument, so it is expecting a byte with the value 0-255, and nothing else. I would guess that the compiler should at least complain about this, since you are providing a non-uint8_t argument to the function. Do you have any compiler warnings when you compile you code? If so, please resolve all of these before proceeding. Every warning the compiler comes with are there for a reason.

    kefom said:
    to read the value that i send it just disconnects and breaks down so i have to restart it again.

    This might have something to do that you are not in fact sending a single value, but a pointer to the start of the whole array. Could you elaborate what you mean when you say that it 'breaks'? Are you getting any warnings or errors written to your logger, or does the device reset on its own?

    kefom said:
    I am thankfull for all your help and i will try to use less magic numbers i am learning to write code with this project so every information is helpfull.

    I am happy to hear that you are taking my suggestions to heart! :) 
    This will definitely improve your code, and avoid a lot of potential future headaches.

    Best regards,
    Karl

  • Thanks for the advice on pointers and all i made my code much more easier to understand now and now i am taking a value and adding it to m_custom_value[0] so it should be all right i think but now its just giving fatal error about twi communication 

    nrfx_err_t nrfx_twim_tx(nrfx_twim_t const * p_instance,
                            uint8_t             address,
                            uint8_t     const * p_data,
                            size_t              length,
                            bool                no_stop)
    {
        nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(address, (uint8_t*)p_data, length);
    
        return nrfx_twim_xfer(p_instance, &xfer, no_stop ? NRFX_TWIM_FLAG_TX_NO_STOP : 0);
    }
    
    
    
    // at this line     nrfx_twim_xfer_desc_t xfer = NRFX_TWIM_XFER_DESC_TX(address, (uint8_t*)p_data, length);
    
    
    i think the problem became the same problem that kennet is helping me with so i dont want to waste both of your times thanks for all good advices if you can think what might the problem be i am all ears 

    Thanks again this conversation helped a lot 

    Efe

  • Hello again Efe,

    kefom said:
    Thanks for the advice on pointers and all i made my code much more easier to understand now
    kefom said:
    Thanks again this conversation helped a lot

    Great! It is no problem at all, I am happy to hear that you found my advice helpful and that your code now is much easier to understand! :)

    kefom said:
    i think the problem became the same problem that kennet is helping me with so i dont want to waste both of your times thanks for all good advices if you can think what might the problem be i am all ears 

    Then I think it is best that you continue the discussion of the issue there, thank you for letting me know!
    On a general basis, I would recommend that you make sure DEBUG is defined in your preprocessor defines, like shown in the included image:

    This will make your logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK. The line you reference in your included code snippet above is just a macro, and as long as it gets the correct types of variables for its fields the compiler will not complain here.
    There might however be runtime errors from this, leading to a non-NRF_SUCCESS error code being returned from the call to nrfx_twim_xfer, which in turn likely is passed through an APP_ERROR_CHECK.
    This is just speculations on my part, so Kenneth will be better suited to help you with this in the other ticket.

    kefom said:
    Thanks again this conversation helped a lot 

    I am glad to hear you say that! Please do not hesitate to open a new ticket if you should encounter any other issues or questions in the future.

    Good luck with your development!

    Best regards,
    Karl

Related