高级
30 分钟

使用 HL7812 和 ATmega328P,实现集成 GNSS 的远距离 LTE-M 与 NB-IoT 连接

多协议 LTE-M/NB-IoT + GNSS 解决方案,适用于可靠、低功耗的蜂窝物联网应用

LTE IoT 19 Click with Arduino UNO Rev3

已发布 10月 22, 2025

点击板

LTE IoT 19 Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328P

为工业级和关键任务型物联网设备增加安全、低功耗的蜂窝通信能力,并支持 2G 回退功能

A

A

硬件概览

它是如何工作的?

LTE IoT 19 Click 基于 Semtech 的 HL7812,这是一款面向工业及关键任务型物联网的全球低功耗广域网 (LPWA) 蜂窝通信模块,集成了 LTE-M、NB-IoT 以及 2G 回退功能。该模块具备行业领先的超低功耗、高发射功率和深度覆盖能力,非常适合远程或电池供电设备的部署,符合 3GPP Release 14 标准要求,确保长期现场设备的前瞻性连接能力。HL7812 同时支持 LTE Cat-M1、Cat-NB2 以及四频段 2G GPRS,可灵活地在不同无线接入技术 (RAT) 间手动或动态切换,确保即使在复杂环境下也能实现可靠通信。模块具备从边缘到云端的高等级安全特性,以及节能扩展功能,如省电模式 (PSM)、扩展不连续接收 (eDRX) 和释放辅助指示 (RAI),因此非常适用于资产追踪、智能抄表、智慧城市基础设施等需要高能效、稳健连接和长期运行的应用场景。HL7812 集成了先进的射频与协议功能,以最大化性能和灵活性。其在 850/900/1800/1900MHz 频段下支持高功率 2G 传输,并在 Cat-M1 与 Cat-NB2 模式下提供 Class 3 (23dBm) 发射功率,确保在小区边缘也能保持强连 

接。LTE Cat-M1 支持最高 1100kbit/s 上行速率与590kbit/s 下行速率,而 Cat-NB2 支持最高 158kbit/s 上行速率与 127kbit/s 下行速率,并支持 3GPP Rel.14 的特性,如 HARQ-ACK 打包、多重 HARQ 进程及控制面 CIoT 优化。模块还内置 GNSS(GPS 与 GLONASS)用于精准定位与追踪,同时支持 NIDD 通过 SCEF/SGi 隧道传输、空闲与连接态移动性,并可在带内、保护带或独立模式下工作。HL7812 与主控 MCU 之间通过 UART 接口进行通信,采用标准 UART RX 与 TX 引脚,并配备硬件流控引脚 (CTS/RTS/RI),以实现高效数据传输。模块默认通信速率为 115200bps,使用 AT 指令进行数据交互。该 Click 板还配备 USB Type-C 接口,用于供电与数据传输,符合 USB 2.0 规范。此外,板上集成多项辅助功能以提升易用性与控制性。RESET 按钮可快速重置模块,该功能也可通过 mikroBUS™ RST 引脚数字控制,实现更高灵活性。除主 UART 接口外,板上还提供专用的调试 UART (UART0),用于固件调试与升级;并配备辅助 UART (UART3),可灵活用于 AT 指

令通信与数据传输,为开发者提供更多控制与连接选项。该板设有两个 SMA 天线接口,分别用于 LTE 天线(如 LTE Flat Rotation Antenna)与 GNSS 天线(如 MIKROE 提供的 GPS 被动天线),以确保高效的连接性能。还配备一个 micro SIM 卡槽,兼容 1.8V 与 3V uSIM 卡,方便用户根据应用选择合适的运营商服务。同时设有一个蓝色 TX 活动指示 LED。板上还提供一组标记为 ADC 的测试点,可访问 HL7812 模块的 12 位分辨率模数转换通道(输入电压范围 0–1.8V),用于监测外部信号;另有 32K 与 26M 测试点,可输出两个数字时钟信号,用于定时或诊断用途。该 Click 板支持 3.3V 与 5V 逻辑电平,可通过 VCC SEL 跳线进行选择。由于 HL7812 工作电压为 4.07V 且通信电平为 1.8V,因此板上集成了 TXB0106 逻辑电平转换器,以确保信号精确转换与正常运行。这样,无论是 3.3V 还是 5V 的 MCU 都可正常使用通信接口。此外,该 Click 板配备了软件库,包含易于使用的函数与示例代码,可作为进一步开发的参考。

LTE IoT 19 Click hardware overview image

功能概述

开发板

Arduino UNO 是围绕 ATmega328P 芯片构建的多功能微控制器板。它为各种项目提供了广泛的连接选项,具有 14 个数字输入/输出引脚,其中六个支持 PWM 输出,以及六个模拟输入。其核心组件包括一个 16MHz 的陶瓷谐振器、一个 USB 连接器、一个电

源插孔、一个 ICSP 头和一个复位按钮,提供了为板 子供电和编程所需的一切。UNO 可以通过 USB 连接到计算机,也可以通过 AC-to-DC 适配器或电池供电。作为第一个 USB Arduino 板,它成为 Arduino 平台的基准,"Uno" 符号化其作为系列首款产品的地

位。这个名称选择,意为意大利语中的 "一",是为了 纪念 Arduino Software(IDE)1.0 的推出。最初与 Arduino Software(IDE)版本1.0 同时推出,Uno 自此成为后续 Arduino 发布的基础模型,体现了该平台的演进。

Arduino UNO Rev3 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

AVR

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

28

RAM (字节)

2048

你完善了我!

配件

Click Shield for Arduino UNO 具有两个专有的 mikroBUS™ 插座,使所有 Click board™ 设备能够轻松与 Arduino UNO 板进行接口连接。Arduino UNO 是一款基于 ATmega328P 的微控制器开发板,为用户提供了一种经济实惠且灵活的方式来测试新概念并构建基于 ATmega328P 微控制器的原型系统,结合了性能、功耗和功能的多种配置选择。Arduino UNO 具有 14 个数字输入/输出引脚(其中 6 个可用作 PWM 输出)、6 个模拟输入、16 MHz 陶瓷谐振器(CSTCE16M0V53-R0)、USB 接口、电源插座、ICSP 头和复位按钮。大多数 ATmega328P 微控制器的引脚都连接到开发板左右两侧的 IO 引脚,然后再连接到两个 mikroBUS™ 插座。这款 Click Shield 还配备了多个开关,可执行各种功能,例如选择 mikroBUS™ 插座上模拟信号的逻辑电平,以及选择 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换电压转换器使用任何 Click board™,无论 Click board™ 运行在 3.3V 还是 5V 逻辑电压电平。一旦将 Arduino UNO 板与 Click Shield for Arduino UNO 连接,用户即可访问数百种 Click board™,并兼容 3.3V 或 5V 逻辑电压电平的设备。

Click Shield for Arduino UNO accessories 1 image

使用的MCU引脚

mikroBUS™映射器

Module Wake-Up
PC0
AN
Reset / ID SEL
PD2
RST
UART RTS / ID COMM
PB2
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
PD6
PWM
UART CTS
PC3
INT
UART TX
PD0
TX
UART RX
PD1
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LTE IoT 19 Click Schematic schematic

一步一步来

项目组装

Click Shield for Arduino UNO front image hardware assembly

从选择您的开发板和Click板™开始。以Arduino UNO Rev3作为您的开发板开始。

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Charger 27 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
Arduino UNO 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

软件支持

库描述

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

示例描述
本示例演示了设备通过标准 “AT” 指令连接到网络并发送 SMS 或 TCP/UDP 消息,或从 GNSS 获取数据的功能。

关键功能:

  • lteiot19_cfg_setup - 初始化 Click 配置结构为初始值。

  • lteiot19_init - 初始化该 Click 板所需的所有引脚和外设。

  • lteiot19_set_sim_apn - 为 SIM 卡设置接入点名称 (APN)。

  • lteiot19_send_sms_text - 向指定电话号码发送短信。

  • lteiot19_cmd_run - 向 Click 模块发送指定命令。

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

应用任务
应用任务分为以下几个阶段:

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

  • LTEIOT19_CONFIG_CONNECTION - 配置设备以便能够连接到网络(仅用于 SMS 或 TCP/UDP 演示示例)。

  • LTEIOT19_CHECK_CONNECTION - 等待通过 CEREG 命令指示的网络注册,然后检查信号质量报告(仅用于 SMS 或 TCP/UDP 演示示例)。

  • LTEIOT19_CONFIG_EXAMPLE - 为所选示例配置设备。

  • LTEIOT19_EXAMPLE - 根据所选演示示例,发送短信(PDU 或 TXT 模式)、TCP/UDP 消息,或等待 GNSS 获取定位信息。默认选择 TCP/UDP 示例。

开源

代码示例

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

/*!
 * @file main.c
 * @brief LTE IoT 19 Click Example.
 *
 * # Description
 * Application example shows device capability of connecting to the network and
 * sending SMS or TCP/UDP messages or retrieving data from GNSS 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:
 *  - LTEIOT19_POWER_UP:
 * Powers up the device, performs a factory reset and reads system information.
 *
 *  - LTEIOT19_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network 
 * (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT19_CHECK_CONNECTION:
 * Waits for the network registration indicated via CEREG command and then checks 
 * the signal quality report (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT19_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - LTEIOT19_EXAMPLE:
 * Depending on the selected demo example, it sends an SMS message (in PDU or TXT mode)
 * or TCP/UDP message, or waits for the GPS fix to retrieve location info from GNSS.
 *
 * By default, the TCP/UDP example is selected.
 *
 * ## Additional Function
 * - static void lteiot19_clear_app_buf ( void )
 * - static void lteiot19_log_app_buf ( void )
 * - static err_t lteiot19_process ( lteiot19_t *ctx )
 * - static err_t lteiot19_read_response ( lteiot19_t *ctx, uint8_t *rsp, uint32_t max_rsp_time )
 * - static err_t lteiot19_power_up ( lteiot19_t *ctx )
 * - static err_t lteiot19_config_connection ( lteiot19_t *ctx )
 * - static err_t lteiot19_check_connection ( lteiot19_t *ctx )
 * - static err_t lteiot19_config_example ( lteiot19_t *ctx )
 * - static err_t lteiot19_example ( lteiot19_t *ctx )
 *
 * @note
 * In order for the examples to work (except GNSS example), 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 "lteiot19.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 EXAMPLE_GNSS                        2               // Example of retrieving location info from GNSS
#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                        ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

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

// Message content
#define MESSAGE_CONTENT                     "LTE IoT 19 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
{
    LTEIOT19_POWER_UP = 1,
    LTEIOT19_CONFIG_CONNECTION,
    LTEIOT19_CHECK_CONNECTION,
    LTEIOT19_CONFIG_EXAMPLE,
    LTEIOT19_EXAMPLE

} lteiot19_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 lteiot19_app_state_t app_state = LTEIOT19_POWER_UP;

static lteiot19_t lteiot19;
static log_t logger;

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

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

/**
 * @brief LTE IoT 19 data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #lteiot19_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 lteiot19_process ( lteiot19_t *ctx );

/**
 * @brief LTE IoT 19 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 #lteiot19_t object definition for detailed explanation.
 * @param[in] rsp  Expected response.
 * @param[in] max_rsp_time : Maximum response time in milliseconds.
 * @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 lteiot19_read_response ( lteiot19_t *ctx, uint8_t *rsp, uint32_t max_rsp_time );

/**
 * @brief LTE IoT 19 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 #lteiot19_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 lteiot19_power_up ( lteiot19_t *ctx );

/**
 * @brief LTE IoT 19 config connection function.
 * @details This function configures and enables connection to the specified network.
 * @param[in] ctx : Click context object.
 * See #lteiot19_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 lteiot19_config_connection ( lteiot19_t *ctx );

/**
 * @brief LTE IoT 19 check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #lteiot19_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 lteiot19_check_connection ( lteiot19_t *ctx );

/**
 * @brief LTE IoT 19 config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #lteiot19_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 lteiot19_config_example ( lteiot19_t *ctx );

/**
 * @brief LTE IoT 19 example function.
 * @details This function executes SMS, TCP/UDP, or GNSS example depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #lteiot19_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 lteiot19_example ( lteiot19_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot19_cfg_t lteiot19_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.
    lteiot19_cfg_setup( &lteiot19_cfg );
    LTEIOT19_MAP_MIKROBUS( lteiot19_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lteiot19_init( &lteiot19, &lteiot19_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LTEIOT19_POWER_UP:
        {
            if ( LTEIOT19_OK == lteiot19_power_up( &lteiot19 ) )
            {
                app_state = LTEIOT19_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT19_CONFIG_CONNECTION:
        {
            if ( LTEIOT19_OK == lteiot19_config_connection( &lteiot19 ) )
            {
                app_state = LTEIOT19_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT19_CHECK_CONNECTION:
        {
            if ( LTEIOT19_OK == lteiot19_check_connection( &lteiot19 ) )
            {
                app_state = LTEIOT19_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT19_CONFIG_EXAMPLE:
        {
            if ( LTEIOT19_OK == lteiot19_config_example( &lteiot19 ) )
            {
                app_state = LTEIOT19_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT19_EXAMPLE:
        {
            lteiot19_example( &lteiot19 );
            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 lteiot19_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void lteiot19_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 lteiot19_process ( lteiot19_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = lteiot19_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;
            for ( int32_t buf_cnt = 0; buf_cnt < overflow_bytes; buf_cnt++ )
            {
                log_printf( &logger, "%c", app_buf[ buf_cnt ] );
            }
            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 LTEIOT19_OK;
    }
    return LTEIOT19_ERROR;
}

static err_t lteiot19_read_response ( lteiot19_t *ctx, uint8_t *rsp, uint32_t max_rsp_time ) 
{
    uint32_t timeout_cnt = 0;
    lteiot19_clear_app_buf ( );
    lteiot19_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LTEIOT19_RSP_ERROR ) ) )
    {
        lteiot19_process( ctx );
        if ( timeout_cnt++ > max_rsp_time )
        {
            lteiot19_log_app_buf( );
            lteiot19_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            log_printf( &logger, "--------------------------------\r\n" );
            return LTEIOT19_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    lteiot19_process( ctx );
    lteiot19_log_app_buf( );
    log_printf( &logger, "--------------------------------\r\n" );
    if ( strstr( app_buf, rsp ) )
    {
        return LTEIOT19_OK;
    }
    return LTEIOT19_ERROR_CMD;
}

static err_t lteiot19_power_up ( lteiot19_t *ctx )
{
    err_t error_flag = LTEIOT19_ERROR;

    while ( LTEIOT19_OK != error_flag )
    {
        log_printf( &logger, ">>> Reset device.\r\n" );
        lteiot19_set_power_state ( ctx, LTEIOT19_POWER_STATE_RESET );

        log_printf( &logger, ">>> Check communication.\r\n" );
        lteiot19_cmd_run( ctx, LTEIOT19_CMD_AT );
        error_flag = lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    }

    log_printf( &logger, ">>> Get device model ID.\r\n" );
    lteiot19_cmd_run( ctx, LTEIOT19_CMD_GET_MODEL_ID );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    lteiot19_cmd_run( ctx, LTEIOT19_CMD_GET_SW_VERSION );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    lteiot19_cmd_run( ctx, LTEIOT19_CMD_GET_SERIAL_NUM );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );

    return error_flag;
}

static err_t lteiot19_config_connection ( lteiot19_t *ctx )
{
    err_t error_flag = LTEIOT19_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Set minimum functionality.\r\n" );
    #define MIN_FUNCTIONALITY "0"
    lteiot19_cmd_set( ctx, LTEIOT19_CMD_SET_UE_FUNCTIONALITY, MIN_FUNCTIONALITY );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CFUN );
    
    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    lteiot19_set_sim_apn( ctx, SIM_APN );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CGDCONT );

    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    lteiot19_cmd_set( ctx, LTEIOT19_CMD_SET_UE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CFUN );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    lteiot19_cmd_set( ctx, LTEIOT19_CMD_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
#endif
    return error_flag;
}

static err_t lteiot19_check_connection ( lteiot19_t *ctx )
{
    err_t error_flag = LTEIOT19_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    lteiot19_cmd_get ( &lteiot19, LTEIOT19_CMD_NETWORK_REGISTRATION );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    if ( strstr( app_buf, LTEIOT19_URC_NETWORK_REGISTERED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        lteiot19_cmd_run( &lteiot19, LTEIOT19_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    }
    else
    {
        error_flag = LTEIOT19_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t lteiot19_config_example ( lteiot19_t *ctx )
{
    err_t error_flag = LTEIOT19_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    lteiot19_cmd_set( &lteiot19, LTEIOT19_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    lteiot19_cmd_set( &lteiot19, LTEIOT19_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    log_printf( &logger, ">>> Set minimum functionality.\r\n" );
    #define MIN_FUNCTIONALITY "0"
    lteiot19_cmd_set( ctx, LTEIOT19_CMD_SET_UE_FUNCTIONALITY, MIN_FUNCTIONALITY );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CFUN );
    log_printf( &logger, ">>> Enable GNSS functionality.\r\n" );
    #define GNSS_AUTO_START "0"
    lteiot19_cmd_set( &lteiot19, LTEIOT19_CMD_GNSS_START, GNSS_AUTO_START );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
#endif
    return error_flag;
}

static err_t lteiot19_example ( lteiot19_t *ctx )
{
    err_t error_flag = LTEIOT19_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 200 ] = { 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, ">>> Open TCP socket.\r\n" );
    #define TCP_MODE_CLIENT "0"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_MODE_CLIENT );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_TCP_OPEN_SOCKET, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    socket_num_buf = strstr( app_buf, LTEIOT19_URC_TCP_SOCKET );
    if ( NULL != socket_num_buf )
    {
        socket_num_buf += strlen ( LTEIOT19_URC_TCP_SOCKET );
        tcp_socket_num[ 0 ] = *socket_num_buf;
    }
    
    log_printf( &logger, ">>> Open UDP socket.\r\n" );
    #define UDP_MODE_CLIENT "0"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_MODE_CLIENT );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_UDP_OPEN_SOCKET, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_URC_UDP_STATUS, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    socket_num_buf = strstr( app_buf, LTEIOT19_URC_UDP_SOCKET );
    if ( NULL != socket_num_buf )
    {
        socket_num_buf += strlen ( LTEIOT19_URC_UDP_SOCKET );
        udp_socket_num[ 0 ] = *socket_num_buf;
    }

    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_TCP_START_CONNECTION, tcp_socket_num );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_URC_TCP_STATUS, LTEIOT19_MAX_RSP_TIME_TCP_START );

    // 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 );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_TCP_SEND_DATA, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_CONNECT, LTEIOT19_MAX_RSP_TIME_TCP_DATA );
    lteiot19_generic_write( ctx, MESSAGE_CONTENT, message_len );
    Delay_ms ( 100 );
    lteiot19_generic_write( ctx, LTEIOT19_CMD_SWITCH_DATA_TO_CMD, strlen ( LTEIOT19_CMD_SWITCH_DATA_TO_CMD ) );
    Delay_ms ( 100 );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_URC_TCP_DATA, LTEIOT19_MAX_RSP_TIME_TCP_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    lteiot19_cmd_set( &lteiot19, LTEIOT19_CMD_TCP_RECEIVE_DATA, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_TCP_DATA );

    log_printf( &logger, ">>> Write message to 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 );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_UDP_SEND_DATA, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_CONNECT, LTEIOT19_MAX_RSP_TIME_UDP_DATA );
    lteiot19_generic_write( ctx, MESSAGE_CONTENT, message_len );
    Delay_ms ( 100 );
    lteiot19_generic_write( ctx, LTEIOT19_CMD_SWITCH_DATA_TO_CMD, strlen ( LTEIOT19_CMD_SWITCH_DATA_TO_CMD ) );
    Delay_ms ( 100 );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_URC_UDP_DATA, LTEIOT19_MAX_RSP_TIME_UDP_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 );
    lteiot19_cmd_set( &lteiot19, LTEIOT19_CMD_UDP_RECEIVE_DATA, cmd_buf );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_UDP_DATA );

    log_printf( &logger, ">>> Close TCP socket.\r\n" );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_TCP_CLOSE_SOCKET, tcp_socket_num );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_TCP_CLOSE );

    log_printf( &logger, ">>> Delete TCP socket.\r\n" );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_TCP_DELETE_SOCKET, tcp_socket_num );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Close UDP socket.\r\n" );
    lteiot19_cmd_set ( &lteiot19, LTEIOT19_CMD_UDP_CLOSE_SOCKET, udp_socket_num );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_UDP_CLOSE );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    lteiot19_cmd_get( &lteiot19, LTEIOT19_CMD_SELECT_SMS_FORMAT );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    if ( strstr( app_buf, LTEIOT19_URC_SMS_FORMAT_PDU ) )
    {
        // Send SMS in PDU mode
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        lteiot19_send_sms_pdu( &lteiot19, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CMGS );
    }
    else if ( strstr( app_buf, LTEIOT19_URC_SMS_FORMAT_TXT ) )
    {
        // Send SMS in TXT mode
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        lteiot19_send_sms_text ( &lteiot19, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_CMGS );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    lteiot19_cmd_get( &lteiot19, LTEIOT19_CMD_GNSS_REPORT );
    error_flag |= lteiot19_read_response( ctx, LTEIOT19_RSP_OK, LTEIOT19_MAX_RSP_TIME_DEFAULT );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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