中级
30 分钟

使用 SARA-R4 和 STM32G071RB 访问全球数据网络

一个蜂窝模块,无限可能

LTE IoT 7 Click with Nucleo 64 with STM32G071RB MCU

已发布 10月 08, 2024

点击板

LTE IoT 7 Click

开发板

Nucleo 64 with STM32G071RB MCU

编译器

NECTO Studio

微控制器单元

STM32G071RB

IoT应用的安全云多频段解决方案

A

A

硬件概览

它是如何工作的?

LTE IoT 7 Click基于u-blox的SARA-R422M8S,这是一款多频段LTE-M/NB-IoT/EGPRS多模式蜂窝模块。它采用了微小的SARA LGA封装模块,可以轻松替代其他u-blox蜂窝模块系列。SARA-R422M8S模块提供了基于软件的多频段配置,实现LTE-M/NB-IoT和(E)GPRS无线接入技术的全球覆盖,支持适用于物联网应用的一套完整的3GPP Rel. 14功能。SARA-R422M8S模块非常适用于关键任务的物联网解决方案,因为它具有独特且不可变的信任根源。它支持物联网安全服务,为一组可信的高级安全功能提供了基础,并提供了最佳的数据加密和解密功能,包括设备到云端和设备上的加密。利用最新的(D)TLS堆栈和密码套件以及硬件加速的加密技术提供了强大、高效且受保护的通信。此模块需要3.8V的电源供应。因此,Click板™集成了由德州仪器标记为TPS7A7002的集成降压(降压DC-DC)转换器,该转换器提供了稳定的3.8V电源,能

够在输入电流出现高峰值时(通常在设备启动时)减轻电压下降。SARA-R422M8S使用UART接口与MCU通信,常用的UART RX和TX引脚以及硬件流控制引脚UART CTS、RTS、RI(清除发送、准备发送和振铃指示器)。它默认配置为115200 bps,通过u-blox提供的AT命令与主机MCU进行数据传输和交换。它还配备了一个USB Type-C连接器,仅供诊断目的使用。该模块是一个USB设备,可以连接到任何具有兼容驱动程序的USB主机。标记为PWR的按钮连接到mikroBUS™插座上的AN引脚,代表点火(上电)按钮,黄色STAT LED指示操作成功。在使用的引脚中,该Click板™还具有一个GNSS外部中断,用于控制GNSS接收器或辅助。它具有三个额外的LED指示器:黄色STAT LED可视地指示设备的操作状态,红色TX LED用于指示网络状态,橙色PPS LED用于时间脉冲信号信息和指示。由于内置了SAW滤波器,紧随高性能u-blox M8并发定位

引擎前面的LNA,因此SARA-R422M8S的GNSS RF输入具有50Ω的特征阻抗和内部直流阻断,适用于主动或被动GNSS天线。除了这些SMA连接器(用于LTE和GNSS天线),LTE IoT 7 Click还具有一个Nano-SIM卡槽,提供多个连接和接口选项。J1标头允许您从内部u-blox GNSS访问可配置的GPIO和GNSS数据输出,而从TP1到TP6标记的测试点可实现简单的FW升级和模块测试。通过使用uFOTA客户端/服务器解决方案,利用轻量级和紧凑的LWM2M协议,客户可以通过空中固件更新未来验证他们的解决方案。正如前面提到的,我们还为FW升级目的直接连接到TxD和RxD引脚的可访问测试点。此Click板™可以通过VCC SEL跳线选择3.3V或5V逻辑电压电平操作。这样,既可以使用3.3V又可以使用5V的MCU正确使用通信线。然而,此Click板配备了一个包含易于使用的功能和示例代码的库,可用作进一步开发的参考。

LTE IoT 7 Click hardware overview image

功能概述

开发板

Nucleo-64 搭载 STM32G071RB MCU 提供了一种经济高效且灵活的平台,供开发者探索新想法并原型设计他们的项目。该板利用 STM32 微控制器的多功能性,使用户能够为他们的项目选择最佳的性能与功耗平衡。它配备了 LQFP64 封装的 STM32 微控制器,并包含了如用户 LED(同时作为 ARDUINO® 信号)、用户和复位按钮,以及 32.768kHz 晶体振荡器用于精确的计时操作等基本组件。Nucleo-64 板设计考虑到扩展性和灵活性,它特有的 ARDUINO® Uno

V3 扩展连接器和 ST morpho 扩展引脚头,提供了对 STM32 I/O 的完全访问,以实现全面的项目整合。电源供应选项灵活,支持 ST-LINK USB VBUS 或外部电源,确保在各种开发环境中的适应性。该板还配备了一个具有 USB 重枚举功能的板载 ST-LINK 调试器/编程器,简化了编程和调试过程。此外,该板设计旨在简化高级开发,它的外部 SMPS 为 Vcore 逻辑供电提供高效支持,支持 USB 设备全速或 USB SNK/UFP 全速,并内置加密功能,提升了项目的功效

和安全性。通过外部 SMPS 实验的专用连接器、 用于 ST-LINK 的 USB 连接器以及 MIPI® 调试连接器,提供了更多的硬件接口和实验可能性。开发者将通过 STM32Cube MCU Package 提供的全面免费软件库和示例得到广泛支持。这些,加上与多种集成开发环境(IDE)的兼容性,包括 IAR Embedded Workbench®、MDK-ARM 和 STM32CubeIDE,确保了流畅且高效的开发体验,使用户能够充分利用 Nucleo-64 板在他们的项目中的能力。

Nucleo 64 with STM32G071RB MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M0

MCU 内存 (KB)

128

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

36864

你完善了我!

配件

Click Shield for Nucleo-64 配备了两个专有的 mikroBUS™ 插座,使得所有的 Click board™ 设备都可以轻松地与 STM32 Nucleo-64 开发板连接。这样,Mikroe 允许其用户从不断增长的 Click boards™ 范围中添加任何功能,如 WiFi、GSM、GPS、蓝牙、ZigBee、环境传感器、LED、语音识别、电机控制、运动传感器等。您可以使用超过 1537 个 Click boards™,这些 Click boards™ 可以堆叠和集成。STM32 Nucleo-64 开发板基于 64 引脚封装的微控制器,采用 32 位 MCU,配备 ARM Cortex M4 处理器,运行速度为 84MHz,具有 512Kb Flash 和 96KB SRAM,分为两个区域,顶部区域代表 ST-Link/V2 调试器和编程器,而底部区域是一个实际的开发板。通过 USB 连接方便地控制和供电这些板子,以便直接对 Nucleo-64 开发板进行编程和高效调试,其中还需要额外的 USB 线连接到板子上的 USB 迷你接口。大多数 STM32 微控制器引脚都连接到了板子左右边缘的 IO 引脚上,然后连接到两个现有的 mikroBUS™ 插座上。该 Click Shield 还有几个开关,用于选择 mikroBUS™ 插座上模拟信号的逻辑电平和 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换器,使用任何 Click board™,无论 Click board™ 是否在 3.3V 或 5V 逻辑电压电平下运行。一旦将 STM32 Nucleo-64 开发板与我们的 Click Shield for Nucleo-64 连接,您就可以访问数百个工作于 3.3V 或 5V 逻辑电压电平的 Click boards™。

Click Shield for Nucleo-64 accessories 1 image

GNSS Active External Antenna是来自u-blox的一种独特的多频段天线,是高精度GNSS应用的完美选择,需要高度精确的定位能力,如RTK。ANN-MB-00是一款多频段(L1、L2/E5b/B2I)主动GNSS天线,带有5米长的电缆和SMA连接器。该天线支持GPS、GLONASS、Galileo和BeiDou,并包括高性能的多频段RHCP双馈源贴片天线元件、内置的高增益LNA和SAW预滤波器,以及5米长的带SMA连接器的天线电缆,具有防水功能。

LTE IoT 7 Click accessories 1 image

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

LTE IoT 7 Click accessories 2 image

使用的MCU引脚

mikroBUS™映射器

Power-On
PC0
AN
GNSS Interrupt
PC12
RST
UART RTS
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
UART RI
PC8
PWM
UART CTS
PC14
INT
UART TX
PA2
TX
UART RX
PA3
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

LTE IoT 7 Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 accessories 1 image hardware assembly

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

Click Shield for Nucleo-64 accessories 1 image hardware assembly
Nucleo 64 with STM32F401RE MCU front image hardware assembly
LTE IoT 5 Click front image hardware assembly
Prog-cut hardware assembly
LTE IoT 5 Click complete accessories setup image hardware assembly
Nucleo-64 with STM32XXX MCU Access MB 1 Mini B Conn - 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
Clicker 4 for STM32F4 HA 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 7 Click 驱动程序的 API。

关键功能:

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

  • lteiot7_send_sms_text - 此函数向手机号发送文本消息。

  • lteiot7_parse_gpgga - 此函数从读取响应缓冲区中解析GPGGA数据。

开源

代码示例

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

/*!
 * @file main.c
 * @brief LTE IoT 7 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, reboots the module which takes about 20 seconds and then tests the communication.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - LTEIOT7_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network. (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT7_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CREG URC event and then checks
 * the connection status (used only for SMS or TCP/UDP demo examples).
 *
 *  - LTEIOT7_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages or for retrieving data from GNSS 
 * depending on the selected demo example.
 *
 *  - LTEIOT7_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 lteiot7_clear_app_buf ( void )
 * - static err_t lteiot7_process ( void )
 * - static void lteiot7_error_check( err_t error_flag )
 * - static void lteiot7_log_app_buf ( void )
 * - static err_t lteiot7_rsp_check ( void )
 * - static err_t lteiot7_configure_for_connection( void )
 * - static err_t lteiot7_check_connection( void )
 * - static err_t lteiot7_configure_for_messages( void )
 * - static err_t lteiot7_send_message( void )
 *
 * @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_TO_MESSAGE.
 * Example:
    SIM_APN "internet"
    SIM_SMSC "+381610401"
    PHONE_NUMBER_TO_MESSAGE "+381659999999"
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lteiot7.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 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_TO_MESSAGE             ""              // Set Phone number to message
#define SMS_MODE                            "1"             // SMS mode: "0" - PDU, "1" - TXT

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

// Message content
#define MESSAGE_CONTENT                     "LTE IoT 7 click board - demo example."

// Application buffer size
#define PROCESS_BUFFER_SIZE                 300

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    LTEIOT7_CONFIGURE_FOR_NETWORK = 1,
    LTEIOT7_WAIT_FOR_CONNECTION,
    LTEIOT7_CONFIGURE_FOR_EXAMPLE,
    LTEIOT7_EXAMPLE

} lteiot7_example_state_t;

static lteiot7_t lteiot7;
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 lteiot7_example_state_t example_state;

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

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

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

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

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

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

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

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

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lteiot7_cfg_t lteiot7_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.
    lteiot7_cfg_setup( &lteiot7_cfg );
    LTEIOT7_MAP_MIKROBUS( lteiot7_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lteiot7_init( &lteiot7, &lteiot7_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    
    lteiot7_set_power_state ( &lteiot7, LTEIOT7_POWER_STATE_REBOOT );
    
    lteiot7_process( );
    lteiot7_clear_app_buf( );
    app_buf_len = 0;
    app_buf_cnt = 0;

    // Check communication
    lteiot7_send_cmd( &lteiot7, LTEIOT7_CMD_AT );
    error_flag = lteiot7_rsp_check( );
    lteiot7_error_check( error_flag );

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

void application_task ( void )
{
    switch ( example_state )
    {
        case LTEIOT7_CONFIGURE_FOR_NETWORK:
        {
            if ( LTEIOT7_OK == lteiot7_configure_for_network( ) )
            {
                example_state = LTEIOT7_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case LTEIOT7_WAIT_FOR_CONNECTION:
        {
            if ( LTEIOT7_OK == lteiot7_check_connection( ) )
            {
                example_state = LTEIOT7_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case LTEIOT7_CONFIGURE_FOR_EXAMPLE:
        {
            if ( LTEIOT7_OK == lteiot7_configure_for_example( ) )
            {
                example_state = LTEIOT7_EXAMPLE;
            }
            break;
        }
        case LTEIOT7_EXAMPLE:
        {
            lteiot7_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example state." );
            break;
        }
    }
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}

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

static err_t lteiot7_process ( void )
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };
    rx_size = lteiot7_generic_read( &lteiot7, rx_buff, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 )
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE )
        {
            lteiot7_clear_app_buf(  );
            return LTEIOT7_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 LTEIOT7_OK;
    }
    return LTEIOT7_ERROR;
}

static err_t lteiot7_rsp_check ( void )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    err_t error_flag = lteiot7_process( );
    if ( ( LTEIOT7_OK != error_flag ) && ( LTEIOT7_ERROR != error_flag ) )
    {
        return error_flag;
    }
    while ( ( 0 == strstr( app_buf, LTEIOT7_RSP_OK ) ) &&
            ( 0 == strstr( app_buf, LTEIOT7_RSP_ERROR ) ) )
    {
        error_flag = lteiot7_process( );
        if ( ( LTEIOT7_OK != error_flag ) && ( LTEIOT7_ERROR != error_flag ) )
        {
            return error_flag;
        }
        if ( timeout_cnt++ > timeout )
        {
            lteiot7_clear_app_buf( );
            return LTEIOT7_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    if ( strstr( app_buf, LTEIOT7_RSP_OK ) )
    {
        return LTEIOT7_OK;
    }
    else if ( strstr( app_buf, LTEIOT7_RSP_ERROR ) )
    {
        return LTEIOT7_ERROR_CMD;
    }
    else
    {
        return LTEIOT7_ERROR_UNKNOWN;
    }
}

static void lteiot7_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case LTEIOT7_OK:
        {
            lteiot7_log_app_buf( );
            break;
        }
        case LTEIOT7_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case LTEIOT7_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case LTEIOT7_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case LTEIOT7_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    lteiot7_clear_app_buf(  );
    Delay_ms( 500 );
}

static void lteiot7_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 lteiot7_configure_for_network( void )
{
    err_t func_error = LTEIOT7_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    // Deregister from network
    #define DEREGISTER_FROM_NETWORK "2"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_COPS, DEREGISTER_FROM_NETWORK );
    error_flag = lteiot7_rsp_check();
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
    
    // Set SIM APN
    lteiot7_set_sim_apn( &lteiot7, SIM_APN );
    error_flag = lteiot7_rsp_check();
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = lteiot7_rsp_check();
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Automatic registration
    #define AUTOMATIC_REGISTRATION "0"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_COPS, AUTOMATIC_REGISTRATION );
    error_flag = lteiot7_rsp_check();
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Enable network registartion
    #define ENABLE_REG "2"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_CREG, ENABLE_REG );
    error_flag = lteiot7_rsp_check();
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
#endif
    return func_error;
}

static err_t lteiot7_check_connection( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    #define CONNECTED "+CREG: 1"
    lteiot7_process( );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms( 100 );
        lteiot7_process( );
        lteiot7_log_app_buf( );
        log_printf( &logger, "\r\n" );
        lteiot7_clear_app_buf( );
        // Check signal quality
        lteiot7_send_cmd( &lteiot7, LTEIOT7_CMD_CSQ );
        error_flag = lteiot7_rsp_check( );
        lteiot7_error_check( error_flag );
        return error_flag;
    }
    return LTEIOT7_ERROR;
#endif
    return LTEIOT7_OK;
}

static err_t lteiot7_configure_for_example( void )
{
    err_t func_error = LTEIOT7_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ACTIVATE_PDP_CONTEXT "1,1"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_CGACT, ACTIVATE_PDP_CONTEXT );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_CMGF, SMS_MODE );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    lteiot7_send_cmd_check( &lteiot7, LTEIOT7_CMD_UGPS );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    #define GPS_POWERED_OFF "+UGPS: 0"
    if ( strstr( app_buf, GPS_POWERED_OFF ) )
    {
        #define ENABLE_GPS "1,0,1"
        lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_UGPS, ENABLE_GPS );
        error_flag = lteiot7_rsp_check( );
        func_error |= error_flag;
        lteiot7_error_check( error_flag );
        Delay_ms ( 1000 );
    }
    #define ENABLE_NMEA_GGA "1"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_UGGGA, ENABLE_NMEA_GGA );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

    // Create TCP socket
    #define RSP_USOCR "+USOCR: "
    #define TCP_PROTOCOL "6"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCR, TCP_PROTOCOL );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    tcp_socket_num[ 0 ] = *socket_num_buf;
    lteiot7_error_check( error_flag );

    // Create UDP socket
    #define UDP_PROTOCOL "17"
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCR, UDP_PROTOCOL );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    socket_num_buf = strstr( app_buf, RSP_USOCR ) + strlen ( RSP_USOCR );
    udp_socket_num[ 0 ] = *socket_num_buf;
    lteiot7_error_check( error_flag );

    // Connect TCP socket to remote IP and port
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCO, cmd_buf );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Connect UDP socket to remote IP and port
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCO, cmd_buf );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Get message length
    uint8_t message_len_buf[ 5 ] = { 0 };
    uint16_t message_len = strlen( MESSAGE_CONTENT );
    uint16_to_str( message_len, message_len_buf );
    l_trim( message_len_buf );
    r_trim( message_len_buf );

    // Write message to TCP socket
    strcpy( cmd_buf, tcp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOWR, cmd_buf );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Read response message from TCP socket
    #define URC_READ_SOCKET_DATA_TCP "+UUSORD: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_TCP );
    strcat( urc_buf, tcp_socket_num );
    for ( ; ; )
    {
        lteiot7_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms( 100 );
            lteiot7_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, tcp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            lteiot7_log_app_buf( );
            lteiot7_clear_app_buf( );
            lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USORD, cmd_buf );
            error_flag = lteiot7_rsp_check( );
            func_error |= error_flag;
            lteiot7_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms( 1 );
    }
    timeout_cnt = 0;

    // Write message to UDP socket
    strcpy( cmd_buf, udp_socket_num );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, message_len_buf );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, MESSAGE_CONTENT );
    strcat( cmd_buf, "\"" );
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOWR, cmd_buf );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );

    // Read response message from UDP socket
    #define URC_READ_SOCKET_DATA_UDP "+UUSORF: "
    strcpy( urc_buf, URC_READ_SOCKET_DATA_UDP );
    strcat( urc_buf, udp_socket_num );
    for ( ; ; )
    {
        lteiot7_process( );
        uint8_t * __generic_ptr start_response_buf = strstr( app_buf, urc_buf );
        if ( start_response_buf )
        {
            Delay_ms( 100 );
            lteiot7_process( );
            uint8_t response_len_buf[ 5 ] = { 0 };
            char * __generic_ptr start_response_len = strstr( start_response_buf, "," ) + 1;
            memcpy ( response_len_buf, start_response_len, app_buf_len - ( start_response_len - app_buf ) );
            strcpy( cmd_buf, udp_socket_num );
            strcat( cmd_buf, "," );
            strcat( cmd_buf, response_len_buf );
            lteiot7_log_app_buf( );
            lteiot7_clear_app_buf( );
            lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USORF, cmd_buf );
            error_flag = lteiot7_rsp_check( );
            func_error |= error_flag;
            lteiot7_error_check( error_flag );
            break;
        }
        if ( timeout_cnt++ > timeout )
        {
            break;
        }
        Delay_ms( 1 );
    }
    
    // Close TCP socket
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCL, tcp_socket_num );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
    
    // Close UDP socket
    lteiot7_send_cmd_with_parameter( &lteiot7, LTEIOT7_CMD_USOCL, udp_socket_num );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    lteiot7_error_check( error_flag );
    Delay_ms( 5000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    // Check SMS mode
    #define CMGF_PDU "+CMGF: 0"
    #define CMGF_TXT "+CMGF: 1"
    lteiot7_send_cmd_check( &lteiot7, LTEIOT7_CMD_CMGF );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        lteiot7_error_check( error_flag );
        // Send SMS in PDU mode
        lteiot7_send_sms_pdu( &lteiot7, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot7_rsp_check( );
        func_error |= error_flag;
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        lteiot7_error_check( error_flag );
        // Send SMS in TXT mode
        lteiot7_send_sms_text ( &lteiot7, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = lteiot7_rsp_check( );
        func_error |= error_flag;
    }
    lteiot7_error_check( error_flag );
    Delay_ms( 10000 );
    Delay_ms( 10000 );
    Delay_ms( 10000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_GNSS )
    lteiot7_send_cmd_check( &lteiot7, LTEIOT7_CMD_UGGGA );
    error_flag = lteiot7_rsp_check( );
    func_error |= error_flag;
    if ( app_buf_len > ( sizeof ( LTEIOT7_RSP_GPGGA ) + LTEIOT7_GPGGA_ELEMENT_SIZE ) ) 
    {
        char element_buf[ 100 ] = { 0 };
        if ( LTEIOT7_OK == lteiot7_parse_gpgga( app_buf, LTEIOT7_GPGGA_LATITUDE, element_buf ) )
        {
            static uint8_t wait_for_fix_cnt = 0;
            if ( strlen( element_buf ) > 0 )
            {
                log_printf( &logger, "\r\n Latitude: %.2s degrees, %s minutes \r\n", element_buf, &element_buf[ 2 ] );
                lteiot7_parse_gpgga( app_buf, LTEIOT7_GPGGA_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 ) );
                lteiot7_parse_gpgga( app_buf, LTEIOT7_GPGGA_ALTITUDE, element_buf );
                log_printf( &logger, " Altitude: %s m \r\n", element_buf );
                wait_for_fix_cnt = 0;
            }
            else
            {
                if ( wait_for_fix_cnt % 5 == 0 )
                {
                    log_printf( &logger, " Waiting for the position fix...\r\n\n" );
                    wait_for_fix_cnt = 0;
                }
                wait_for_fix_cnt++;
            }
            lteiot7_clear_app_buf(  );
        }
    }
    Delay_ms( 1000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

额外支持

资源

喜欢这个项目吗?

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