Intermediate
30 min

Discover a smarter approach to IoT connectivity with SKY66430-11 and PIC32MZ1024EFH064

Stay ahead in the world of IoT

LTE IoT 8 Click with PIC32MZ clicker

Published Sep 13, 2023

Click board™

LTE IoT 8 Click

Dev Board

PIC32MZ clicker

Compiler

NECTO Studio

MCU

PIC32MZ1024EFH064

Experience a transformation in IoT communication with LTE IoT, as the integration of CAT M1 and NB1 technologies delivers robust and future-proof connectivity solutions

A

A

Hardware Overview

How does it work?

LTE IoT 8 Click is based on the SKY66430-11, 5G Ready IoT SiP, compact low-power solution for LTE-M and NB-IoT connectivity in wearables, asset tracking, industrial monitoring, and smart metering from Skyworks Solutions and Sequans Communications. The SiP integrates the RF front-end, transceiver, baseband modem, memory, and power management, providing 18 bands from 698 to 2200MHz. It is pre-certified with major network operators, regulatory agencies, and standards organizations for fast global deployment. The device offers up to 300kbps download and 375kbps upload, supporting Positioning over LTE. It also has the best-in-class “Deep Sleep” power consumption of 1uA, representing a significant benefit for battery-powered IoT devices. Besides, it features a 100% software-based low-power secure positioning solution that works anywhere (enables location deep indoors where GPS doesn’t work). The SKY66430-11 communicates with 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. It is also equipped with a USB type C connector, which allows the module to be powered and configured by a personal computer (PC) using FT2232D, a compact USB to a serial UART interface device designed to operate efficiently with USB host controllers. This Click board™ can be battery-powered and used as a stand-alone device. It also contains the MC34671, a fully-integrated Li-Ion or Li-Polymer battery charger allowing a battery charge when Click board™ is inserted in a mikroBUS™ socket or plugged into a USB port with the CHG LED indicator used as an indicator of the charging progress. CHG LED will turn off once the battery charging is finished. Also, this Click board™ has an additional yellow LED labeled as STAT that indicates different operating modes of the module, while the PWM pin on the mikroBUS™ socket is used as a Power Saving Status

indicator. This indicator is set to a high logic state when the modem is active or a low logic state when the modem is in low power mode. An onboard pushbutton labeled ON is routed to the RST pin on the mikroBUS™, representing the Ignition button. LTE IoT 8 Click has the SMA antenna connector with an impedance of 50Ω for connecting the appropriate antenna that MIKROE offers. Besides the LTE SMA connector, this Click board™ has a Nano-SIM card slot that provides multiple connections and interface options. This Click board™ can operate with both 3.3V and 5V MCUs. Appropriate voltage level translator TXS0108E performs a proper logic voltage level conversion while the onboard LDOs ensure the recommended voltage levels of the power module. 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.

LTE IoT 8 Click hardware overview image

Features overview

Development board

PIC32MZ Clicker is a compact starter development board 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 with FPU from Microchip, a USB connector, LED indicators, buttons, a mikroProg connector, and a header for interfacing with external electronics. Thanks to its compact design with clear and easy-recognizable silkscreen markings, it provides a fluid and immersive working experience, allowing access anywhere and under

any circumstances. Each part of the PIC32MZ Clicker development kit contains the components necessary for the most efficient operation of the same board. In addition to the possibility of choosing the PIC32MZ Clicker programming method, using USB HID mikroBootloader, or through an external mikroProg connector for PIC, dsPIC, or PIC32 programmer, the Clicker board also includes a clean and regulated power supply module for the development kit. The USB Micro-B connection can provide up to 500mA of current, which is more than enough to operate all onboard

and additional modules. All communication methods that mikroBUS™ itself supports are on this board, including the well-established mikroBUS™ socket, reset button, and several buttons and LED indicators. PIC32MZ Clicker is an integral part of the Mikroe ecosystem, allowing 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.

PIC32MZ clicker double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC32

MCU Memory (KB)

1024

Silicon Vendor

Microchip

Pin count

64

RAM (Bytes)

524288

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.

LTE IoT 8 Click accessories image

Used MCU Pins

mikroBUS™ mapper

UART RI
RE4
AN
Ignition
RE5
RST
UART CTS
RG9
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Power-Saving Status
RB3
PWM
UART RTS
RB5
INT
UART TX
RB2
TX
UART RX
RB0
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

LTE IoT 8 Click Schematic schematic

Step by step

Project assembly

PIC32MZ clicker front image hardware assembly

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

PIC32MZ clicker front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Micro B Connector Clicker 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

This Click board can be interfaced and monitored in two ways:

  • Application Output - Use the "Application Output" window in Debug mode for real-time data monitoring. Set it up properly by following this tutorial.

Software Support

Library Description

This library contains API for LTE IoT 8 Click driver.

Key functions:

  • lteiot8_send_cmd - Send command function

  • lteiot8_set_sim_apn - Set SIM APN

  • lteiot8_send_text_message - Send SMS message to number in text mode

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 LTE IoT 8 Click Example.
 *
 * # Description
 * Application example shows device capability to connect 
 * network and send SMS messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver and wake-up module and test communication.
 *
 * ## Application Task
 * Application taks is split in few stages:
 *  - LTEIOT8_CONFIGURE_FOR_CONNECTION: 
 * Sets configuration to device to be able to connect to newtork.
 * 
 *  - LTEIOT8_WAIT_FOR_CONNECTION: 
 * Checks device response untill device sends information 
 * that it is connected to network.
 * 
 *  - LTEIOT8_CHECK_CONNECTION:
 * Checks device connection status parameters.
 * 
 *  - LTEIOT8_CONFIGURE_FOR_MESSAGES:
 * Sets configuration to device to send SMS messages.
 * 
 *  - LTEIOT8_MESSAGES:
 * Sends message in selected mode (PDU/TXT).
 *
 *
 * ## Additional Function
 * - static void lteiot8_clear_app_buf ( void )
 * - static err_t lteiot8_process ( void )
 * - static void lteiot8_error_check( err_t error_flag )
 * - static void lteiot8_log_app_buf ( void )
 * - static err_t lteiot8_rsp_check ( void )
 * - static err_t lteiot8_configure_for_connection( void )
 * - static err_t lteiot8_check_connection( void )
 * - static err_t lteiot8_check_connection_parameters( void )
 * - static err_t lteiot8_configure_for_meesages( void )
 * - static err_t lteiot8_send_meesage( void )
 * - 
 *
 * @note
 *  * In order for the example to work, user needs to set the phone number to which he wants 
 * to send an SMS, and also will need to set an APN and SMSC of entered SIM card.
 * Enter valid data for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * E.g. 
 * SIM_APN                     "vip.iot"
 * SMSC_ADDRESS_CSCA           "\"+381999999\",145"
 * SMSC_ADDRESS_PDU            "+381999999\" 
 * PHONE_NUMBER_TO_MESSAGE     "+381659999999"
 *
 * @author Luka Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lteiot8.h"

//Set valid SIM APN
#define SIM_APN                     ""
//Set Phone number to message
#define PHONE_NUMBER_TO_MESSAGE     "+381659999999"
//Messege content 
#define MESSAGE_CONTENT             "LTE IoT 8 Click"
//Set valid SMSC fro SIM
#define SMSC_ADDRESS_CSCA           "\"+381999999\",145" 
#define SMSC_ADDRESS_PDU            "+381999999" 

#define PROCESS_BUFFER_SIZE         300

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
   LTEIOT8_CONFIGURE_FOR_CONNECTION = 1,
   LTEIOT8_WAIT_FOR_CONNECTION,
   LTEIOT8_CHECK_CONNECTION,
   LTEIOT8_CONFIGURE_FOR_MESSAGES,
   LTEIOT8_MESSAGES

} lteiot8_example_state_t;

static lteiot8_t lteiot8;
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 lteiot8_example_state_t example_state;


/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application 
 * buffer and reset it's length and counter.
 */
static void lteiot8_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and 
 * appends 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.
 */
static err_t lteiot8_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 lteiot8_error_check( err_t error_flag );

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

/**
 * @brief Response check.
 * @details This function checks for response and 
 * returns the status of response.
 * 
 * @return application status.
 * See #err_t definition for detailed explanation.
 */
static err_t lteiot8_rsp_check ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable 
 * connection to the secifide network.
 */
static err_t lteiot8_configure_for_connection( void );

/**
 * @brief Whait for connection signal.
 * @details Wait for connection signal from CREG command.
 */
static err_t lteiot8_check_connection( void );

/**
 * @brief Check connection parameters.
 * @details This function checks connection parameters.
 */
static err_t lteiot8_check_connection_parameters( void );

/**
 * @brief Configure device for sending messages.
 * @details Configure device to send txt mode 
 * messages and SMSC of the SIM card.
 */
static err_t lteiot8_configure_for_meesages( void );

/**
 * @brief Sending text message.
 * @details This function sends text messages to predefined number.
 */
static err_t lteiot8_send_meesage( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot8_cfg_t lteiot8_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.
    lteiot8_cfg_setup( &lteiot8_cfg );
    LTEIOT8_MAP_MIKROBUS( lteiot8_cfg, MIKROBUS_1 );
    err_t init_flag  = lteiot8_init( &lteiot8, &lteiot8_cfg );
    if ( init_flag == UART_ERROR ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }
    lteiot8_process();
    lteiot8_clear_app_buf(); 
    app_buf_len = 0;
    app_buf_cnt = 0;
    
    lteiot8_default_cfg ( &lteiot8 );
    while ( 0 == strstr( app_buf, LTEIOT8_RSP_SYSTART ) )
    {
        lteiot8_process();
    }
    
    //Check communication
    lteiot8_send_cmd( &lteiot8, "AT" );
    error_flag = lteiot8_rsp_check();
    lteiot8_error_check( error_flag );
    
    log_info( &logger, " Application Task " );
    example_state = LTEIOT8_CONFIGURE_FOR_CONNECTION;
}

void application_task ( void ) 
{
    switch ( example_state )
    {
        case LTEIOT8_CONFIGURE_FOR_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_configure_for_connection( ) )
            {
                example_state = LTEIOT8_WAIT_FOR_CONNECTION;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_WAIT_FOR_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_check_connection( ) )
            {
                example_state = LTEIOT8_CHECK_CONNECTION;
            }
            break;
        }
        case LTEIOT8_CHECK_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_check_connection_parameters( ) )
            {
                example_state = LTEIOT8_CONFIGURE_FOR_MESSAGES;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_CONFIGURE_FOR_MESSAGES:
        {
            if ( LTEIOT8_OK == lteiot8_configure_for_meesages( ) )
            {
                example_state = LTEIOT8_MESSAGES;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_MESSAGES:
        {
            lteiot8_send_meesage();
            // 10 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 );
            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 lteiot8_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t lteiot8_process ( void ) 
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = lteiot8_generic_read( &lteiot8, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE ) 
        {
           lteiot8_clear_app_buf(  );
            return LTEIOT8_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 LTEIOT8_OK;
    }
    return LTEIOT8_ERROR;
}

static err_t lteiot8_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;
    
    err_t error_flag = lteiot8_process(  );
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }
    
    while ( ( 0 == strstr( app_buf, LTEIOT8_RSP_OK ) ) && 
            ( 0 == strstr( app_buf, LTEIOT8_RSP_ERROR ) ) )
    {
        error_flag = lteiot8_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }
        
        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            lteiot8_clear_app_buf(  );
            return LTEIOT8_ERROR_TIMEOUT;
        }
        
        Delay_ms ( 1 );
    }
    
    if ( 0 != strstr( app_buf, LTEIOT8_RSP_OK ) )
    {
        return LTEIOT8_OK;
    }
    else if ( 0 == strstr( app_buf, LTEIOT8_RSP_ERROR ) )
    {
        return LTEIOT8_ERROR_CMD;
    }
    else
    {
        return LTEIOT8_ERROR_UNKNOWN;
    }
}

static void lteiot8_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case LTEIOT8_OK:
        {
            lteiot8_log_app_buf( );
            break;
        }
        case LTEIOT8_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case LTEIOT8_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case LTEIOT8_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case LTEIOT8_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    lteiot8_clear_app_buf(  );
    Delay_ms ( 500 );
}

static void lteiot8_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 lteiot8_configure_for_connection( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Enable device LED
    #define GNSS_LEN "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_SQNLED, GNSS_LEN );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Set SIM APN
    lteiot8_set_sim_apn( &lteiot8, SIM_APN );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Automatic configure cops
    #define AUTOMATIC "0"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_COPS, AUTOMATIC );
    error_flag = lteiot8_rsp_check(); 
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Enable network registartion
    #define ENABLE_REG "2"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CEREG, ENABLE_REG );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    return func_error;
}

static err_t lteiot8_check_connection( void )
{
    #define CONNECTED "+CEREG: 1"
    lteiot8_process();
    if ( 0 != strstr(app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        lteiot8_process();
        lteiot8_log_app_buf( );
        lteiot8_clear_app_buf(  );
        return LTEIOT8_OK;
    }
    
    return LTEIOT8_ERROR;
}

static err_t lteiot8_check_connection_parameters( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Check if connected
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CGATT );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Check registration
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CEREG );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Check signal quality
    lteiot8_send_cmd( &lteiot8, LTEIOT8_CMD_CSQ );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    return func_error;
}

static err_t lteiot8_configure_for_meesages( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Set message mode
    #define PDU_MESSAGE_MODE "0"
    #define TXT_MESSAGE_MODE "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CMGF, TXT_MESSAGE_MODE );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Set SMSC for SIM card
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CSCA, SMSC_ADDRESS_CSCA );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );

    return func_error;
}

static err_t lteiot8_send_meesage( void )
{
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    
    //Sendind message  
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CMGF );
    error_flag = lteiot8_rsp_check();
    
    if ( 0 != strstr( app_buf, CMGF_PDU ) )
    {
        lteiot8_clear_app_buf(  );
        lteiot8_send_sms_pdu( &lteiot8, SMSC_ADDRESS_PDU, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot8_rsp_check();
        lteiot8_error_check( error_flag );
    }
    else if ( 0 != strstr( app_buf, CMGF_TXT ) )
    {
        lteiot8_clear_app_buf(  );
        lteiot8_send_text_message( &lteiot8, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot8_rsp_check();
        lteiot8_error_check( error_flag );
    }
    
    return error_flag;
}


// ------------------------------------------------------------------------ END
/*!
 * @file main.c
 * @brief LTE IoT 8 Click Example.
 *
 * # Description
 * Application example shows device capability to connect 
 * network and send SMS messages using standard "AT" commands.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver and wake-up module and test communication.
 *
 * ## Application Task
 * Application taks is split in few stages:
 *  - LTEIOT8_CONFIGURE_FOR_CONNECTION: 
 * Sets configuration to device to be able to connect to newtork.
 * 
 *  - LTEIOT8_WAIT_FOR_CONNECTION: 
 * Checks device response untill device sends information 
 * that it is connected to network.
 * 
 *  - LTEIOT8_CHECK_CONNECTION:
 * Checks device connection status parameters.
 * 
 *  - LTEIOT8_CONFIGURE_FOR_MESSAGES:
 * Sets configuration to device to send SMS messages.
 * 
 *  - LTEIOT8_MESSAGES:
 * Sends message in selected mode (PDU/TXT).
 *
 *
 * ## Additional Function
 * - static void lteiot8_clear_app_buf ( void )
 * - static err_t lteiot8_process ( void )
 * - static void lteiot8_error_check( err_t error_flag )
 * - static void lteiot8_log_app_buf ( void )
 * - static err_t lteiot8_rsp_check ( void )
 * - static err_t lteiot8_configure_for_connection( void )
 * - static err_t lteiot8_check_connection( void )
 * - static err_t lteiot8_check_connection_parameters( void )
 * - static err_t lteiot8_configure_for_meesages( void )
 * - static err_t lteiot8_send_meesage( void )
 * - 
 *
 * @note
 *  * In order for the example to work, user needs to set the phone number to which he wants 
 * to send an SMS, and also will need to set an APN and SMSC of entered SIM card.
 * Enter valid data for the following macros: SIM_APN, SIM_SMSC and PHONE_NUMBER_TO_MESSAGE.
 * E.g. 
 * SIM_APN                     "vip.iot"
 * SMSC_ADDRESS_CSCA           "\"+381999999\",145"
 * SMSC_ADDRESS_PDU            "+381999999\" 
 * PHONE_NUMBER_TO_MESSAGE     "+381659999999"
 *
 * @author Luka Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lteiot8.h"

//Set valid SIM APN
#define SIM_APN                     ""
//Set Phone number to message
#define PHONE_NUMBER_TO_MESSAGE     "+381659999999"
//Messege content 
#define MESSAGE_CONTENT             "LTE IoT 8 Click"
//Set valid SMSC fro SIM
#define SMSC_ADDRESS_CSCA           "\"+381999999\",145" 
#define SMSC_ADDRESS_PDU            "+381999999" 

#define PROCESS_BUFFER_SIZE         300

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
   LTEIOT8_CONFIGURE_FOR_CONNECTION = 1,
   LTEIOT8_WAIT_FOR_CONNECTION,
   LTEIOT8_CHECK_CONNECTION,
   LTEIOT8_CONFIGURE_FOR_MESSAGES,
   LTEIOT8_MESSAGES

} lteiot8_example_state_t;

static lteiot8_t lteiot8;
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 lteiot8_example_state_t example_state;


/**
 * @brief Clearing application buffer.
 * @details This function clears memory of application 
 * buffer and reset it's length and counter.
 */
static void lteiot8_clear_app_buf ( void );

/**
 * @brief Data reading function.
 * @details This function reads data from device and 
 * appends 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.
 */
static err_t lteiot8_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 lteiot8_error_check( err_t error_flag );

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

/**
 * @brief Response check.
 * @details This function checks for response and 
 * returns the status of response.
 * 
 * @return application status.
 * See #err_t definition for detailed explanation.
 */
static err_t lteiot8_rsp_check ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable 
 * connection to the secifide network.
 */
static err_t lteiot8_configure_for_connection( void );

/**
 * @brief Whait for connection signal.
 * @details Wait for connection signal from CREG command.
 */
static err_t lteiot8_check_connection( void );

/**
 * @brief Check connection parameters.
 * @details This function checks connection parameters.
 */
static err_t lteiot8_check_connection_parameters( void );

/**
 * @brief Configure device for sending messages.
 * @details Configure device to send txt mode 
 * messages and SMSC of the SIM card.
 */
static err_t lteiot8_configure_for_meesages( void );

/**
 * @brief Sending text message.
 * @details This function sends text messages to predefined number.
 */
static err_t lteiot8_send_meesage( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot8_cfg_t lteiot8_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.
    lteiot8_cfg_setup( &lteiot8_cfg );
    LTEIOT8_MAP_MIKROBUS( lteiot8_cfg, MIKROBUS_1 );
    err_t init_flag  = lteiot8_init( &lteiot8, &lteiot8_cfg );
    if ( init_flag == UART_ERROR ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }
    lteiot8_process();
    lteiot8_clear_app_buf(); 
    app_buf_len = 0;
    app_buf_cnt = 0;
    
    lteiot8_default_cfg ( &lteiot8 );
    while ( 0 == strstr( app_buf, LTEIOT8_RSP_SYSTART ) )
    {
        lteiot8_process();
    }
    
    //Check communication
    lteiot8_send_cmd( &lteiot8, "AT" );
    error_flag = lteiot8_rsp_check();
    lteiot8_error_check( error_flag );
    
    log_info( &logger, " Application Task " );
    example_state = LTEIOT8_CONFIGURE_FOR_CONNECTION;
}

void application_task ( void ) 
{
    switch ( example_state )
    {
        case LTEIOT8_CONFIGURE_FOR_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_configure_for_connection( ) )
            {
                example_state = LTEIOT8_WAIT_FOR_CONNECTION;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_WAIT_FOR_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_check_connection( ) )
            {
                example_state = LTEIOT8_CHECK_CONNECTION;
            }
            break;
        }
        case LTEIOT8_CHECK_CONNECTION:
        {
            if ( LTEIOT8_OK == lteiot8_check_connection_parameters( ) )
            {
                example_state = LTEIOT8_CONFIGURE_FOR_MESSAGES;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_CONFIGURE_FOR_MESSAGES:
        {
            if ( LTEIOT8_OK == lteiot8_configure_for_meesages( ) )
            {
                example_state = LTEIOT8_MESSAGES;
            }
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            Delay_ms ( 1000 );
            break;
        }
        case LTEIOT8_MESSAGES:
        {
            lteiot8_send_meesage();
            // 10 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 );
            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 lteiot8_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t lteiot8_process ( void ) 
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = lteiot8_generic_read( &lteiot8, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE ) 
        {
           lteiot8_clear_app_buf(  );
            return LTEIOT8_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 LTEIOT8_OK;
    }
    return LTEIOT8_ERROR;
}

static err_t lteiot8_rsp_check ( void )
{
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 10000;
    
    err_t error_flag = lteiot8_process(  );
    if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
    {
        return error_flag;
    }
    
    while ( ( 0 == strstr( app_buf, LTEIOT8_RSP_OK ) ) && 
            ( 0 == strstr( app_buf, LTEIOT8_RSP_ERROR ) ) )
    {
        error_flag = lteiot8_process(  );
        if ( ( error_flag != 0 ) && ( error_flag != -1 ) )
        {
            return error_flag;
        }
        
        timeout_cnt++;
        if ( timeout_cnt > timeout )
        {
            lteiot8_clear_app_buf(  );
            return LTEIOT8_ERROR_TIMEOUT;
        }
        
        Delay_ms ( 1 );
    }
    
    if ( 0 != strstr( app_buf, LTEIOT8_RSP_OK ) )
    {
        return LTEIOT8_OK;
    }
    else if ( 0 == strstr( app_buf, LTEIOT8_RSP_ERROR ) )
    {
        return LTEIOT8_ERROR_CMD;
    }
    else
    {
        return LTEIOT8_ERROR_UNKNOWN;
    }
}

static void lteiot8_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case LTEIOT8_OK:
        {
            lteiot8_log_app_buf( );
            break;
        }
        case LTEIOT8_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case LTEIOT8_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case LTEIOT8_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case LTEIOT8_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    lteiot8_clear_app_buf(  );
    Delay_ms ( 500 );
}

static void lteiot8_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 lteiot8_configure_for_connection( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Enable device LED
    #define GNSS_LEN "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_SQNLED, GNSS_LEN );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Set SIM APN
    lteiot8_set_sim_apn( &lteiot8, SIM_APN );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Automatic configure cops
    #define AUTOMATIC "0"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_COPS, AUTOMATIC );
    error_flag = lteiot8_rsp_check(); 
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Enable network registartion
    #define ENABLE_REG "2"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CEREG, ENABLE_REG );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    return func_error;
}

static err_t lteiot8_check_connection( void )
{
    #define CONNECTED "+CEREG: 1"
    lteiot8_process();
    if ( 0 != strstr(app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        lteiot8_process();
        lteiot8_log_app_buf( );
        lteiot8_clear_app_buf(  );
        return LTEIOT8_OK;
    }
    
    return LTEIOT8_ERROR;
}

static err_t lteiot8_check_connection_parameters( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Check if connected
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CGATT );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Check registration
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CEREG );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Check signal quality
    lteiot8_send_cmd( &lteiot8, LTEIOT8_CMD_CSQ );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    return func_error;
}

static err_t lteiot8_configure_for_meesages( void )
{
    err_t func_error = LTEIOT8_OK;
    
    //Set message mode
    #define PDU_MESSAGE_MODE "0"
    #define TXT_MESSAGE_MODE "1"
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CMGF, TXT_MESSAGE_MODE );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );
    
    //Set SMSC for SIM card
    lteiot8_send_cmd_with_parameter( &lteiot8, LTEIOT8_CMD_CSCA, SMSC_ADDRESS_CSCA );
    error_flag = lteiot8_rsp_check();
    func_error |= error_flag;
    lteiot8_error_check( error_flag );

    return func_error;
}

static err_t lteiot8_send_meesage( void )
{
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    
    //Sendind message  
    lteiot8_send_cmd_check( &lteiot8, LTEIOT8_CMD_CMGF );
    error_flag = lteiot8_rsp_check();
    
    if ( 0 != strstr( app_buf, CMGF_PDU ) )
    {
        lteiot8_clear_app_buf(  );
        lteiot8_send_sms_pdu( &lteiot8, SMSC_ADDRESS_PDU, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot8_rsp_check();
        lteiot8_error_check( error_flag );
    }
    else if ( 0 != strstr( app_buf, CMGF_TXT ) )
    {
        lteiot8_clear_app_buf(  );
        lteiot8_send_text_message( &lteiot8, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot8_rsp_check();
        lteiot8_error_check( error_flag );
    }
    
    return error_flag;
}


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