Intermediate
30 min
0

Achieve trouble-free NB-IoT connectivity with BC66-NA and STM32F373VC

Stay connected and responsive in any environment

NB IoT 2 Click with UNI-DS v8

Published Sep 13, 2023

Click board™

NB IoT 2 Click

Development board

UNI-DS v8

Compiler

NECTO Studio

MCU

STM32F373VC

Experience IoT in a new light with NB-IoT technology, redefining what's achievable and setting the stage for boundless innovation

A

A

Hardware Overview

How does it work?

NB IoT 2 Click is based on the BC66-NA, a high-performance NB-IoT module with extremely low power consumption from Quectel Wireless Solutions. It is designed to communicate with the infrastructures of mobile network operators through NB-IoT radio protocols (3GPP Rel.13 and 3GPP Rel.14). It supports a broad range of frequency bands such as B1/B2/B3/B4/B5/B8/B12/ B13/B17/B18/B19/B20/B25/B28/B66/B71/B85. It also provides several interfaces, UART and SPI, and protocol stacks such as UDP/TCP, MQTT, LwM2M, and others. These protocols allowing data and SMS transmission using NB technology make this module the perfect choice for building IoT applications such as smart gas and water meters, even without needing an external MCU unit. To turn ON this Click board™, the user has to press the onboard push-button labeled PWRKEY down for a period longer, at least 500ms, which represents the Ignition (Power-On) button. The PWRKEY button cannot be pulled down constantly; otherwise, the module cannot enter

power-save mode. This feature is shown by the yellow diode labeled as CONNECT to indicate the device's Operational Status. NB IoT 2 Click communicates with MCU using the UART interface as its default communication protocol with the option for the users to use another interface, such as SPI if they want to configure the module and write the library by themselves. It supports automatic baud rate detection, operates at 115200 bps by default configuration, and is used for data transmission and exchanging AT commands with the host MCU. In addition to these features, the BC66-NA uses two GPIO pins connected to the mikroBUS™ socket. The WUP pin routed on the INT pin of the mikroBUS™ represents the external interrupt pin used for waking up the device from a power-saving mode, while the RST pin on the mikroBUS™ socket can perform Hardware Reset function by putting this pin in a logic low state. This Click board™ also has a micro USB connector, allowing the module to be powered and configured by a personal computer.

Also, it has an additional header labeled ADC IN because the BC66-NA provides a 10-bit ADC input channel to read the voltage value.  The interface is available in Active mode and has to be woken up first to ensure availability in Sleep mode. NB IoT 2 Click possesses the SMA antenna connector with an impedance of 50Ω. This Click board™ can be used to connect the appropriate antenna that MIKROE offers. Besides the SMA connector, it has a SIM card slot that provides multiple connections and interface options. This Click board™ can be operated only with a 3.3V logic voltage level and comes with a proper logic voltage level conversion performed by the appropriate voltage level translator TXB0106. The board must complete the proper logic voltage level conversion before use with MCUs with different logic levels. However, the Click board™ comes equipped with a library containing functions and an example code that can be used as a reference for further development.

NB IoT 2 Click hardware overview image

Features overview

Development board

UNI-DS v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports a wide range of microcontrollers, such as different STM32, Kinetis, TIVA, CEC, MSP, PIC, dsPIC, PIC32, and AVR MCUs regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer over WiFi. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. Thanks to innovative manufacturing technology, UNI-DS v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the UNI-DS v8 development board contains the components necessary for the most efficient operation of the same board. An advanced integrated CODEGRIP programmer/debugger module offers many valuable programming/debugging options, including support for JTAG, SWD, and SWO Trace (Single Wire Output)), and seamless integration with the Mikroe software environment. Besides, it also includes a clean and regulated power supply module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector. Communication options such as USB-UART, USB

HOST/DEVICE, CAN (on the MCU card, if supported), and Ethernet is also included. In addition, it also has the well-established mikroBUS™ standard, a standardized socket for the MCU card (SiBRAIN standard), and two display options for the TFT board line of products and character-based LCD. UNI-DS v8 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

UNI-DS v8 horizontal image

Microcontroller Overview

MCU Card / MCU

default

Type

8th Generation

Architecture

ARM Cortex-M4

MCU Memory (KB)

256

Silicon Vendor

STMicroelectronics

Pin count

100

RAM (Bytes)

49152

You complete me!

Accessories

LTE Flat Rotation Antenna is a versatile choice for boosting the performance of 3G/4G LTE devices. With a wide frequency range of 700-2700MHz, it ensures optimal connectivity on major cellular bands worldwide. This flat antenna features an SMA male connector, making it easy to attach directly to your device or SMA module connector. One of its standout features is its adjustable angle, which can be set in 45⁰ increments (0⁰/45⁰/90⁰), allowing you to fine-tune the antenna's orientation for maximum signal reception. With an impedance of 50Ω and a VSW Ratio of <2.0:1, this antenna ensures a reliable and efficient connection. Its 5dB gain, vertical polarization, and omnidirectional radiation pattern enhance signal strength, making it suitable for various applications. Measuring 196mm in length and 38mm in width, this antenna offers a compact yet effective solution for improving your connectivity. With a maximum input power of 50W, it can handle the demands of various devices.

NB IoT 2 Click accessories image

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Reset
PB10
RST
SPI Chip Select
PD15
CS
SPI Clock
PA5
SCK
SPI Data OUT
PA6
MISO
SPI Data IN
PA7
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Wake-Up Interrupt
PA8
INT
UART TX
PA2
TX
UART RX
PA3
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

NB IoT 2 Click Schematic schematic

Step by step

Project assembly

Fusion for PIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the UNI-DS v8 as your development board.

Fusion for PIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
SiBRAIN for PIC32MZ1024EFK144 front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
v8 SiBRAIN Access MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

After pressing the "FLASH" button on the left-side panel, it is necessary to open the UART terminal to display the achieved results. By clicking on the Tools icon in the right-hand panel, multiple different functions are displayed, among which is the UART Terminal. Click on the offered "UART Terminal" icon.

UART Application Output Step 1

Once the UART terminal is opened, the window takes on a new form. At the top of the tab are two buttons, one for adjusting the parameters of the UART terminal and the other for connecting the UART terminal. The tab's lower part is reserved for displaying the achieved results. Before connecting, the terminal has a Disconnected status, indicating that the terminal is not yet active. Before connecting, it is necessary to check the set parameters of the UART terminal. Click on the "OPTIONS" button.

UART Application Output Step 2

In the newly opened UART Terminal Options field, we check if the terminal settings are correct, such as the set port and the Baud rate of UART communication. If the data is not displayed properly, it is possible that the Baud rate value is not set correctly and needs to be adjusted to 115200. If all the parameters are set correctly, click on "CONFIGURE".

UART Application Output Step 3

The next step is to click on the "CONNECT" button, after which the terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART Application Output Step 4

Software Support

Library Description

This library contains API for NB IoT 2 Click driver.

Key functions:

  • nbiot2_set_rst_pin_state - This function sets RST pin state

  • nbiot2_hw_reset - NB IoT 2 hardware reset function

  • nbiot2_send_cmd - NB IoT 2 send command function

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * @file main.c
 * @brief NB IoT 2 Click Example.
 *
 * # Description
 * This example reads and processes data from NB IoT 2 clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver, wake-up module and sets default configuration 
 * for connecting device to network.
 *
 * ## Application Task
 * This application is split into two examples:
 * - TCP/UDP example - connecting to echo server and writing and reading data.
 * - SMS example - Sending SMS message to desired number using TEXT or PDU mode. 
 *
 * ## Additional Function
 * - static void nbiot2_clear_app_buf ( void )
 * - static err_t nbiot2_process ( void )
 * - static void nbiot2_config_device_for_example ( void )
 * - static void nbiot2_check_connection ( void )
 * - static void nbiot2_log_response ( void )
 * - static void nbiot2_error_check( err_t error_flag );
 * - static err_t nbiot2_check_response ( void )
 * - static void nbiot2_config_device_for_network ( void )
 * - static void nbiot2_send_sms ( void )
 * - static void nbiot2_config_device_for_tcp_udp ( void )
 * - static void nbiot2_tcp_udp_example ( void )
 *
 * @note
 * In order for the examples to work, user needs to set the APN and SMSC (SMS PDU mode only) 
 * of entered SIM card as well as the phone number to which he wants to send an SMS.
 * Enter valid values for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * Example: 
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 * 
 * @author Stefan Ilic
 *
 */

#include "board.h"
#include "log.h"
#include "conversions.h"
#include "nbiot2.h"

// Example selection macros
#define NBIOT2_TCP_UDP_EXAMPLE
// #define NBIOT2_SMS_EXAMPLE

// SMS example selection macros
// #define SMS_PDU_MODE
#define SMS_TEXT_MODE

// SMS example parameters
#define PHONE_NUMBER_TO_MESSAGE             ""              // Set Phone number to message
#define SIM_SMSC                            ""              // Set SIM SMSC

// TCP/UDP example parameters
#define REMOTE_IP                           "77.46.162.162" // TCP/UDP echo server IP address
#define REMOTE_PORT                         "51111"         // TCP/UDP echo server port

// SIM APN config
#define SIM_APN                             ""
#define MESSAGE_CONTENT                     "NB IoT 2 Click Example"   // Message content

#define PROCESS_BUFFER_SIZE                 200

#define CONFIGURATION_FOR_EXAMPLE           0
#define EXAMPLE                             1

static nbiot2_t nbiot2;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static int32_t app_buf_cnt = 0;

static err_t app_error_flag;

static uint8_t app_connection_status = CONFIGURATION_FOR_EXAMPLE;

/**
 * @brief NB IoT 2 clearing application buffer.
 * @details This function clears memory of application buffer and reset its length and counter.
 * @note None.
 */
static void nbiot2_clear_app_buf ( void );

/**
 * @brief NB IoT 2 data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nbiot2_process ( void );

/**
 * @brief Configure device for example.
 * @details Configure device for sending messages or sending messages to TCP/UDP Echo server.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_example ( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_check_connection ( void );

/**
 * @brief Logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void nbiot2_log_response ( void );

/**
 * @brief Check for errors.
 * @details This function checks for different types of 
 * errors and logs them on UART or logs the response if no errors occured.
 * @note None.
 */
static void nbiot2_error_check( err_t error_flag );

/**
 * @brief NB IoT 2 response read function.
 * @details This function reads the response of the sent command and writes it on the USB UART.
 * @return @li @c  0 - Response OK.
 *         @li @c -1 - Response ERROR.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nbiot2_check_response ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable connection to the specified network.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_network ( void );

/**
 * @brief Check for connection to the network.
 * @details Sends commands to check connection to the specified network.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_check_connection_to_network ( void );

/**
 * @brief Send SMS example.
 * @details Sends commands to send SMS messages in TEXT or PDU mode.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_send_sms ( void );

/**
 * @brief Configure device for TCP/UDP example.
 * @details Sends commands to connect with TCP/UDP server.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_tcp_udp ( void );

/**
 * @brief TCP/UDP example.
 * @details Sends message to TCP/UDP server and receives data from it.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_tcp_udp_example ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nbiot2_cfg_t nbiot2_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    nbiot2_cfg_setup( &nbiot2_cfg );
    NBIOT2_MAP_MIKROBUS( nbiot2_cfg, MIKROBUS_1 );
    if ( UART_ERROR == nbiot2_init( &nbiot2, &nbiot2_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_printf( &logger, " - Reseting device... \r\n" );
    nbiot2_hw_reset( &nbiot2 );
    Delay_ms( 4000 );
    app_error_flag = nbiot2_process( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_AT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_ATI );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CFUN, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QSPCHSC, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CPSMS, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_set_apn( &nbiot2, SIM_APN );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );   
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QRST, "1" );
    Delay_ms( 2000 );
    app_error_flag = nbiot2_process( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QBAND, "1,20" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );      
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CEREG, "2" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );   
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CSCON, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QNBIOTEVENT, "1,1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_SM, "LOCK" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QSCLK, "0" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    log_info( &logger, " Application Task " );
    
    app_connection_status = CONFIGURATION_FOR_EXAMPLE;
    
    app_buf_len = 0;
    app_buf_cnt = 0;
}

void application_task ( void ) 
{   
    switch( app_connection_status )
    {
        case CONFIGURATION_FOR_EXAMPLE:
        {
            nbiot2_config_device_for_example( );
            break;
        }
        case EXAMPLE:
        {
            #if defined( NBIOT2_TCP_UDP_EXAMPLE )
                nbiot2_tcp_udp_example( );
            #endif   
            #if defined( NBIOT2_SMS_EXAMPLE )
                nbiot2_send_sms( );
                Delay_ms( 10000 );
            #endif
            break;
        }
        default:
        {
            log_error( &logger, "Application status error!" );
            app_connection_status = CONFIGURATION_FOR_EXAMPLE;
            Delay_ms( 1000 );
            break;
        }
    }
}

void main ( void ) 
{
    application_init( );

    for ( ; ; ) 
    {
        application_task( );
    }
}

static void nbiot2_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t nbiot2_process ( void ) 
{
    int32_t rx_size;
    char rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    rx_size = nbiot2_generic_read( &nbiot2, rx_buf, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE ) 
        {
            nbiot2_clear_app_buf(  );
            return NBIOT2_ERROR;
        } 
        else 
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }
        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] != 0 ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buf[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
        return NBIOT2_OK;
    }
    return NBIOT2_ERROR;
}

static void nbiot2_config_device_for_example ( void )
{
#if defined( NBIOT2_TCP_UDP_EXAMPLE )
    nbiot2_config_device_for_tcp_udp( );
#endif
#if defined( NBIOT2_SMS_EXAMPLE )
    nbiot2_config_device_for_network( );
    for( ; ; )
    {
        nbiot2_check_connection_to_network( );
        if ( CONFIGURATION_FOR_EXAMPLE != app_connection_status )
        {
            break;
        }
    }
#endif 
}

static void nbiot2_check_connection( void )
{
    #define CONNECTED "+CGATT: 1"

    if ( strstr( app_buf, CONNECTED ) != 0 )
    {
        app_connection_status = EXAMPLE;
    }
}

static err_t nbiot2_check_response ( void )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 100000;
    err_t error_flag;
    while ( ( 0 == strstr( app_buf, NBIOT2_RSP_OK ) ) && 
            ( 0 == strstr( app_buf, NBIOT2_RSP_ERROR ) ) )
    {
        error_flag = nbiot2_process( );
        if ( ( NBIOT2_OK != error_flag ) && ( NBIOT2_ERROR != error_flag ) )
        {
            return error_flag;
        }
        if ( timeout_cnt++ > timeout )
        {
            nbiot2_clear_app_buf( );
            return NBIOT2_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    if ( strstr( app_buf, NBIOT2_RSP_OK ) )
    {
        return NBIOT2_OK;
    }
    else if ( strstr( app_buf, NBIOT2_RSP_ERROR ) )
    {
        return NBIOT2_ERROR;
    }
    else
    {
        return NBIOT2_ERROR;
    }
}

static void nbiot2_log_response ( void )
{    
    if ( app_buf_len > 0 ) 
    {
        log_printf( &logger, "%s", app_buf );
        nbiot2_check_connection();
    }
    log_printf( &logger, "-----------------------------------\r\n" );
}

static void nbiot2_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case NBIOT2_OK:
        {
            nbiot2_log_response( );
            break;
        }
        case NBIOT2_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case NBIOT2_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    nbiot2_clear_app_buf(  );
    Delay_ms( 500 );
}

static void nbiot2_config_device_for_network( void )
{
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_CIMI );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_set_apn( &nbiot2, SIM_APN );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CREG, "2" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
}

static void nbiot2_check_connection_to_network( void )
{
    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_CGATT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_CEREG );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_COPS );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    if ( CONFIGURATION_FOR_EXAMPLE != app_connection_status )
    {
        log_info( &logger, "CONNECTED TO NETWORK" );
    }
}

static void nbiot2_send_sms ( void )
{
#if defined( SMS_TEXT_MODE )
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CMGF, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    log_printf( &logger, "> Sending message to phone number...\r\n" );
    nbiot2_send_sms_text_mode( &nbiot2, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    if ( NBIOT2_OK != app_error_flag )
    {
        log_printf( &logger, "> Message sent...\r\n" );
        Delay_ms( 10000 );
    }
#elif defined( SMS_PDU_MODE )
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CMGF, "0" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_sms_pdu ( &nbiot2, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    if ( NBIOT2_OK != app_error_flag )
    {
        log_printf( &logger, "> Message sent...\r\n" );
        Delay_ms( 10000 );
    }
#endif
}

static void nbiot2_config_device_for_tcp_udp ( void )
{
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CGATT, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    if ( NBIOT2_OK == app_error_flag )
    {
        app_connection_status = EXAMPLE;
    }
}

static void nbiot2_tcp_udp_example ( void )
{
    #define CONTEXTID       "1"
    #define TCP_PROTOCOL    "0"
    #define UDP_PROTOCOL    "1"
    #define URC_READ_SOCKET_DATA "+QIURC: \"recv\""
    
    char cmd_buf[ 100 ] = { 0 };
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 30000;
    
    // Get message length
    uint8_t message_len_buf[ 5 ] = { 0 };
    uint16_t message_len = strlen( MESSAGE_CONTENT );
    uint16_to_str( message_len, message_len_buf );
    l_trim( message_len_buf );
    r_trim( message_len_buf );
    
    // Create TCP socket
    strcpy( cmd_buf, CONTEXTID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_PROTOCOL );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, "TCP" );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "," ); 
    strcat( cmd_buf, REMOTE_PORT ); 
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIOPEN, cmd_buf );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    Delay_ms( 2000 );
    
    if ( NBIOT2_OK == app_error_flag )
    {
        log_info( &logger, "SENDING MESSAGES TO TCP" );
        // Write message to TCP socket
        strcpy( cmd_buf, TCP_PROTOCOL );
        strcat( cmd_buf, "," );
        strcat( cmd_buf, message_len_buf );
        strcat( cmd_buf, ",\"" );
        strcat( cmd_buf, MESSAGE_CONTENT );
        strcat( cmd_buf, "\"" );
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QISEND, cmd_buf );
    
        // Read response message from TCP socket
        for ( ; ; )
        {
            nbiot2_process( );
            uint8_t * __generic_ptr start_response_buf = strstr( app_buf, URC_READ_SOCKET_DATA );
            if ( start_response_buf )
            {
                log_info( &logger, "RECEIVED MESSAGE" );
                strcpy( cmd_buf, TCP_PROTOCOL );
                strcat( cmd_buf, "," );
                strcat( cmd_buf, "256" );
                nbiot2_clear_app_buf( );
                nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIRD, cmd_buf );
                app_error_flag = nbiot2_check_response( );
                nbiot2_error_check( app_error_flag );  
            
                if ( NBIOT2_OK == app_error_flag )
                {
                    break;
                }
            }
            if ( timeout_cnt++ > timeout )
            {
                log_error( &logger, "TIMEOUT!!!" );
                break;
            }   
            Delay_ms( 1 );
        }
        timeout_cnt = 0;
        Delay_ms( 2000 );
        
        // Close TCP socket
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QICLOSE, TCP_PROTOCOL );
        app_error_flag = nbiot2_check_response( );
        nbiot2_error_check( app_error_flag );  
    }
    else
    {
        log_error( &logger, "Not connected to the TCP server" );
    }
    
    // Create UDP socket
    strcpy( cmd_buf, CONTEXTID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_PROTOCOL );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, "UDP" );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "," ); 
    strcat( cmd_buf, REMOTE_PORT ); 
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIOPEN, cmd_buf );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    Delay_ms( 2000 );
    
    if ( NBIOT2_OK == app_error_flag )
    {
        log_info( &logger, "SENDING MESSAGES TO UDP" );
        // Write message to UDP socket
        strcpy( cmd_buf, UDP_PROTOCOL );
        strcat( cmd_buf, "," );
        strcat( cmd_buf, message_len_buf );
        strcat( cmd_buf, ",\"" );
        strcat( cmd_buf, MESSAGE_CONTENT );
        strcat( cmd_buf, "\"" );
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QISEND, cmd_buf );
    
        // Read response message from UDP socket
        for ( ; ; )
        {
            nbiot2_process( );
            uint8_t * __generic_ptr start_response_buf = strstr( app_buf, URC_READ_SOCKET_DATA );
            if ( start_response_buf )
            {
                log_info( &logger, "RECEIVED MESSAGE" );
                strcpy( cmd_buf, UDP_PROTOCOL );
                strcat( cmd_buf, "," );
                strcat( cmd_buf, "256" );
                nbiot2_clear_app_buf( );
                nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIRD, cmd_buf );
                app_error_flag = nbiot2_check_response( );
                nbiot2_error_check( app_error_flag );  
            
                if ( NBIOT2_OK == app_error_flag )
                {
                    break;
                }
            }
            if ( timeout_cnt++ > timeout )
            {
                log_error( &logger, "TIMEOUT!!!" );
                break;
            }   
            Delay_ms( 1 );
        }
        timeout_cnt = 0;
        Delay_ms( 2000 );
        
        // Close UDP socket
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QICLOSE, UDP_PROTOCOL );
        app_error_flag = nbiot2_check_response( );
        nbiot2_error_check( app_error_flag );  
    }
    else
    {
        log_error( &logger, "Not connected to the UDP server" );
    }
    
    Delay_ms( 5000 );
}




// ------------------------------------------------------------------------ END

Additional Support

Resources