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

Hello all i want to interface temperature sensor with nRF52 DK. And send the temperature values to the users smart phone. I have checked with the tutorial and have used the sd_temp_get() function to get the temperature using the on board sensor.

 nd Since i am still a newbie to nordic i wanted to know how can i break the task in smaller steps. 

1)As a first step i get the values in temperature form using saadc. 

2)How can i notify the value just as it does after calling for sd_temp_get().

3)As a 3rd question I have already interfaced the battery management IC with nrF52 and extracted the required bit. I wish to send that bit and read/notify it on one of the characteristics. But the value doesnt get updated.

Any inputs would be helpful 

Parents
  • Hello,

    From your description it seems to me that you have successfully acquired the sensor value, and now just seek to send it to a connected smartphone, could you confirm that this is correct?

    How can i notify the value just as it does after calling for sd_temp_get().

    Which example are you working out of? To send the value to a connected device you can use a notification.

    You mention in your title (which you should keep to a descriptive minimum, not a body of text, by the way) that you have checked with "the tutorial", which tutorial are you referring to here?
    To learn more about how you can setup your own custom service, and configure it to notify its peer if the value changes, I highly recommend taking a look at the Beginners Service tutorial

    As a 3rd question I have already interfaced the battery management IC with nrF52 and extracted the required bit. I wish to send that bit and read/notify it on one of the characteristics. But the value doesnt get updated.

    How are you attempting to do this now, and what do you observe when you attempt this? Is there an error code generated, or does the device reset? Does the call to send the notification return successfully?
    It would be great if you could share some code here, and explain what you attempted, what you expected it to do, and how it failed.
    The more detail you provide, the easier it will be for us to resolve this issue together!
    Please use the "Insert->Code" option when sharing code here on DevZone.

    Best regards,
    Karl 

  • Hello Karl Thanks for replying .

     I would like to start with 3rd question first. 

    The Battery management IC has been connected using TWI.  

    I have mentioned how i have extracted the charging bit and how have i implemented the BLE part. I have followed the exact steps provided in tutorial for Ble custom characteristics and i can see the value of temperature on nRF connect  APP. The problem that i am facing is if i use the get_charging_bit_1(&charging_bit) in timer timeout handler the program compiles properly but the device(nordic blinky) doesnt appear on the nRFconnect App. If i comment get_charging_bit_1 out from timer timeout handler i can see the device and all my characteristics on nRFconnect App.  

    uint8_t charging_check(void)
     {
      uint8_t receive_buffer;
      uint8_t charging;
      uint8_t charging_done;
      b_register_read(BQ_SAT_0_REG_ADDR, &receive_buffer, 1);
      NRF_LOG_INFO("receive_buffer is %x", receive_buffer);
      NRF_LOG_FLUSH();
      if ((receive_buffer==65) || (receive_buffer==1))         // 65 is decimal equivalent of 41 in hex 
      {
       NRF_LOG_INFO("Charging");
       NRF_LOG_FLUSH();
       charging = 1;
       charging_done = 0;
       
       NRF_LOG_INFO("Battery is charging, charging = %d", charging);
       NRF_LOG_FLUSH();
      
      }
      else if (receive_buffer==33)                             //33 is decimal equivalent of 21 in hex
      {
       NRF_LOG_INFO("Charged");
       NRF_LOG_FLUSH();
       charging_done = 1;
      
       NRF_LOG_INFO("Charging completed %d", charging_done);
       NRF_LOG_FLUSH();
      }
      return charging_done;
     }
    
    extern int8_t get_charging_bit_1(int*charging_bit)
    { 
     {int temp;
     temp=charging_check();
     *charging_bit=temp;
     }
     
    }
    
    
    
    void charging_characteristic_update(ble_cus_t *p_cus, int32_t *charging_bit)
    {
        // OUR_JOB: Step 3.E, Update characteristic value
        if (p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)
      {
          uint16_t               len = 4;
          ble_gatts_hvx_params_t hvx_params;
          memset(&hvx_params, 0, sizeof(hvx_params));
    
          hvx_params.handle = p_cus->custom_value_handles_2.value_handle;
          hvx_params.type   = BLE_GATT_HVX_NOTIFICATION;
          hvx_params.offset = 0;
          hvx_params.p_len  = &len;
          hvx_params.p_data = (uint8_t*)charging_bit;  
    
          sd_ble_gatts_hvx(p_cus->conn_handle, &hvx_params);
    
    
    
      }
    
     
     
     static void timer_timeout_handler(void * p_context)
    {
     
       int8_t charging_bit = 0;
       static int8_t previous_charging_bit=0;
       get_charging_bit_1(&charging_bit);
       if(charging_bit != previous_charging_bit)
                       {
                           
                           charging_characteristic_update(&m_cus, &charging_bit);
                       }
    }
     

  • Hello again PaSi,

    This means that you try to call a SoftDevice function before the SoftDevice is enabled.
    Please go back and answer my question in regards to the 50 µs you mentioned earlier.
    I think this might be the issue is - if this function is called every 50 µs, and called before the SoftDevice is enabled, you will get this error.

    Your timers_init comes before the ble_stack_init, so if it triggers immediately it will try to do a sd_temp_get, but fail since the SoftDevice is not yet enabled.

    Please read through my previous comments, and answer the missed questions.

    In general, when a non-NRF_SUCCESS error code is returned by a function, you should look into that functions API reference to see why it returned this particular error code.

    Best regards,
    Karl

  • Hi Karl,

    I am not calling the function get_char_bit1()  every 50 micro seconds. 

    As i said the program is working perfectly fine with sd_temp_get() i can see values changing but as soon as i call get_char_bit function in timer timeout handler nRF connect App doesnt show nordic blinky. I had a question here do we need a differen timer timeout for sd_temp_get() and get_char_bit() or it can be implemented in same timer timeout handler.

    Yes the timer_init()  is called before ble_stack_init

    https://devzone.nordicsemi.com/nordic/short-range-guides/b/bluetooth-low-energy/posts/ble-characteristics-a-beginners-tutorial here the app_tmers_start() is called after advertising_start in solution folder .

    int main()
    
    { log_init();
    
    leds_init();
    timers_init();
    
    // buttons_init();
    power_management_init();
    ble_stack_init();
    pwm_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    
    // Start execution.
    NRF_LOG_INFO("Blinky example started.");
    application_timers_start(); // This one i am talking about 
    advertising_start();
    
    }
    Thanks a lot!

    Best Regards,

    PaSi

  • Hello PaSi,

    PaSi said:
    I am not calling the function get_char_bit1()  every 50 micro seconds. 

    Thank you for clarifying - I did not think so, but your phrasing left some ambiguity and I thought it best to check rather than assume. 

    PaSi said:
    I had a question here do we need a differen timer timeout for sd_temp_get() and get_char_bit() or it can be implemented in same timer timeout handler.

    That is a good question - you should always make sure to keep interrupt handlers as short as possible, since they interrupt the regular program flow. In this case, the call to sd_temp_get is blocking, and it might block for as long as 50 µs each time it is called - which means a lot of time could be wasted here if you end up waiting for it each call.
    How often do you intend to get these temperature measurements?
    Could you possibly make these calls in your main context instead, which updates a kept variable, and then have the timeout handler do whatever it is you intend to do with the temperature data when it times out, for example? This would greatly reduce the time spent waiting for the sd_temp_get function to return. 

    PaSi said:
    here the app_tmers_start() is called after advertising_start in solution folder .

    Yes, it seemed to me that you were starting your timers before having finished the configuration, my mistake.

    PaSi said:
    as soon as i call get_char_bit function in timer timeout handler nRF connect App doesnt show nordic blinky.

    Are you getting the SOFTDEVICE_NOT_ENABLED error when you move the get_char_bit into the timer timeout handler?
    If not, what error message is printed when you move it into the timeout handler, and attempt to start the program execution?

    Best regards,
    Karl

  • Hi Karl,

    After minutely observing the problem occuring i exactly know where the problem is. Let me first describe how i get the sensor value first.

    Interfaced sensor with TWI and the reading the register of TWI. I read from a register using the function below to get extracted bit.

    THE FUNCTION BELOW READS VALUE FROM A PARTICULAR REGISTER OF SENSOR.

    extern uint8_t register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
    {
     ret_code_t err_code;
     //Set the flag to false to show the receiving is not yet completed
     m_xfer_done = false;

     // Send the Register address where we want to read the data from
     err_code = nrf_drv_twi_tx(&m_twi, REG_ADDR, &register_address, 1, true);


       if (NRF_SUCCESS == err_code)
       {
       NRF_LOG_INFO("Success in tx_read");
       NRF_LOG_FLUSH();

        }

         m_xfer_done = false;

        err_code = nrf_drv_twi_rx(&m_twi, BQ21061_ADDR, destination, number_of_bytes);

       if (NRF_SUCCESS == err_code)
       {
       NRF_LOG_INFO("Success in rx");
       NRF_LOG_FLUSH();
        return destination;

         }


    }

    THIS FUNCTION EXTRACTS THE REQUIRED CHARGING BIT:

    extern ret_code_t charging_check(uint8_t*charge_bit)
    {
      uint8_t receive_buffer;
       uint8_t charging_bit;
      uint8_t charging_done_bit;
      register_read(REG_ADDR, &receive_buffer, 1);
      NRF_LOG_INFO("receive_buffer is %x", receive_buffer);
      NRF_LOG_FLUSH();
       if ((receive_buffer==65) || (receive_buffer==1)) 
      { 
       NRF_LOG_INFO("Charging");
       NRF_LOG_FLUSH();
       charging = 1;
       charging_done = 90;
       NRF_LOG_INFO("Battery is charging, charging = %d", charging);
        NRF_LOG_FLUSH();
      }
      else if (receive_buffer==33) //33 is decimal equivalent of 21 in hex
      {
       NRF_LOG_INFO("Charged");
       NRF_LOG_FLUSH();
        charging_done = 91;
       NRF_LOG_INFO("Charging completed %d", charging_done);
        NRF_LOG_FLUSH();
        }
      *charge_bit= charging_done;
    }

    void ble_modules_init(void)
    {

    leds_init();
    timers_init();

    // buttons_init();
    power_management_init();
    ble_stack_init();
    pwm_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    NRF_LOG_INFO("Blinky example started.");
    application_timers_start();
    advertising_start();
    // OneWireSlave_init();

    // Enter main loop.

    }

    I call ble_modules_init(void) in main after twi _nit()  (TWO WIRE INTERFACE FOR SENSOR)

    The message Blinky example started prints and after that

    success in tx_read function gets printed which means that the register read function doesnt get executed completely after called in timer_timeout_handler as it doesnt recieve anything. Could you suggest how do i go about this. Thanks for all the help .

    Kind Regards,

    PaSi

  • Hello PaSi,

    Thank you for your patience with this.

    PaSi said:
    THE FUNCTION BELOW READS VALUE FROM A PARTICULAR REGISTER OF SENSOR.
    Karl Ylvisaker said:
    Please use the "Insert->Code" option when sharing code here on DevZone.

    Please use the "Insert->Code" option when sharing code here on DevZone.

    PaSi said:
     // Send the Register address where we want to read the data from
     err_code = nrf_drv_twi_tx(&m_twi, REG_ADDR, &register_address, 1, true);


       if (NRF_SUCCESS == err_code)

    You are still not checking the returned error codes, like I asked in my initial reply.
    Please make sure that all returned error codes are passed to an APP_ERROR_CHECK.
    If you do not do this, you will have no way of knowing whether a function has failed and whether you need to do something about it or if your program can proceed as usual.
    Please do this, and confirm to me when it is done.

    PaSi said:

    The message Blinky example started prints and after that

    success in tx_read function gets printed which means that the register read function doesnt get executed completely after called in timer_timeout_handler as it doesnt recieve anything.

    What exactly does the log say? It is good that you include an explanation, but it is also beneficial if you can share the loggers output.

    PaSi said:
    Could you suggest how do i go about this.

    I suspect that the function has returned an error code. Please make sure that all returned error codes are passed to an APP_ERROR_CHECK.
    If you have defined DEBUG in your preprocessor defines, like I asked in my initial reply, you will have a detailed error message printed to your log whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    This is not relevant to the current issue, but I notice that your ble_modules_init seems to contain a "start execution" and "main loop" section. Is this intentional, or is the formatting of your code incorrect here?

    Looking forward to resolve this issue together.

    Best regards,
    Karl

Reply
  • Hello PaSi,

    Thank you for your patience with this.

    PaSi said:
    THE FUNCTION BELOW READS VALUE FROM A PARTICULAR REGISTER OF SENSOR.
    Karl Ylvisaker said:
    Please use the "Insert->Code" option when sharing code here on DevZone.

    Please use the "Insert->Code" option when sharing code here on DevZone.

    PaSi said:
     // Send the Register address where we want to read the data from
     err_code = nrf_drv_twi_tx(&m_twi, REG_ADDR, &register_address, 1, true);


       if (NRF_SUCCESS == err_code)

    You are still not checking the returned error codes, like I asked in my initial reply.
    Please make sure that all returned error codes are passed to an APP_ERROR_CHECK.
    If you do not do this, you will have no way of knowing whether a function has failed and whether you need to do something about it or if your program can proceed as usual.
    Please do this, and confirm to me when it is done.

    PaSi said:

    The message Blinky example started prints and after that

    success in tx_read function gets printed which means that the register read function doesnt get executed completely after called in timer_timeout_handler as it doesnt recieve anything.

    What exactly does the log say? It is good that you include an explanation, but it is also beneficial if you can share the loggers output.

    PaSi said:
    Could you suggest how do i go about this.

    I suspect that the function has returned an error code. Please make sure that all returned error codes are passed to an APP_ERROR_CHECK.
    If you have defined DEBUG in your preprocessor defines, like I asked in my initial reply, you will have a detailed error message printed to your log whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    This is not relevant to the current issue, but I notice that your ble_modules_init seems to contain a "start execution" and "main loop" section. Is this intentional, or is the formatting of your code incorrect here?

    Looking forward to resolve this issue together.

    Best regards,
    Karl

Children
No Data
Related