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

I want to read sensor data from TX pin of sensor connected to nrf51822 RX pin

Hello, 

I am trying to read data from a sensor through sensor TX pin connected to RX pin of nrf51822. 
Nrf51822 recieves this data through RX pin and sends it via bluetooth to Android App.

I have this simple code i wrote in arduino, but don't know how to implement it in nrf51822.

   bittpos = 0;
   while(Serial.available())
         {          
            if(bittpos < 4){
                mbufer[bittpos++] = Serial.read();
            }else break;
         }
    Serial.flush(); 
    
     
    mdata = (mbufer[1] * 256) + mbufer[2];
    
    // Print out sensor data
    Serial.println(mdata); 

I've gone through the UART sample code. But i don't know where to start from in implementing the above code.

Parents
  • Hi.

    I have this simple code i wrote in arduino, but don't know how to implement it in nrf51822.

     I'm a bit confused by this unfortunately.

    Are you looking for an equivalent code snippet for SDK 12.3 for the code snippet you have posted? Or are you having trouble running the code snippet you posted on the nRF51822 DK?

    Best regards,

    Andreas

  • I'm trying to read values from a sensor that uses UART to send readings. I've connected the sensor TX pin to RX of Nrf51822, but i cant figure out how to get/read the data from the sensor. 

    I succeeded on arduino with the code snippet above. I'm having difficulty in achieving this on the nrf51822

  • Have you looked at the UART examples in the SDK ?

  • I did try the UART example but I can't find where RX values are captured.
    I'm just trying to read TX from sensor and send readings to android app.

  • Hi.

    Did you look at the uart_event_handle() function in the example found in the examples\ble_peripheral\ble_app_uart folder?

    /**@brief   Function for handling app_uart events.
     *
     * @details This function will receive a single character from the app_uart module and append it to
     *          a string. The string will be be sent over BLE when the last character received was a
     *          'new line' i.e '\r\n' (hex 0x0D) or if the string has reached a length of
     *          @ref NUS_MAX_DATA_LENGTH.
     */
    /**@snippet [Handling the data received over UART] */
    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;
        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 - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
                {
                    err_code = ble_nus_string_send(&m_nus, data_array, index);
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
    
                    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;
        }
    }

    The event handler uses the function app_uart_get() to get a byte from the UART.

    /**@brief Function for getting a byte from the UART.
     *
     * @details This function will get the next byte from the RX buffer. If the RX buffer is empty
     *          an error code will be returned and the app_uart module will generate an event upon
     *          reception of the first byte which is added to the RX buffer.
     *
     * @param[out] p_byte    Pointer to an address where next byte received on the UART will be copied.
     *
     * @retval NRF_SUCCESS          If a byte has been received and pushed to the pointer provided.
     * @retval NRF_ERROR_NOT_FOUND  If no byte is available in the RX buffer of the app_uart module.
     */
    uint32_t app_uart_get(uint8_t * p_byte);

    And then it sends the data over BLE if the last character for the string is new line, or if the string has reached a length of BLE_NUS_MAX_DATA_LEN, by calling the function ble_nus_string_send().

    /**@brief Function for sending a string to the peer.
     *
     * @details This function sends the input string as an RX characteristic notification to the
     *          peer.
     *
     * @param[in] p_nus       Pointer to the Nordic UART Service structure.
     * @param[in] p_string    String to be sent.
     * @param[in] length      Length of the string.
     *
     * @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
     */
    uint32_t ble_nus_string_send(ble_nus_t * p_nus, uint8_t * p_string, uint16_t length);
    

    I hope this helps you!

    Best regards,

    Andreas

Reply
  • Hi.

    Did you look at the uart_event_handle() function in the example found in the examples\ble_peripheral\ble_app_uart folder?

    /**@brief   Function for handling app_uart events.
     *
     * @details This function will receive a single character from the app_uart module and append it to
     *          a string. The string will be be sent over BLE when the last character received was a
     *          'new line' i.e '\r\n' (hex 0x0D) or if the string has reached a length of
     *          @ref NUS_MAX_DATA_LENGTH.
     */
    /**@snippet [Handling the data received over UART] */
    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;
        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 - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
                {
                    err_code = ble_nus_string_send(&m_nus, data_array, index);
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
    
                    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;
        }
    }

    The event handler uses the function app_uart_get() to get a byte from the UART.

    /**@brief Function for getting a byte from the UART.
     *
     * @details This function will get the next byte from the RX buffer. If the RX buffer is empty
     *          an error code will be returned and the app_uart module will generate an event upon
     *          reception of the first byte which is added to the RX buffer.
     *
     * @param[out] p_byte    Pointer to an address where next byte received on the UART will be copied.
     *
     * @retval NRF_SUCCESS          If a byte has been received and pushed to the pointer provided.
     * @retval NRF_ERROR_NOT_FOUND  If no byte is available in the RX buffer of the app_uart module.
     */
    uint32_t app_uart_get(uint8_t * p_byte);

    And then it sends the data over BLE if the last character for the string is new line, or if the string has reached a length of BLE_NUS_MAX_DATA_LEN, by calling the function ble_nus_string_send().

    /**@brief Function for sending a string to the peer.
     *
     * @details This function sends the input string as an RX characteristic notification to the
     *          peer.
     *
     * @param[in] p_nus       Pointer to the Nordic UART Service structure.
     * @param[in] p_string    String to be sent.
     * @param[in] length      Length of the string.
     *
     * @retval NRF_SUCCESS If the string was sent successfully. Otherwise, an error code is returned.
     */
    uint32_t ble_nus_string_send(ble_nus_t * p_nus, uint8_t * p_string, uint16_t length);
    

    I hope this helps you!

    Best regards,

    Andreas

Children
  • Hello,

    I tested tried your explanation and i've been able to read data from UART, but the incoming data is not showing.

    Will be grateful if you could help me view the incoming data as float or long. 

    This is the code used to retrieve data.

    void uart_callback(uint8_t * p_data, uint8_t length)
    {
      // TODO: Process 'data_array' of size 'length'.
    
        SEGGER_RTT_printf(0, "Data :\n", p_data);
     
    }
    
    
    
    

    Event handler

    void uart_error_handle(app_uart_evt_t * p_event)
    { 
    	static uint8_t data_array[4];
            static uint8_t index = 0;
            uint32_t       err_code;
            
            if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
            {
                APP_ERROR_HANDLER(p_event->data.error_communication);
            }
            else if (p_event->evt_type == APP_UART_FIFO_ERROR)
            {
                APP_ERROR_HANDLER(p_event->data.error_code);
            }
            if (p_event->evt_type == APP_UART_DATA_READY)
            {  
               err_code = app_uart_get(&data_array[index]);
               index++;
               /* TODO: Substitute '\n' in the statement below with the Modbus escape character, if 
                   applicable */
               if ((data_array[index - 1] == '\n') || (index >= (4)))
               {
                    uart_callback(&data_array[0], index);
    
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
    
                    index = 0;
                }
    
           }
    }

  • I've found the solution. Got the values output with this.

    void uart_callback(uint8_t * p_data, uint8_t length)
    {
            // TODO: Process 'data_array' of size 'length'. 
    	  for (int i = 0; i < length; i++) { 
    		SEGGER_RTT_printf(0, "%d\n", p_data[i]);  
      }
    }

Related