高级
30 分钟

使用 LEXI-R520 和 STM32F031K6 实现低功耗 LTE-M 和 NB-IoT 连接

集成蜂窝调制解调器和 A-GPS 技术的多频段 LTE-M/NB-IoT 解决方案

LTE IoT 15 Click with Nucleo 32 with STM32F031K6 MCU

已发布 3月 27, 2025

点击板

LTE IoT 15 Click

开发板

Nucleo 32 with STM32F031K6 MCU

编译器

NECTO Studio

微控制器单元

STM32F031K6

低功耗 LTE-M 和 NB-IoT 连接,适用于全球物联网应用,如资产追踪、智能计量和远程监控

A

A

硬件概览

它是如何工作的?

LTE IoT 15 Click 基于 LEXI-R520,这是 Trasna 推出的多频段 LTE-M/NB-IoT 模块。该模块采用 UBX-R52 芯片组,结合蜂窝连接和辅助 GPS,支持数据传输和位置追踪。LEXI-R520 经过优化,支持超低功耗运行,并提供深度睡眠模式(PSM 和 eDRX),非常适用于电池供电的物联网应用。其软件可配置的多频段设计确保全球网络兼容性,而 3GPP Release 14 功能使其即使在地下室或地下区域(使用 NB2 时)也能保持扩展覆盖。该模块的一个重要优势是支持Trasna uFOTA 系统,通过 LwM2M 协议进行高效、轻量级的 OTA(空中固件更新)。它支持多个 LTE 频段(1/2/3/4/5/8/12/13/18/19/20/25/26/28/66/71
/85),具有 +23dBm 的传输功率,数据速率可达 1200 kbit/s(上行)和 588 kbit/s(下行)。专为紧凑型物联网设备设计,该 Click 板适用于资产追踪、可穿戴设备、智能计量、远程监控和互联医疗。凭借集成的 IP 协议栈和多种接口选项,它支持低到中等数

据吞吐量,并确保长电池寿命。LEXI-R520 与主机 MCU 之间的通信通过 UART 接口进行,使用标准 UART RX 和 TX 引脚以及硬件流控引脚(CTS/RTS/RI - Clear to Send/Ready to Send/Ring Indicator)以确保高效数据传输。默认通信速率为 115200bps,支持通过 AT 指令进行无缝数据交换。该 Click 板还包括一个 USB Type C 端口,用于供电和数据传输,符合 USB 2.0 规范,主要用于诊断目的。LTE IoT 15 Click 还配备多个增强功能,提高其可用性和控制能力。PWR 按钮允许用户手动开启或关闭模块,RESET 按钮提供快速复位模块的选项。这些功能还可通过 mikroBUS™ PWR 和 RST 引脚进行数字控制,提供更大的灵活性。此外,该板具有专用测试点(TP2/TP3),用于更轻松的调试和测试,一个未焊接的 3 引脚可配置 GPIO 接口(LEXI-R520 模块),以及两个 LED 状态指示灯。红色 NET LED 指示模块的当前网络状态,例如已注册、未注册或数据

传输中。如果 LED 完全关闭,表示设备已关闭或处于省电模式。黄色 STAT LED 指示模块的工作状态,例如关机、深度睡眠、空闲、活跃或已连接模式。该板配备 SMA 连接器,可连接 MIKROE 提供的 GSM/GPRS 天线,确保高效的连接性能。然而,目前该 Click 板不支持 GNSS 功能,因此相关的 GNSS 组件未焊接。板上带有一个 GNSS ANT 跳线,允许选择 3.3V 或 5V 供电 GNSS 天线,但由于 GNSS 功能未启用,此功能不可用。此外,该板包含一个 micro SIM 卡槽,兼容 1.8V 和 3.0V uSIM 卡,用户可根据具体应用选择合适的运营商。该 Click 板支持 3.3V 和 5V 逻辑电压,可通过 VCC SEL 跳线进行选择。由于 LEXI-R520 模块工作电压为 3.8V,该板还集成了 TXB0106 逻辑电平转换器,确保信号电平的正确转换,使 3.3V 和 5V 兼容 MCU 都能正常通信。此外,该 Click 板配备了开发库,包含易于使用的函数和示例代码,可作为进一步开发的参考。

LTE IoT 15 Click hardware overview image

功能概述

开发板

Nucleo 32开发板搭载STM32F031K6 MCU,提供了一种经济且灵活的平台,适用于使用32引脚封装的STM32微控制器进行实验。该开发板具有Arduino™ Nano连接性,便于通过专用扩展板进行功能扩展,并且支持mbed,使其能够无缝集成在线资源。板载集成

ST-LINK/V2-1调试器/编程器,支持通过USB重新枚举,提供三种接口:虚拟串口(Virtual Com port)、大容量存储和调试端口。该开发板的电源供应灵活,可通过USB VBUS或外部电源供电。此外,还配备了三个LED指示灯(LD1用于USB通信,LD2用于电源

指示,LD3为用户可控LED)和一个复位按钮。STM32 Nucleo-32开发板支持多种集成开发环境(IDEs),如IAR™、Keil®和基于GCC的IDE(如AC6 SW4STM32),使其成为开发人员的多功能工具。

Nucleo 32 with STM32F031K6 MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M0

MCU 内存 (KB)

32

硅供应商

STMicroelectronics

引脚数

32

RAM (字节)

4096

你完善了我!

配件

Click Shield for Nucleo-32是扩展您的开发板功能的理想选择,专为STM32 Nucleo-32引脚布局设计。Click Shield for Nucleo-32提供了两个mikroBUS™插座,可以添加来自我们不断增长的Click板™系列中的任何功能。从传感器和WiFi收发器到电机控制和音频放大器,我们应有尽有。Click Shield for Nucleo-32与STM32 Nucleo-32开发板兼容,为用户提供了一种经济且灵活的方式,使用任何STM32微控制器快速创建原型,并尝试各种性能、功耗和功能的组合。STM32 Nucleo-32开发板无需任何独立的探针,因为它集成了ST-LINK/V2-1调试器/编程器,并随附STM32全面的软件HAL库和各种打包的软件示例。这个开发平台为用户提供了一种简便且通用的方式,将STM32 Nucleo-32兼容开发板与他们喜欢的Click板™结合,应用于即将开展的项目中。

Click Shield for Nucleo-32 accessories 1 image

这款多频段 LTE 橡胶天线具有可调角度,是所有基于 3G/4G LTE 的 Click boards™ 的理想选择,也适用于其他需要在全球主要蜂窝频段上获得卓越吞吐量的设备。天线配备 SMA 公头连接器,可以直接安装在 Click board™ 或 SMA 母头模块连接器上。天线位置可按 45° 增量调节(0°/45°/90°),提供灵活的安装选项,以实现最佳信号接收和设备性能。

LTE IoT 15 Click accessories 1 image

使用的MCU引脚

mikroBUS™映射器

Module Power-ON
PA0
AN
Reset / ID SEL
PA11
RST
UART RTS / ID COMM
PA4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
PA8
PWM
UART CTS
PA12
INT
UART TX
PA10
TX
UART RX
PA9
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LTE IoT 15 Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-144 front image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 32 with STM32F031K6 MCU作为您的开发板开始。

Click Shield for Nucleo-144 front image hardware assembly
Nucleo 144 with STM32L4A6ZG MCU front image hardware assembly
Stepper 22 Click front image hardware assembly
Prog-cut hardware assembly
Stepper 22 Click complete accessories setup image hardware assembly
Board mapper by product8 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
STM32 M4 Clicker HA MCU/Select 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

软件支持

库描述

LTE IoT 15 Click 演示应用程序使用 NECTO Studio开发,确保与 mikroSDK 的开源库和工具兼容。该演示设计为即插即用,可与所有具有 mikroBUS™ 插座的 开发板、入门板和 mikromedia 板完全兼容,用于快速实现和测试。

示例描述
应用示例展示了设备连接到网络并使用标准 "AT" 指令发送 SMS 或 TCP/UDP 消息的能力。

关键功能:

  • lteiot15_cfg_setup - 此函数初始化 Click 配置结构为初始值。

  • lteiot15_init - 此函数初始化该 Click 板所需的所有引脚和外设。

  • lteiot15_set_sim_apn - 此函数设置 SIM 卡的 APN。

  • lteiot15_send_sms_text - 此函数向指定电话号码发送短信。

  • lteiot15_cmd_set - 此函数为 Click 模块的指定指令设置值

应用初始化
初始化驱动程序和日志记录器。

应用任务
应用任务分为多个阶段:

  • LTEIOT15_POWER_UP: 启动设备,执行设备出厂重置并读取系统信息。

  • LTEIOT15_CONFIG_CONNECTION: 配置设备以连接到网络。

  • LTEIOT15_CHECK_CONNECTION: 通过 CEREG 指令等待网络注册完成,并检查信号质量报告。

  • LTEIOT15_CONFIG_EXAMPLE: 配置设备以执行选定的示例任务。

  • LTEIOT15_EXAMPLE: 根据所选演示示例,发送 SMS 消息(PDU 或 TXT 模式)或 TCP/UDP 消息。默认选择 TCP/UDP 示例。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief LTE IoT 15 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 and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - LTEIOT15_POWER_UP:
 * Powers up the device, performs a device factory reset and reads system information.
 *
 *  - LTEIOT15_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - LTEIOT15_CHECK_CONNECTION:
 * Waits for the network registration indicated via CEREG command and then checks the signal quality report.
 *
 *  - LTEIOT15_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - LTEIOT15_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 lteiot15_clear_app_buf ( void )
 * - static void lteiot15_log_app_buf ( void )
 * - static err_t lteiot15_process ( lteiot15_t *ctx )
 * - static err_t lteiot15_read_response ( lteiot15_t *ctx, uint8_t *rsp )
 * - static err_t lteiot15_power_up ( lteiot15_t *ctx )
 * - static err_t lteiot15_config_connection ( lteiot15_t *ctx )
 * - static err_t lteiot15_check_connection ( lteiot15_t *ctx )
 * - static err_t lteiot15_config_example ( lteiot15_t *ctx )
 * - static err_t lteiot15_example ( lteiot15_t *ctx )
 *
 * @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.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lteiot15.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                             "internet"      // Set valid SIM APN

// SMS example parameters
#define SIM_SMSC                            ""              // Set valid SMS Service Center Address - only in SMS PDU mode
#define PHONE_NUMBER                        ""              // 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                     "LTE IoT 15 Click board - demo example."

// Application buffer size
#define APP_BUFFER_SIZE                     256
#define PROCESS_BUFFER_SIZE                 256

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    LTEIOT15_POWER_UP = 1,
    LTEIOT15_CONFIG_CONNECTION,
    LTEIOT15_CHECK_CONNECTION,
    LTEIOT15_CONFIG_EXAMPLE,
    LTEIOT15_EXAMPLE

} lteiot15_app_state_t;

/**
 * @brief Application example variables.
 * @details Variables used in application example.
 */
static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static lteiot15_app_state_t app_state = LTEIOT15_POWER_UP;

static lteiot15_t lteiot15;
static log_t logger;

/**
 * @brief LTE IoT 15 clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void lteiot15_clear_app_buf ( void );

/**
 * @brief LTE IoT 15 log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void lteiot15_log_app_buf ( void );

/**
 * @brief LTE IoT 15 data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_process ( lteiot15_t *ctx );

/**
 * @brief LTE IoT 15 read response function.
 * @details This function waits for a response message, reads and displays it on the USB UART.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @param[in] rsp  Expected response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_read_response ( lteiot15_t *ctx, uint8_t *rsp );

/**
 * @brief LTE IoT 15 power up function.
 * @details This function powers up the device, performs a factory reset and reads system information.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_power_up ( lteiot15_t *ctx );

/**
 * @brief LTE IoT 15 config connection function.
 * @details This function configures and enables connection to the specified network.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_config_connection ( lteiot15_t *ctx );

/**
 * @brief LTE IoT 15 check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_check_connection ( lteiot15_t *ctx );

/**
 * @brief LTE IoT 15 config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_config_example ( lteiot15_t *ctx );

/**
 * @brief LTE IoT 15 example function.
 * @details This function executes SMS or TCP/UDP depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #lteiot15_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot15_example ( lteiot15_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot15_cfg_t lteiot15_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.
    lteiot15_cfg_setup( &lteiot15_cfg );
    LTEIOT15_MAP_MIKROBUS( lteiot15_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lteiot15_init( &lteiot15, &lteiot15_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

    app_state = LTEIOT15_POWER_UP;
    log_printf( &logger, ">>> APP STATE - POWER UP <<<\r\n\n" );
}

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LTEIOT15_POWER_UP:
        {
            if ( LTEIOT15_OK == lteiot15_power_up( &lteiot15 ) )
            {
                app_state = LTEIOT15_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT15_CONFIG_CONNECTION:
        {
            if ( LTEIOT15_OK == lteiot15_config_connection( &lteiot15 ) )
            {
                app_state = LTEIOT15_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT15_CHECK_CONNECTION:
        {
            if ( LTEIOT15_OK == lteiot15_check_connection( &lteiot15 ) )
            {
                app_state = LTEIOT15_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT15_CONFIG_EXAMPLE:
        {
            if ( LTEIOT15_OK == lteiot15_config_example( &lteiot15 ) )
            {
                app_state = LTEIOT15_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT15_EXAMPLE:
        {
            lteiot15_example( &lteiot15 );
            break;
        }
        default:
        {
            log_error( &logger, " APP 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 lteiot15_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void lteiot15_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 lteiot15_process ( lteiot15_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = lteiot15_generic_read( ctx, rx_buf, PROCESS_BUFFER_SIZE );
    if ( ( rx_size > 0 ) && ( rx_size <= APP_BUFFER_SIZE ) ) 
    {
        if ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) 
        {
            overflow_bytes = ( app_buf_len + rx_size ) - APP_BUFFER_SIZE;
            app_buf_len = APP_BUFFER_SIZE - rx_size;
            memmove ( app_buf, &app_buf[ overflow_bytes ], app_buf_len );
            memset ( &app_buf[ app_buf_len ], 0, overflow_bytes );
        }
        for ( rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ app_buf_len++ ] = rx_buf[ rx_cnt ];
            }
        }
        return LTEIOT15_OK;
    }
    return LTEIOT15_ERROR;
}

static err_t lteiot15_read_response ( lteiot15_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS    120000
    uint32_t timeout_cnt = 0;
    lteiot15_clear_app_buf ( );
    lteiot15_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LTEIOT15_RSP_ERROR ) ) )
    {
        lteiot15_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            lteiot15_log_app_buf( );
            lteiot15_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return LTEIOT15_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    lteiot15_process( ctx );
    lteiot15_log_app_buf( );
    if ( strstr( app_buf, rsp ) )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        return LTEIOT15_OK;
    }
    return LTEIOT15_ERROR_CMD;
}

static err_t lteiot15_power_up ( lteiot15_t *ctx )
{
    err_t error_flag = LTEIOT15_OK;

    uint8_t power_state = LTEIOT15_POWER_STATE_OFF;
    for ( ; ; )
    {
        lteiot15_process( ctx );
        lteiot15_log_app_buf ( );
        lteiot15_clear_app_buf ( );
        // Wake up UART interface
        lteiot15_cmd_run( ctx, LTEIOT15_CMD_AT );

        log_printf( &logger, ">>> Check communication.\r\n" );
        lteiot15_cmd_run( ctx, LTEIOT15_CMD_AT );
        if ( ( ( LTEIOT15_OK == lteiot15_process( ctx ) ) && strstr( app_buf, LTEIOT15_RSP_OK ) ) )
        {
            power_state = LTEIOT15_POWER_STATE_ON;
            break;
        }
        else if ( LTEIOT15_POWER_STATE_OFF == power_state )
        {
            power_state = LTEIOT15_POWER_STATE_ON;
            log_printf( &logger, ">>> Power up device.\r\n" );
            lteiot15_set_power_state ( ctx, LTEIOT15_POWER_STATE_ON );
        }
        else if ( LTEIOT15_POWER_STATE_ON == power_state )
        {
            power_state = LTEIOT15_POWER_STATE_OFF;
            log_printf( &logger, ">>> Power down device.\r\n" );
            lteiot15_set_power_state ( ctx, LTEIOT15_POWER_STATE_OFF );
        }
    }
    lteiot15_cmd_run( ctx, LTEIOT15_CMD_AT );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    
    log_printf( &logger, ">>> Factory reset.\r\n" );
    lteiot15_cmd_run( ctx, LTEIOT15_CMD_FACTORY_RESET );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    lteiot15_cmd_run( ctx, LTEIOT15_CMD_GET_SW_VERSION );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    lteiot15_cmd_run( ctx, LTEIOT15_CMD_GET_SERIAL_NUM );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    return error_flag;
}

static err_t lteiot15_config_connection ( lteiot15_t *ctx )
{
    err_t error_flag = LTEIOT15_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Configure network status LED.\r\n" );
    #define NETWORK_STATUS_LED "14,2"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_GPIO_CONFIG, NETWORK_STATUS_LED );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Configure module status LED.\r\n" );
    #define MODULE_STATUS_LED "15,10"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_GPIO_CONFIG, MODULE_STATUS_LED );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    lteiot15_set_sim_apn( &lteiot15, SIM_APN );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_SET_MODULE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_EPS_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    lteiot15_cmd_set( ctx, LTEIOT15_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
#endif
    return error_flag;
}

static err_t lteiot15_check_connection ( lteiot15_t *ctx )
{
    err_t error_flag = LTEIOT15_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    #define CONNECTED "+CEREG: 2,1"
    lteiot15_cmd_get ( &lteiot15, LTEIOT15_CMD_EPS_NETWORK_REGISTRATION );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        lteiot15_cmd_run ( &lteiot15, LTEIOT15_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    }
    else
    {
        error_flag = LTEIOT15_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t lteiot15_config_example ( lteiot15_t *ctx )
{
    err_t error_flag = LTEIOT15_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    lteiot15_cmd_set( &lteiot15, LTEIOT15_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    lteiot15_cmd_set( &lteiot15, LTEIOT15_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    lteiot15_cmd_set( &lteiot15, LTEIOT15_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
#endif
    return error_flag;
}

static err_t lteiot15_example ( lteiot15_t *ctx )
{
    err_t error_flag = LTEIOT15_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    uint8_t * __generic_ptr socket_num_buf = 0;
    uint8_t tcp_socket_num[ 2 ] = { 0 };
    uint8_t udp_socket_num[ 2 ] = { 0 };
    log_printf( &logger, ">>> Create TCP socket.\r\n" );
    #define TCP_PROTOCOL "6"
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CREATE_SOCKET, TCP_PROTOCOL );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    socket_num_buf = strstr( app_buf, LTEIOT15_URC_CREATE_SOCKET ) + strlen ( LTEIOT15_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        tcp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Create UDP socket.\r\n" );
    #define UDP_PROTOCOL "17"
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CREATE_SOCKET, UDP_PROTOCOL );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    socket_num_buf = strstr( app_buf, LTEIOT15_URC_CREATE_SOCKET ) + strlen ( LTEIOT15_URC_CREATE_SOCKET );
    if ( NULL != socket_num_buf )
    {
        udp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CONNECT_SOCKET, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    // Get message length
    uint8_t message_len_buf[ 10 ] = { 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 );

    log_printf( &logger, ">>> Write message to TCP connection.\r\n" );
    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, "\"" );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    lteiot15_cmd_set( &lteiot15, LTEIOT15_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );

    log_printf( &logger, ">>> Write message to UDP connection.\r\n" );
    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, "\"" );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_WRITE_SOCKET_DATA, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    lteiot15_cmd_set( &lteiot15, LTEIOT15_CMD_READ_SOCKET_DATA, cmd_buf );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CLOSE_SOCKET, tcp_socket_num );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    lteiot15_cmd_set ( &lteiot15, LTEIOT15_CMD_CLOSE_SOCKET, udp_socket_num );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    lteiot15_cmd_get( &lteiot15, LTEIOT15_CMD_SELECT_SMS_FORMAT );
    error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        lteiot15_send_sms_pdu( &lteiot15, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        lteiot15_send_sms_text ( &lteiot15, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot15_read_response( ctx, LTEIOT15_RSP_OK );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。