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

How to send sensor data using AT commands to a device using Uart in segger embedded studio

Hi, I am new to NRF and segger embedded studio.

So I want to send temperature sensor data connected with nrf52840 DK to the Murata1_SC  device using UART.

The data will be sent using AT Commands.

So how to send AT Commands in Segger embedded studio.

Thank you for the help in advance.

Parents
  • 
    #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"
    #include "nrf_uart.h"
    
    #define UART_TX_BUFF_SIZE 128
    #define UART_RX_BUFF_SIZE 128
    
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
    
    void uart_err_handle(app_uart_evt_type_t * p)
    {
      
    
    }
    
    int main(void)
    {
      uint32_t err_code;
    
      bsp_board_init(BSP_INIT_LEDS);
      
      const app_uart_comm_params_t com_params = 
       {
        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        UART_HWFC,
        false,
        NRF_UART_BAUDRATE_115200
        };
    
      APP_UART_FIFO_INIT(&com_params, UART_RX_BUFF_SIZE, UART_TX_BUFF_SIZE, uart_err_handle, APP_IRQ_PRIORITY_LOWEST, err_code);
    
      APP_ERROR_CHECK(err_code);
      
      printf("Hello PC from Nordic Device!!!\r\n");
      
      while(true)
      {
        uint8_t cr;
    
        while(app_uart_get(&cr) != NRF_SUCCESS);//wait here for the character from pc
        if(cr == 'o')
        {
          bsp_board_leds_on();
          printf("Leds are ON\r\n");
        }
        if(cr == 'f')
        {
          bsp_board_leds_off();
          printf("Leds are OFF\r\n");
        }
      }
    }
    
    
    
    

  • I have seen this code

    uart_putstring((uint8_t*)"AT\r");
    nrf_delay_ms(2000);
    uart_putstring((uint8_t*)"AT+CMGF=1\r");
           nrf_delay_ms(2000);
    uart_putstring((uint8_t*)"AT+CMGS=\"8919242248\"\r");
       nrf_delay_ms(2000);
       uart_putstring((uint8_t*)"test\r");
              nrf_delay_ms(2000);
              app_uart_put((char)0x1A);   ///used (uint8_t) 26 also but no response..
                     nrf_delay_ms(1000);

    in this link, where someone is saying to wait for the response, so how can i do that.

  • I have sent AT every commands to the module using TeraTerm software, so some commands took almost 3 to 4 seconds to respond. 

    So 4 seconds timout will be fine for this?

  • This should really be documented by the module manufacturer - you need to contact them for details.

    Remember that you have to consider worst case conditions.

    But if your application is OK to wait a whole 4s for a command which should complete "immediately" - then that's fine.

    It's your application - only you can answer that.

  • Yes, I am just testing the timeout with different commands.

    Could me please tell me, how can I make other pins as UART

    Because P0.05 to P0.08 are Hardware UART pins

      how can i make other GPIO pins work as UART like software serial in Arduino 

  • See the Product Specification: the nRF52 have flexible pin mapping - with just a couple of restrictions, the UART (and other peripherals) can be routed to any pins.

    The nRF52840 has two hardware UARTs 

  • Is this correct

    #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"
    #include "nrf_uart.h"
    #include <string.h>
    
    #define RX_PIN_NUMBER 27 //8
    #define TX_PIN_NUMBER 26 //6
    #define CTS_PIN_NUMBER 12 //7
    #define RTS_PIN_NUMBER 11 //5
    
    
    #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */
    
    /* When UART is used for communication with the host do not use flow control.*/
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
    
    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);
        }
    }
    
    void uart_putstring(const uint8_t *str) 
    {
           uint32_t err_code;
    
           while( str ) 
           {
                err_code = app_uart_put(( uint8_t*) str++ );
    
                APP_ERROR_CHECK(err_code);
           }
    }
     
    
    
    /***
    
    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;
    }
    
    ***/
    
    char* uart_getstring(char* rx_data) 
    {
           uint32_t err_code;
           err_code = app_uart_get(rx_data);
           APP_ERROR_CHECK(err_code);
           return rx_data;
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        uint32_t err_code;
    
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              UART_HWFC,
              false,
    
              NRF_UART_BAUDRATE_115200
    
          };
    
        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);
    
     //Commands to send data to murata_1sc
    
        uart_putstring((const char*)"AT\r\n"); //variaible iterate - array iterate
        nrf_delay_ms(100);
         char* output;
        output = uart_getstring(output);
        printf(output);
        
        uart_putstring((const char*)"AT%AWSIOTCMD=\"PUBLISH\",\"tracker/location\",\"{\"message\":\"Hello from Murata 1SC\"}\"\r\n"); //variaible iterate - array iterate
        nrf_delay_ms(100);
         char* output;
        output = uart_getstring(output);
        printf(output);
     
    }
    
    
    
    
    
    
    
    
    /***
     
     //array of commands declare
     char arrr[12][150]  = { " AT\r\n ",
                             " ",
                             " ",
                             " AT%CERTCMD=\"DIR\"%CERTCMD:murata_1sc_cert.pem,cert.pem,privatekey.pem,murata_private.key\r\n ",
                             " AT%CERTCFG=\"ADD\",1,,\"~\",\"murata_1sc_cert.pem\",\"murata_1sc_private.key\"\r\n ",
                             " AT%AWSIOTCFG=\"PROTOCOL\",1200,0\r\n ",
                             " AT%AWSIOTEV=\"ALL\",1\r\n ",
                             " AT%AWSIOTCMD=\"CONNECT\"\r\n ",
                             " AT%AWSIOTCMD=\"PUBLISH\",\"tracker/location\",\"{\"message\":\"Hello from Murata 1SC\"}\"\r\n ",
                             " AT%AWSIOTCMD=\"SUBSCRIBE\",\"tracker/location\"\r\n ",
                             " AT%AWSIOTCMD=\"SUBSCRIBE\",\"$aws/things/murata_1sc/shadow/update\"\r\n ",
                             " AT%AWSIOTCMD=\"PUBLISH\",\"$aws/things/murata_1sc/shadow/update\",\" { \"state\": {\"desired\": { \"color\": \"yellow\" } } }\"\r\n "
                           };
     //char outttt[12][100];    
     //loopstart
    
     int i = 0;
     for(i=0;i<13;i++)
     {
        uart_putstring((const char*)arrr[i]); //variaible iterate - array iterate
        nrf_delay_ms(100);
        char* output;
        while( true )
            {
              // wait for timeout or reply complete
              output = uart_getstring(output);
              if (output == "ERROR")
              {
               printf("ERROR");
              }
    }
    
    //print
    //loop end
    }
    }
    
    ***/
    
    
    
    

    From where to check the GPIO pin in nrf

    like

    P1.02

    P1.03

    P1.04

Reply
  • Is this correct

    #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"
    #include "nrf_uart.h"
    #include <string.h>
    
    #define RX_PIN_NUMBER 27 //8
    #define TX_PIN_NUMBER 26 //6
    #define CTS_PIN_NUMBER 12 //7
    #define RTS_PIN_NUMBER 11 //5
    
    
    #define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
    #define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */
    
    /* When UART is used for communication with the host do not use flow control.*/
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
    
    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);
        }
    }
    
    void uart_putstring(const uint8_t *str) 
    {
           uint32_t err_code;
    
           while( str ) 
           {
                err_code = app_uart_put(( uint8_t*) str++ );
    
                APP_ERROR_CHECK(err_code);
           }
    }
     
    
    
    /***
    
    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;
    }
    
    ***/
    
    char* uart_getstring(char* rx_data) 
    {
           uint32_t err_code;
           err_code = app_uart_get(rx_data);
           APP_ERROR_CHECK(err_code);
           return rx_data;
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        uint32_t err_code;
    
        const app_uart_comm_params_t comm_params =
          {
              RX_PIN_NUMBER,
              TX_PIN_NUMBER,
              RTS_PIN_NUMBER,
              CTS_PIN_NUMBER,
              UART_HWFC,
              false,
    
              NRF_UART_BAUDRATE_115200
    
          };
    
        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);
    
     //Commands to send data to murata_1sc
    
        uart_putstring((const char*)"AT\r\n"); //variaible iterate - array iterate
        nrf_delay_ms(100);
         char* output;
        output = uart_getstring(output);
        printf(output);
        
        uart_putstring((const char*)"AT%AWSIOTCMD=\"PUBLISH\",\"tracker/location\",\"{\"message\":\"Hello from Murata 1SC\"}\"\r\n"); //variaible iterate - array iterate
        nrf_delay_ms(100);
         char* output;
        output = uart_getstring(output);
        printf(output);
     
    }
    
    
    
    
    
    
    
    
    /***
     
     //array of commands declare
     char arrr[12][150]  = { " AT\r\n ",
                             " ",
                             " ",
                             " AT%CERTCMD=\"DIR\"%CERTCMD:murata_1sc_cert.pem,cert.pem,privatekey.pem,murata_private.key\r\n ",
                             " AT%CERTCFG=\"ADD\",1,,\"~\",\"murata_1sc_cert.pem\",\"murata_1sc_private.key\"\r\n ",
                             " AT%AWSIOTCFG=\"PROTOCOL\",1200,0\r\n ",
                             " AT%AWSIOTEV=\"ALL\",1\r\n ",
                             " AT%AWSIOTCMD=\"CONNECT\"\r\n ",
                             " AT%AWSIOTCMD=\"PUBLISH\",\"tracker/location\",\"{\"message\":\"Hello from Murata 1SC\"}\"\r\n ",
                             " AT%AWSIOTCMD=\"SUBSCRIBE\",\"tracker/location\"\r\n ",
                             " AT%AWSIOTCMD=\"SUBSCRIBE\",\"$aws/things/murata_1sc/shadow/update\"\r\n ",
                             " AT%AWSIOTCMD=\"PUBLISH\",\"$aws/things/murata_1sc/shadow/update\",\" { \"state\": {\"desired\": { \"color\": \"yellow\" } } }\"\r\n "
                           };
     //char outttt[12][100];    
     //loopstart
    
     int i = 0;
     for(i=0;i<13;i++)
     {
        uart_putstring((const char*)arrr[i]); //variaible iterate - array iterate
        nrf_delay_ms(100);
        char* output;
        while( true )
            {
              // wait for timeout or reply complete
              output = uart_getstring(output);
              if (output == "ERROR")
              {
               printf("ERROR");
              }
    }
    
    //print
    //loop end
    }
    }
    
    ***/
    
    
    
    

    From where to check the GPIO pin in nrf

    like

    P1.02

    P1.03

    P1.04

Children
  • I am using hardware pins,but i am able to make communication with device.

  • Hi by using the above code, I am not able to send anything.

    But I tried the following code to send data to Arduino and I am able to send the data.

    
    #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"
    #include "nrf_uart.h"
    
    //#define RX_PIN_NUMBER 27 //8
    //#define TX_PIN_NUMBER 26 //6
    //#define CTS_PIN_NUMBER 12 //7
    //#define RTS_PIN_NUMBER 11 //5
    
    
    #define UART_TX_BUFF_SIZE 256
    #define UART_RX_BUFF_SIZE 256
    
    #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
    
    void uart_err_handle(app_uart_evt_type_t * p)
    {
      
    
    }
    
    int main(void)
    {
      uint32_t err_code;
    
      bsp_board_init(BSP_INIT_LEDS);
      
      const app_uart_comm_params_t com_params = 
       {
        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        UART_HWFC,
        false,
        NRF_UART_BAUDRATE_115200
        };
    
      APP_UART_FIFO_INIT(&com_params, UART_RX_BUFF_SIZE, UART_TX_BUFF_SIZE, uart_err_handle, APP_IRQ_PRIORITY_LOWEST, err_code);
    
      APP_ERROR_CHECK(err_code);
      
      printf("Hello PC from Nordic\r\n");
      
      while(true)
      {
        uint8_t cr;
    
        while(app_uart_get(&cr) != NRF_SUCCESS);//wait here for the character from pc
        if(cr == 'OK')
        {
          bsp_board_leds_on();
          printf("Leds are ON\r\n");
        }
        if(cr == 'ERROR')
        {
          bsp_board_leds_off();
          printf("Leds are OFF\r\n");
        }
      }
    }
    
    
    
    
     

    Also, I am confused that by changing these pins

    #define RX_PIN_NUMBER 27 //8
    #define TX_PIN_NUMBER 26 //6
    #define CTS_PIN_NUMBER 12 //7
    #define RTS_PIN_NUMBER 11 //5
    

    Could I able to change the hardware UART to Software UART

    Because Hardware UART is used by Debugger 

  • Hi,

    What's the exact issue you are having ? 
    Can you develop your app based on the uart example in the SDK or the example that you found working above ? 

    I don't really understand what you meant by Software UART ? 

    What did you mean by "hardware UART is used by debugger" ? 
    Calling this: 

    #define RX_PIN_NUMBER 27 //8
    #define TX_PIN_NUMBER 26 //6
    #define CTS_PIN_NUMBER 12 //7
    #define RTS_PIN_NUMBER 11 //5

    Will degine the RX pin to pin P0.27 on the chip. TX pin to P0.26 on the chip, and so on. 

  • don't really understand what you meant by Software UART

    I presume he means bit-banging it in software?

    Doesn't seem much point in doing that when the nRF52840 has 2 "real" (ie, hardware) UARTS?

    RX pin to pin P0.27 on the chip. TX pin to P0.26

    How does it work for P1.xx pins ?

  • basically, I want to send AT commands to an LTE Module using nrf52840, so I used the UART example in the example folder of nrf sdk 17.0.2.

    But I have read its response back to see what I get.

    Si I have read m Q&A on devzone.nordicsemi.com.

    Some are saying P0.05 to P0.08 is used by J-Link.

    So I am confused, so which pins should I use to communicate and how to change the pins in the code.

    Actually I am new to nrf and segger so i have some basic questions also

    And

    printf("AT\n")

    is used to send data through UART.

    But how can I check the data in the output console, means which command is used to print data in the output console

    is it

    NRF_LOG_INFO("AT/n");

    or

    app_uart_put()

    Please help

Related