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

Trying to setup a uart connection between nodemcu and nrf52832

Hi guys I was trying send data from nrf52832 to Nodemcu and from Nodemcu to nrf52832 via UART. I used Default uart pins on nrf52832 and used the hardware serial on Nodemcu but I am not able to establish an connection.

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif


//#define ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */

#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

void uart_error_handle(app_uart_evt_t * p_event)
{
    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);
    }
}


#ifdef ENABLE_LOOPBACK_TEST
/* Use flow control in loopback test. */
#define UART_HWFC APP_UART_FLOW_CONTROL_ENABLED

/** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
 */
static void show_error(void)
{

    bsp_board_leds_on();
    while (true)
    {
        // Do nothing.
    }
}


/** @brief Function for testing UART loop back.
 *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
 *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
 */
static void uart_loopback_test()
{
    uint8_t * tx_data = (uint8_t *)("\r\nLOOPBACK_TEST\r\n");
    uint8_t   rx_data;

    // Start sending one byte and see if you get the same
    for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
    {
        uint32_t err_code;
        while (app_uart_put(tx_data[i]) != NRF_SUCCESS);

        nrf_delay_ms(10);
        err_code = app_uart_get(&rx_data);

        if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
        {
            show_error();
        }
    }
    return;
}
#else
/* When UART is used for communication with the host do not use flow control.*/
#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
#endif


/**
 * @brief Function for main application entry.
 */
int main(void)

{
    uint32_t err_code;
    nrf_gpio_cfg_output(16);//green

    bsp_board_init(BSP_INIT_LEDS);

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);

#ifndef ENABLE_LOOPBACK_TEST
    printf("\r\nUART example started.\r\n");


  while (true)
    {
       //trying to get data       
               
        uint8_t kr;
        while (app_uart_get(&kr) != NRF_SUCCESS);
        while (app_uart_put(kr) != NRF_SUCCESS);


        if (kr == 'k' || kr == 'u')
        {
          nrf_gpio_pin_write(16,1);
          nrf_delay_ms(1000);
          nrf_gpio_pin_write(16,0);
        }
               
    }
 }
    
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
#endif




/** @} */

  • Hello,

    Sorry for the late reply, there was just a public holiday here in Norway.

    Now, to your issue - you are unable to establish UART communication between your nRF52832 and your NodeMCU.

    First, could you tell me if you are working on a custom board with the nRF52832, or are you using a development kit or dongle? Could you also describe your hardware connection between the two devices? Which pins have you connected where?
    It would also be beneficial if you could elaborate more on what you are seeing: You say that you are not able to establish a connection, what does this mean; Is there nothing happening on the nRF TX pin while the application is running? Does the NodeMCU receive anything at all?
    A great way to eliminate some of these possibilities is to test the UART example according to the documentation, to verify whether the communication is happening at all. This way, you can isolate the problem to either the nRF or the NodeMCU.

    Your code looks alright, as it essentially is the UART peripheral example, so I would not expect that this is where it goes wrong. 

    Looking forward to solving this issue together!

    Best regards,
    Karl

Related