中级
20 分钟

使用BG95M3LA-64-SGNS和PIC32MZ2048EFH100实现低功耗LTE和NB-IoT连接及精确位置跟踪

具有LTE Cat M1/Cat NB2/EGPRS和集成GNSS的多模模块解决方案

LTE IoT 12 Click with Flip&Click PIC32MZ

已发布 10月 23, 2024

点击板

LTE IoT 12 Click

开发板

Flip&Click PIC32MZ

编译器

NECTO Studio

微控制器单元

PIC32MZ2048EFH100

可靠的低功耗LTE和NB-IoT连接,配备精准的GNSS跟踪功能,非常适合资产跟踪、智能计量和可穿戴技术

A

A

硬件概览

它是如何工作的?

LTE IoT 12 Click 基于 BG95-M3 (BG95M3LA-64-SGNS),这是 Quectel 的多模式数据专用模块,支持 LTE Cat M1、LTE Cat NB2 和 EGPRS 技术,并集成了 GNSS(GPS、GLONASS、BDS、Galileo、QZSS)用于精确的位置追踪。BG95-M3 模块支持多种 LTE 频段,包括 LTE Cat M1(B1/B2/B3/B4/B5/B8/B12/B13/B18/B19/
B20/B25/B26/B27/B28/B66/B85)和 LTE Cat NB2(B1/B2/B3/B4/B5/B8/B12/B13/B18/B19/
B20/B25/B28/B66/B71/B85),提供强大的覆盖范围和可靠的通信。此外,该模块还支持 EGPRS 在 GSM850、EGSM900、DCS1800 和 PCS1900 频段上运行,在 LTE 覆盖不足的地方提供与 2G 网络的向后兼容性。其符合 3GPP Release 14 标准,确保了增强的连接性和数据速率,在 LTE Cat M1 上下行速度分别可达 588kbps 和 1119kbps。该模块的超低功耗使其非常适合电池供电的物联网应用。通过集成的 RAM、闪存以及运行 ThreadX 的 ARM Cortex A7 处理器,它相较于早期型号,在 PSM(省电模式)泄漏上减少了 70%,在 eDRX(扩展不连续接收)电流消耗上减少了 85%。ARM Cortex A7 的 TrustZone 引擎还确保了强大的硬件安全性,支持在设备上直接运行安全应用。BG95-M3 支持通过 Quectel 扩展 AT 命

令控制的标准互联网服务协议,如 TCP、UDP 和 PPP,便于集成到各种应用中。丰富的功能集、行业标准接口和大量的功能使该模块适用于各种 M2M 应用,例如无线 POS 终端、智能计量、跟踪、可穿戴设备等。BG95-M3 和主 MCU 之间的通信通过 UART 接口实现,使用标准 UART RX 和 TX 引脚,以及硬件流控制引脚(CTS/RTS/RI - 清除发送/准备发送/振铃指示器)进行高效数据传输。模块默认的通信速度为 115200bps,允许通过 AT 命令实现无缝的数据交换。此外,该 Click board™ 还包含多个额外功能,增强了其可用性和控制性。mikroBUS™ 插座的 PWR 引脚允许用户轻松地开关模块电源,而 RESET 按钮则为模块提供了快速重置的方式。板上还配有两个视觉指示器,用于提供实时状态更新。蓝色的 NET LED 提供网络活动的反馈:当模块搜索网络时会慢速闪烁,数据传输时则快速闪烁。绿色的 STA LED 指示模块的电源状态:当模块关闭时熄灭,模块通电时点亮。模块的状态也可以通过 mikroBUS™ 插座上的 STA 引脚获取。该 Click board™ 还提供了多个额外功能,增强其功能性。它包括符合 USB 2.0 规范的 USB 接口,支持低速(1.5 Mbps)、全速(12 Mbps)和高速(480 Mbps)操作。此 USB 接口可用于 AT 命令通信、GNSS NMEA 语句输出、软件调试

和固件升级。此外,板上还配有一个 BOOT SEL 跳线,当设置为 EN 位置时,强制模块从 USB 接口启动以进行固件升级,默认位置为 DIS(禁用)。此外,该板还包括两个未焊接的 UART 头,提供扩展功能。第一个头 DBG 用于软件调试和日志输出,第二个头 GNSS 专用于 GNSS 数据输出和 NMEA 语句流。还有一个带有 AC 和 AR 引脚的未焊接头。AC 引脚控制模块的某些操作模式,例如飞行模式,该模式关闭所有 RF 功能和相关的 AT 命令。AR 引脚用作处理器就绪指示器,指示主 MCU 的睡眠状态。该板配有两个 SMA 连接器,用于连接 MIKROE 提供的 LTE 和 GNSS 天线,例如 LTE 平板旋转天线和主动 GPS 天线,以提供高效的连接选项。此外,该板配有一个支持 1.8V 和 3.0V uSIM 卡的微型 SIM 卡座,确保与各种蜂窝网络的兼容性,并允许用户根据其特定用例选择最合适的服务提供商。此 Click board™ 可以通过选择 VCC SEL 跳线操作在 3.3V 和 5V 逻辑电压电平下。由于 BG95-M3 模块在 5V 下工作,TXB0106 逻辑电平转换器用于确保正常操作和精确的信号电平转换。这样,3.3V 和 5V 兼容的 MCU 都可以正确使用通信线。此外,该 Click board™ 配备了包含易于使用的功能和示例代码的库,可用作进一步开发的参考。

LTE IoT 12 Click hardware overview image

功能概述

开发板

Flip&Click PIC32MZ 是一款紧凑型开发板,设计为一套完整的解决方案,它将 Click 板™的灵活性带给您喜爱的微控制器,使其成为实现您想法的完美入门套件。它配备了一款板载 32 位 PIC32MZ 微控制器,Microchip 的 PIC32MZ2048EFH100,四个 mikroBUS™ 插槽用于 Click 板™连接,两个 USB 连接器,LED 指示灯,按钮,调试器/程序员连接器,以及两个与 Arduino-UNO 引脚兼容的头部。得益于创

新的制造技术,它允许您快速构建具有独特功能和特性的小工具。Flip&Click PIC32MZ 开发套件的每个部分都包含了使同一板块运行最高效的必要组件。此外,还可以选择 Flip&Click PIC32MZ 的编程方式,使用 chipKIT 引导程序(Arduino 风格的开发环境)或我们的 USB HID 引导程序,使用 mikroC、mikroBasic 和 mikroPascal for PIC32。该套件包括一个通过 USB 类型-C(USB-C)连接器的干净且调

节过的电源供应模块。所有 mikroBUS™ 本身支持的 通信方法都在这块板上,包括已经建立良好的 mikroBUS™ 插槽、用户可配置的按钮和 LED 指示灯。Flip&Click PIC32MZ 开发套件允许您在几分钟内创建新的应用程序。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作的许多方面。

Flip&Click PIC32MZ double image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC32

MCU 内存 (KB)

2048

硅供应商

Microchip

引脚数

100

RAM (字节)

524288

你完善了我!

配件

LTE Flat Rotation Antenna 是增强 3G/4G LTE 设备性能的多功能选择。凭借 700-2700MHz 的宽频率范围,它确保在全球主要蜂窝频段上的最佳连接。该平板天线采用 SMA 公头连接器,便于直接连接到设备或 SMA 模块连接器。其亮点之一是可调角度,可按 45⁰ 增量(0⁰/45⁰/90⁰)设置,允许您微调天线的方向以获得最佳信号接收。具有 50Ω 阻抗和 <2.0:1 的电压驻波比 (VSWR),此天线确保可靠高效的连接。其 5dB 增益、垂直极化和全向辐射图形增强了信号强度,适用于各种应用。天线长度为 196mm,宽度为 38mm,提供紧凑但有效的解决方案来改善您的连接。最大输入功率为 50W,能够满足各种设备的需求。

LTE IoT 12 Click accessories 1 image

主动 GPS 天线旨在增强您的 GPS 和 GNSS Click 板™ 的性能。这款外置天线结构坚固,适用于各种天气条件。凭借 1575.42MHz 的频率范围和 50Ohm 的阻抗,它确保了可靠的信号接收。天线在较宽的角度范围内提供大于 -4dBic 的增益,确保超过 75% 的覆盖率。± 5MHz 的带宽进一步保证了精确的数据采集。天线采用右旋圆极化 (RHCP),提供稳定的信号接收。其紧凑的尺寸为 48.5×39×15mm,配有 2 米长的电缆,安装方便。磁性天线类型与 SMA 公连接器确保了安全便捷的连接。如果您需要为定位设备提供可靠的外置天线,我们的主动 GPS 天线是完美的解决方案。

LTE IoT 12 Click accessories 2 image

使用的MCU引脚

mikroBUS™映射器

Status Indicator
RB11
AN
Module Power Control
RE2
RST
UART RTS / ID COMM
RA0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Ring Indicator
RC14
PWM
UART CTS
RD9
INT
UART TX
RE3
TX
UART RX
RG9
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LTE IoT 12 Click Schematic schematic

一步一步来

项目组装

Flip&Click PIC32MZ front image hardware assembly

从选择您的开发板和Click板™开始。以Flip&Click PIC32MZ作为您的开发板开始。

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

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

该库包含 LTE IoT 12 Click 驱动程序的 API。

关键功能:

  • lteiot12_set_sim_apn - 此函数用于设置SIM卡的APN。

  • lteiot12_send_sms_text - 此函数用于向电话号码发送短信。

  • lteiot12_cmd_run - 此函数用于向Click模块发送指定命令。

开源

代码示例

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

/*!
 * @file main.c
 * @brief LTE IoT 12 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:
 *  - LTEIOT12_POWER_UP:
 * Powers up the device, performs a factory reset and reads system information.
 *
 *  - LTEIOT12_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT12_CHECK_CONNECTION:
 * Waits for the network registration indicated via CREG command and then checks the signal quality report 
 * (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT12_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - LTEIOT12_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 lteiot12_clear_app_buf ( void )
 * - static void lteiot12_log_app_buf ( void )
 * - static err_t lteiot12_process ( lteiot12_t *ctx )
 * - static err_t lteiot12_read_response ( lteiot12_t *ctx, uint8_t *rsp )
 * - static err_t lteiot12_power_up ( lteiot12_t *ctx )
 * - static err_t lteiot12_config_connection ( lteiot12_t *ctx )
 * - static err_t lteiot12_check_connection ( lteiot12_t *ctx )
 * - static err_t lteiot12_config_example ( lteiot12_t *ctx )
 * - static err_t lteiot12_example ( lteiot12_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 "lteiot12.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                             "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 12 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
{
    LTEIOT12_POWER_UP = 1,
    LTEIOT12_CONFIG_CONNECTION,
    LTEIOT12_CHECK_CONNECTION,
    LTEIOT12_CONFIG_EXAMPLE,
    LTEIOT12_EXAMPLE

} lteiot12_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 lteiot12_app_state_t app_state = LTEIOT12_POWER_UP;

static lteiot12_t lteiot12;
static log_t logger;

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

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

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

/**
 * @brief LTE IoT 12 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 #lteiot12_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.
 *         @li @c -4 - Unknown error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lteiot12_read_response ( lteiot12_t *ctx, uint8_t *rsp );

/**
 * @brief LTE IoT 12 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 #lteiot12_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 lteiot12_power_up ( lteiot12_t *ctx );

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

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

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

/**
 * @brief LTE IoT 12 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 #lteiot12_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 lteiot12_example ( lteiot12_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot12_cfg_t lteiot12_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.
    lteiot12_cfg_setup( &lteiot12_cfg );
    LTEIOT12_MAP_MIKROBUS( lteiot12_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lteiot12_init( &lteiot12, &lteiot12_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LTEIOT12_POWER_UP:
        {
            if ( LTEIOT12_OK == lteiot12_power_up( &lteiot12 ) )
            {
                app_state = LTEIOT12_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT12_CONFIG_CONNECTION:
        {
            if ( LTEIOT12_OK == lteiot12_config_connection( &lteiot12 ) )
            {
                app_state = LTEIOT12_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT12_CHECK_CONNECTION:
        {
            if ( LTEIOT12_OK == lteiot12_check_connection( &lteiot12 ) )
            {
                app_state = LTEIOT12_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT12_CONFIG_EXAMPLE:
        {
            if ( LTEIOT12_OK == lteiot12_config_example( &lteiot12 ) )
            {
                app_state = LTEIOT12_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LTEIOT12_EXAMPLE:
        {
            lteiot12_example( &lteiot12 );
            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 lteiot12_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void lteiot12_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 lteiot12_process ( lteiot12_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = lteiot12_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 LTEIOT12_OK;
    }
    return LTEIOT12_ERROR;
}

static err_t lteiot12_read_response ( lteiot12_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS    120000
    uint32_t timeout_cnt = 0;
    lteiot12_clear_app_buf ( );
    lteiot12_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LTEIOT12_RSP_ERROR ) ) )
    {
        lteiot12_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            lteiot12_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return LTEIOT12_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    lteiot12_process( ctx );
    if ( strstr( app_buf, rsp ) )
    {
        lteiot12_log_app_buf( );
        log_printf( &logger, "--------------------------------\r\n" );
        return LTEIOT12_OK;
    }
    else if ( strstr( app_buf, LTEIOT12_RSP_ERROR ) )
    {
        log_error( &logger, " CMD!" );
        return LTEIOT12_ERROR_CMD;
    }
    log_error( &logger, " Unknown!" );
    return LTEIOT12_ERROR_UNKNOWN;
}

static err_t lteiot12_power_up ( lteiot12_t *ctx )
{
    err_t error_flag = LTEIOT12_OK;

    uint8_t power_state = LTEIOT12_POWER_STATE_OFF;
    for ( ; ; )
    {
        lteiot12_process( ctx );
        lteiot12_log_app_buf ( );
        lteiot12_clear_app_buf ( );
        // Wake up UART interface
        lteiot12_cmd_run( ctx, LTEIOT12_CMD_AT );

        log_printf( &logger, ">>> Check communication.\r\n" );
        lteiot12_cmd_run( ctx, LTEIOT12_CMD_AT );
        if ( ( ( LTEIOT12_OK == lteiot12_process( ctx ) ) && strstr( app_buf, LTEIOT12_RSP_OK ) ) )
        {
            power_state = LTEIOT12_POWER_STATE_ON;
            break;
        }
        else if ( LTEIOT12_POWER_STATE_OFF == power_state )
        {
            power_state = LTEIOT12_POWER_STATE_ON;
            log_printf( &logger, ">>> Power up device.\r\n" );
            lteiot12_set_power_state ( ctx, LTEIOT12_POWER_STATE_ON );
        }
        else if ( LTEIOT12_POWER_STATE_ON == power_state )
        {
            power_state = LTEIOT12_POWER_STATE_OFF;
            log_printf( &logger, ">>> Power down device.\r\n" );
            lteiot12_set_power_state ( ctx, LTEIOT12_POWER_STATE_OFF );
        }
    }
    lteiot12_cmd_run( ctx, LTEIOT12_CMD_AT );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Factory reset.\r\n" );
    lteiot12_cmd_run( ctx, LTEIOT12_CMD_FACTORY_RESET );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Disable power saving mode.\r\n" );
    #define DISABLE_PSM "0"
    lteiot12_cmd_set( ctx, LTEIOT12_CMD_PSM_SETTING, DISABLE_PSM );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    
    log_printf( &logger, ">>> Get device model ID.\r\n" );
    lteiot12_cmd_run( ctx, LTEIOT12_CMD_GET_MODEL_ID );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Get device software version ID.\r\n" );
    lteiot12_cmd_run( ctx, LTEIOT12_CMD_GET_SW_VERSION );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    lteiot12_cmd_run( ctx, LTEIOT12_CMD_GET_SERIAL_NUM );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    return error_flag;
}

static err_t lteiot12_config_connection ( lteiot12_t *ctx )
{
    err_t error_flag = LTEIOT12_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    lteiot12_cmd_set( ctx, LTEIOT12_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    lteiot12_set_sim_apn( &lteiot12, SIM_APN );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    lteiot12_cmd_set( ctx, LTEIOT12_CMD_SET_UE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    lteiot12_cmd_set( ctx, LTEIOT12_CMD_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    lteiot12_cmd_set( ctx, LTEIOT12_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
#endif
    return error_flag;
}

static err_t lteiot12_check_connection ( lteiot12_t *ctx )
{
    err_t error_flag = LTEIOT12_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    #define CONNECTED "+CREG: 2,1"
    lteiot12_cmd_get ( &lteiot12, LTEIOT12_CMD_NETWORK_REGISTRATION );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        lteiot12_cmd_run( &lteiot12, LTEIOT12_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    }
    else
    {
        error_flag = LTEIOT12_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t lteiot12_config_example ( lteiot12_t *ctx )
{
    err_t error_flag = LTEIOT12_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    log_printf( &logger, ">>> Check GNSS power state.\r\n" );
    #define GNSS_TURNED_OFF "+QGPS: 0"
    lteiot12_cmd_get( &lteiot12, LTEIOT12_CMD_TURN_ON_GNSS );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    if ( strstr ( app_buf, GNSS_TURNED_OFF ) )
    {
        log_printf( &logger, ">>> Turn on GNSS power supply.\r\n" );
        #define GNSS_TURN_ON "1"
        lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_TURN_ON_GNSS, GNSS_TURN_ON );
        error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    }
    log_printf( &logger, ">>> Initialize GPIO3 as output.\r\n" );
    #define GNSS_INIT_GPIO3 "\"gpio\",1,64,1,0,4"
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_EXTENDED_CONFIG, GNSS_INIT_GPIO3 );
    log_printf( &logger, ">>> Turn on GNSS antenna via GPIO3.\r\n" );
    #define GNSS_ANTENNA_ON "\"gpio\",3,64,1"
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_EXTENDED_CONFIG, GNSS_ANTENNA_ON );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
#endif
    return error_flag;
}

static err_t lteiot12_example ( lteiot12_t *ctx )
{
    err_t error_flag = LTEIOT12_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };
    log_printf( &logger, ">>> Open TCP connection.\r\n" );
    #define PDP_CID "1"
    #define TCP_SOCKET_NUM "0"
    #define TCP_CONN_TYPE "TCP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, TCP_CONN_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_URC_OPEN_SOCKET );

    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    #define UDP_SOCKET_NUM "1"
    #define UDP_CONN_TYPE "UDP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, UDP_CONN_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_URC_OPEN_SOCKET );

    // 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 );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_SEND_DATA, cmd_buf );
    Delay_ms ( 100 );
    lteiot12_generic_write ( &lteiot12, MESSAGE_CONTENT, message_len );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_RECEIVE_DATA, cmd_buf );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_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 );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_SEND_DATA, cmd_buf );
    Delay_ms ( 100 );
    lteiot12_generic_write ( &lteiot12, MESSAGE_CONTENT, message_len );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_URC_RECEIVED_DATA );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    lteiot12_cmd_set( &lteiot12, LTEIOT12_CMD_RECEIVE_DATA, cmd_buf );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_CLOSE_SOCKET, TCP_SOCKET_NUM );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_CLOSE_SOCKET, UDP_SOCKET_NUM );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    log_printf( &logger, ">>> Check SMS format.\r\n" );
    lteiot12_cmd_get( &lteiot12, LTEIOT12_CMD_SELECT_SMS_FORMAT );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        lteiot12_send_sms_pdu( &lteiot12, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        lteiot12_send_sms_text ( &lteiot12, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    }
    // 30 seconds delay
    for ( uint8_t delay_cnt = 0; delay_cnt < 30; delay_cnt++ )
    {
        Delay_ms ( 1000 );
    }
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    uint8_t element_buf[ 100 ] = { 0 };
    #define GNSS_NMEA_GGA "\"GGA\""
    log_printf( &logger, ">>> Get GNSS NMEA sentence.\r\n" );
    lteiot12_cmd_set ( &lteiot12, LTEIOT12_CMD_ACQUIRE_NMEA_SENTENCES, GNSS_NMEA_GGA );
    error_flag |= lteiot12_read_response( ctx, LTEIOT12_RSP_OK );
    log_printf( &logger, ">>> Parse GNSS NMEA sentence.\r\n" );
    if ( LTEIOT12_OK == lteiot12_parse_gga( app_buf, LTEIOT12_GGA_LATITUDE, element_buf ) )
    {
        if ( strlen( element_buf ) > 0 )
        {
            log_printf( &logger, "Latitude: %.2s degrees, %s minutes\r\n", element_buf, &element_buf[ 2 ] );
            memset( element_buf, 0, sizeof( element_buf ) );
            lteiot12_parse_gga( app_buf, LTEIOT12_GGA_LONGITUDE, element_buf );
            log_printf( &logger, "Longitude: %.3s degrees, %s minutes\r\n", element_buf, &element_buf[ 3 ] );
            memset( element_buf, 0, sizeof( element_buf ) );
            lteiot12_parse_gga( app_buf, LTEIOT12_GGA_ALTITUDE, element_buf );
            log_printf( &logger, "Altitude: %s m\r\n", element_buf );
        }
        else
        {
            log_printf( &logger, "No position fix detected.\r\n" );
        }
    }
    log_printf( &logger, "--------------------------------\r\n" );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return error_flag;
}

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

额外支持

资源

喜欢这个项目吗?

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