Intermediate
30 min

Stay connected and in control with LARA-R6401D-00B and PIC32MZ2048EFH100 for secure and reliable IoT communication

Professional-grade multi-band LTE Cat 1 data-only solution for North American applications

4G LTE 2 Click - Data (for North America) with Flip&Click PIC32MZ

Published Nov 28, 2024

Click board™

4G LTE 2 Click - Data (for North America)

Dev. board

Flip&Click PIC32MZ

Compiler

NECTO Studio

MCU

PIC32MZ2048EFH100

Unlock the full potential of your IoT projects with this compact, data-only solution for secure and reliable connectivity in North America

A

A

Hardware Overview

How does it work?

4G LTE 2 Click - Data (for North America) is based on the LARA-R6401D module (LARA-R6401D-00B) from u-blox, a professional-grade multi-band and multi-mode module supporting LTE Cat 1 FDD (8 LTE bands - B2,4,5,12,13,14,66,71), providing the ideal solution for uncompromised connectivity in North American region. This Click board™ represents a data-only solution with all relevant MNO certifications, offering great flexibility except for the support of voice/audio applications. Versatile interfaces and features make the LARA-R6401D ideally suited for a wide range of applications that require medium data speed, superior coverage, and streaming data like asset tracking, telematics, remote monitoring, point-of-sale terminals, and more. This module requires a 3.8V power supply. Therefore, the Click board™ incorporates an integrated buck (step-down DC-DC) converter, the TPS7A7002 by Texas Instruments, which provides a stable 3.8V power supply, capable of mitigating voltage drops at the input when a high current peak appears (typically at the StartUp of the device). The module Ignition (Power-On) pin, labeled as PWR and routed to the RST pin on the mikroBUS™ socket, enables power

to be switched on and off. The LARA-R6401D communicates with an MCU using the UART interface with commonly used UART RX and TX pins with the hardware flow control pins UART CTS, RTS, RI (Clear to Send, Ready to Send, and Ring Indicator). It operates at 115200 bps by default configuration to transmit and exchange data with the host MCU through AT commands that u-blox provides. Besides the UART interface, the LARA-R6401D also provides the possibility of using the I2C interface as an I2C host, which can communicate with I2C local devices via the I2C bus specifications. This Click board™ is also equipped with a USB type C connector for diagnostic purposes only. The module acts as a USB device and can be connected to any USB host equipped with compatible drivers. This board is also equipped with all the necessary components and circuitry for an audio interface supported by the module family. However, in the current version, with the module placed on the board, the audio interface is not supported. As a result, those components have not been soldered onto the board. Among its used pins, this Click board™ also possesses two additional LED indicators: the yellow LED labeled

as STATUS, routed both to the mikroBUS™ AN pin (STS) and the LED, used to indicate the status of the network connection visually, and a red LED labeled as TX used to tell the transmitting status of the module. The LTE cellular networks use spatial multiplexing antenna technology, allowing more than one antenna to be used for better reception. For that reason, besides the primary TX/RX antenna, this Click board™ uses a secondary diversity RX antenna, which allows better signal reception. Alongside the SMA connectors, the 4G LTE 2 Click also has a Nano-SIM card slot that provides multiple connections and interface options alongside several test points labeled from TP1 to TP3, enabling easy reboot and testing of the module. This Click board™ can operate with either 3.3V or 5V logic voltage levels selected via the VCC SEL jumper. This way, both 3.3V and 5V capable MCUs can use the communication lines properly. However, the Click board™ comes equipped with a library containing easy-to-use functions and an example code that can be used, as a reference, for further development.

4G LTE 2 Click - Data (for North America) hardware overview image

Features overview

Development board

Flip&Click PIC32MZ is a compact development board designed as a complete solution that brings the flexibility of add-on Click boards™ to your favorite microcontroller, making it a perfect starter kit for implementing your ideas. It comes with an onboard 32-bit PIC32MZ microcontroller, the PIC32MZ2048EFH100 from Microchip, four mikroBUS™ sockets for Click board™ connectivity, two USB connectors, LED indicators, buttons, debugger/programmer connectors, and two headers compatible with Arduino-UNO pinout. Thanks to innovative manufacturing technology,

it allows you to build gadgets with unique functionalities and features quickly. Each part of the Flip&Click PIC32MZ development kit contains the components necessary for the most efficient operation of the same board. In addition, there is the possibility of choosing the Flip&Click PIC32MZ programming method, using the chipKIT bootloader (Arduino-style development environment) or our USB HID bootloader using mikroC, mikroBasic, and mikroPascal for PIC32. This kit includes a clean and regulated power supply block through the USB Type-C (USB-C) connector. All communication

methods that mikroBUS™ itself supports are on this board, including the well-established mikroBUS™ socket, user-configurable buttons, and LED indicators. Flip&Click PIC32MZ development kit allows you to create a new application in minutes. Natively supported by Mikroe software tools, it covers many aspects of prototyping thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

Flip&Click PIC32MZ double image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC32

MCU Memory (KB)

2048

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

524288

You complete me!

Accessories

868MHz right-angle rubber antenna is a compact and versatile solution for wireless communication. Operating within the frequency range of 868-915MHz, it ensures optimal signal reception and transmission. With a 50-ohm impedance, it's compatible with various devices and systems. This antenna boasts a 2dB gain, enhancing signal strength and extending communication range. Its vertical polarization further contributes to signal clarity. Designed to handle up to 50W of input power, it's a robust choice for various applications. Measuring just 48mm in length, this antenna is both discreet and practical. Its SMA male connector ensures a secure and reliable connection to your equipment. Whether you're working with IoT devices, remote sensors, or other wireless technologies, the 868MHz right-angle antenna offers the performance and flexibility you need for seamless communication.

4G LTE 2 Click - Data (for North America) accessories 1 image

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.

4G LTE 2 Click - Data (for North America) accessories 2 image

Used MCU Pins

mikroBUS™ mapper

Module Status
RB11
AN
Power-ON
RE2
RST
UART RTS
RA0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
RC14
PWM
UART CTS
RD9
INT
UART TX
RE3
TX
UART RX
RG9
RX
I2C Clock
RA2
SCL
I2C Data
RA3
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

4G LTE 2 Click - Data (for North America) Schematic schematic

Step by step

Project assembly

Flip&Click PIC32MZ front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Flip&Click PIC32MZ as your development board.

Flip&Click PIC32MZ front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Flip&Click PIC32MZ MB1 Access - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Flip&Click PIC32MZ MCU step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for 4G LTE 2 Click - Data (for North America) driver.

Key functions:

  • c4glte2datana_set_power_state - This function sets a desired power state by toggling PWR pin with a specific time for high state.

  • c4glte2datana_set_sim_apn - This function sets APN for SIM card.

  • c4glte2datana_send_sms_text - This function sends text message to a phone number.

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief 4G LTE 2 Data-NA Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, restarts the module and tests the communication.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - C4GLTE2DATANA_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - C4GLTE2DATANA_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CREG URC event and then checks the connection status.
 *
 *  - C4GLTE2DATANA_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
 *
 *  - C4GLTE2DATANA_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode) or TCP/UDP message.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void c4glte2datana_clear_app_buf ( void )
 * - static err_t c4glte2datana_process ( void )
 * - static void c4glte2datana_error_check( err_t error_flag )
 * - static void c4glte2datana_log_app_buf ( void )
 * - static err_t c4glte2datana_rsp_check ( void )
 * - static err_t c4glte2datana_configure_for_network( void )
 * - static err_t c4glte2datana_check_connection( void )
 * - static err_t c4glte2datana_configure_for_example( void )
 * - static err_t c4glte2datana_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 (SMS mode only) 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 Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "c4glte2datana.h"
#include "generic_pointer.h"
#include "conversions.h"

// Example selection macros
#define EXAMPLE_TCP_UDP                     0               // Example of sending messages to a TCP/UDP echo server
#define EXAMPLE_SMS                         1               // Example of sending SMS to a phone number
#define DEMO_EXAMPLE                        EXAMPLE_TCP_UDP // Example selection macro

// SIM APN config
#define SIM_APN                             ""              // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER_TO_MESSAGE             ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

// 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

// Message content
#define MESSAGE_CONTENT                     "4G LTE 2 Data-NA Click board - demo example."

// Application buffer size
#define PROCESS_BUFFER_SIZE                 300

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    C4GLTE2DATANA_CONFIGURE_FOR_NETWORK = 1,
    C4GLTE2DATANA_WAIT_FOR_CONNECTION,
    C4GLTE2DATANA_CONFIGURE_FOR_EXAMPLE,
    C4GLTE2DATANA_EXAMPLE

} c4glte2datana_example_state_t;

static c4glte2datana_t c4glte2datana;
static log_t logger;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
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 error_flag;
static c4glte2datana_example_state_t example_state;

/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application
 * buffer and reset its length and counter.
 */
static void c4glte2datana_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and
 * appends it to the application buffer.
 * @return @li @c  0 - Some data is read.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_process ( 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.
 */
static void c4glte2datana_error_check( err_t error_flag );

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

/**
 * @brief Response check.
 * @details This function checks for response and
 * returns the status of response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_rsp_check ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable
 * connection to the specified network.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_configure_for_network( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_check_connection( void );

/**
 * @brief Configure device for example.
 * @details Configure device for the specified example.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_configure_for_example( void );

/**
 * @brief Execute example.
 * @details This function executes SMS or TCP/UDP example depending on the DEMO_EXAMPLE macro.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 */
static err_t c4glte2datana_example( void );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    c4glte2datana_cfg_t c4glte2datana_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.
    c4glte2datana_cfg_setup( &c4glte2datana_cfg );
    C4GLTE2DATANA_MAP_MIKROBUS( c4glte2datana_cfg, MIKROBUS_1 );
    if ( UART_ERROR == c4glte2datana_init( &c4glte2datana, &c4glte2datana_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    c4glte2datana_set_power_state ( &c4glte2datana, C4GLTE2DATANA_POWER_STATE_OFF );
    c4glte2datana_set_power_state ( &c4glte2datana, C4GLTE2DATANA_POWER_STATE_ON );
    c4glte2datana_process( );
    c4glte2datana_clear_app_buf( );
    app_buf_len = 0;
    app_buf_cnt = 0;

    // Check communication
    c4glte2datana_send_cmd( &c4glte2datana, C4GLTE2DATANA_CMD_AT );
    error_flag = c4glte2datana_rsp_check( );
    c4glte2datana_error_check( error_flag );

    log_info( &logger, " Application Task " );
    example_state = C4GLTE2DATANA_CONFIGURE_FOR_NETWORK;
}

void application_task ( void )
{
    switch ( example_state )
    {
        case C4GLTE2DATANA_CONFIGURE_FOR_NETWORK:
        {
            if ( C4GLTE2DATANA_OK == c4glte2datana_configure_for_network( ) )
            {
                example_state = C4GLTE2DATANA_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case C4GLTE2DATANA_WAIT_FOR_CONNECTION:
        {
            if ( C4GLTE2DATANA_OK == c4glte2datana_check_connection( ) )
            {
                example_state = C4GLTE2DATANA_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case C4GLTE2DATANA_CONFIGURE_FOR_EXAMPLE:
        {
            if ( C4GLTE2DATANA_OK == c4glte2datana_configure_for_example( ) )
            {
                example_state = C4GLTE2DATANA_EXAMPLE;
            }
            break;
        }
        case C4GLTE2DATANA_EXAMPLE:
        {
            c4glte2datana_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example state." );
            break;
        }
    }
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

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

static err_t c4glte2datana_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    rx_size = c4glte2datana_generic_read( &c4glte2datana, rx_buff, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE )
        {
            c4glte2datana_clear_app_buf(  );
            return C4GLTE2DATANA_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_buff[ rx_cnt ] != 0 )
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
        return C4GLTE2DATANA_OK;
    }
    return C4GLTE2DATANA_ERROR;
}

static err_t c4glte2datana_rsp_check ( void )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    err_t error_flag = c4glte2datana_process( );
    if ( ( C4GLTE2DATANA_OK != error_flag ) && ( C4GLTE2DATANA_ERROR != error_flag ) )
    {
        return error_flag;
    }
    while ( ( 0 == strstr( app_buf, C4GLTE2DATANA_RSP_OK ) ) &&
            ( 0 == strstr( app_buf, C4GLTE2DATANA_RSP_ERROR ) ) )
    {
        error_flag = c4glte2datana_process( );
        if ( ( C4GLTE2DATANA_OK != error_flag ) && ( C4GLTE2DATANA_ERROR != error_flag ) )
        {
            return error_flag;
        }
        if ( timeout_cnt++ > timeout )
        {
            c4glte2datana_clear_app_buf( );
            return C4GLTE2DATANA_ERROR_TIMEOUT;
        }
        Delay_ms ( 1 );
    }
    if ( strstr( app_buf, C4GLTE2DATANA_RSP_OK ) )
    {
        return C4GLTE2DATANA_OK;
    }
    else if ( strstr( app_buf, C4GLTE2DATANA_RSP_ERROR ) )
    {
        return C4GLTE2DATANA_ERROR_CMD;
    }
    else
    {
        return C4GLTE2DATANA_ERROR_UNKNOWN;
    }
}

static void c4glte2datana_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case C4GLTE2DATANA_OK:
        {
            c4glte2datana_log_app_buf( );
            break;
        }
        case C4GLTE2DATANA_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case C4GLTE2DATANA_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case C4GLTE2DATANA_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case C4GLTE2DATANA_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    c4glte2datana_clear_app_buf(  );
    Delay_ms ( 500 );
}

static void c4glte2datana_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
}

static err_t c4glte2datana_configure_for_network( void )
{
    err_t func_error = C4GLTE2DATANA_OK;
    // Deregister from network
    #define DEREGISTER_FROM_NETWORK "2"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_COPS, DEREGISTER_FROM_NETWORK );
    error_flag = c4glte2datana_rsp_check();
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
    
    // Set SIM APN
    c4glte2datana_set_sim_apn( &c4glte2datana, SIM_APN );
    error_flag = c4glte2datana_rsp_check();
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = c4glte2datana_rsp_check();
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Automatic registration
    #define AUTOMATIC_REGISTRATION "0"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_COPS, AUTOMATIC_REGISTRATION );
    error_flag = c4glte2datana_rsp_check();
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Enable network registartion
    #define ENABLE_REG "2"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_CREG, ENABLE_REG );
    error_flag = c4glte2datana_rsp_check();
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
    return func_error;
}

static err_t c4glte2datana_check_connection( void )
{
    #define CONNECTED "+CREG: 1"
    c4glte2datana_process( );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        c4glte2datana_process( );
        c4glte2datana_log_app_buf( );
        log_printf( &logger, "\r\n" );
        c4glte2datana_clear_app_buf( );
        // Check signal quality
        c4glte2datana_send_cmd( &c4glte2datana, C4GLTE2DATANA_CMD_CSQ );
        error_flag = c4glte2datana_rsp_check( );
        c4glte2datana_error_check( error_flag );
        return error_flag;
    }
    return C4GLTE2DATANA_ERROR;
}

static err_t c4glte2datana_configure_for_example( void )
{
    err_t func_error = C4GLTE2DATANA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ACTIVATE_PDP_CONTEXT "1,1"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_CGACT, ACTIVATE_PDP_CONTEXT );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_CMGF, SMS_MODE );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

static err_t c4glte2datana_example( void )
{
    err_t func_error = C4GLTE2DATANA_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    char cmd_buf[ 100 ] = { 0 };
    char urc_buf[ 20 ] = { 0 };
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 30000;
    uint8_t * __generic_ptr socket_num_buf = 0;
    uint8_t tcp_socket_num[ 2 ] = { 0 };
    uint8_t udp_socket_num[ 2 ] = { 0 };

    // Create TCP socket
    #define RSP_USOCR "+USOCR: "
    #define TCP_PROTOCOL "6"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCR, TCP_PROTOCOL );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    tcp_socket_num[ 0 ] = *socket_num_buf;
    c4glte2datana_error_check( error_flag );

    // Create UDP socket
    #define UDP_PROTOCOL "17"
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCR, UDP_PROTOCOL );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    udp_socket_num[ 0 ] = *socket_num_buf;
    c4glte2datana_error_check( error_flag );

    // Connect TCP socket to remote IP and port
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCO, cmd_buf );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Connect UDP socket to remote IP and port
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCO, cmd_buf );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // 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 );

    // Write message to TCP socket
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOWR, cmd_buf );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Read response message from TCP socket
    #define URC_READ_SOCKET_DATA_TCP "+UUSORD: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_TCP );
    strcat( urc_buf, tcp_socket_num );
    for ( ; ; )
    {
        c4glte2datana_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms ( 100 );
            c4glte2datana_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, tcp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            c4glte2datana_log_app_buf( );
            c4glte2datana_clear_app_buf( );
            c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USORD, cmd_buf );
            error_flag = c4glte2datana_rsp_check( );
            func_error |= error_flag;
            c4glte2datana_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms ( 1 );
    }
    timeout_cnt = 0;

    // Write message to UDP socket
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOWR, cmd_buf );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );

    // Read response message from UDP socket
    #define URC_READ_SOCKET_DATA_UDP "+UUSORD: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_UDP );
    strcat( urc_buf, udp_socket_num );
    for ( ; ; )
    {
        c4glte2datana_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms ( 100 );
            c4glte2datana_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, udp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            c4glte2datana_log_app_buf( );
            c4glte2datana_clear_app_buf( );
            c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USORD, cmd_buf );
            error_flag = c4glte2datana_rsp_check( );
            func_error |= error_flag;
            c4glte2datana_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms ( 1 );
    }
    
    // Close TCP socket
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCL, tcp_socket_num );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
    
    // Close UDP socket
    c4glte2datana_send_cmd_with_parameter( &c4glte2datana, C4GLTE2DATANA_CMD_USOCL, udp_socket_num );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    c4glte2datana_error_check( error_flag );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    c4glte2datana_send_cmd_check( &c4glte2datana, C4GLTE2DATANA_CMD_CMGF );
    error_flag = c4glte2datana_rsp_check( );
    func_error |= error_flag;
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        c4glte2datana_error_check( error_flag );
        // Send SMS in PDU mode
        c4glte2datana_send_sms_pdu( &c4glte2datana, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = c4glte2datana_rsp_check( );
        func_error |= error_flag;
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        c4glte2datana_error_check( error_flag );
        // Send SMS in TXT mode
        c4glte2datana_send_sms_text ( &c4glte2datana, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = c4glte2datana_rsp_check( );
        func_error |= error_flag;
    }
    c4glte2datana_error_check( error_flag );
    // 30 seconds delay
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.