高级
30 分钟

使用 BC65 和 STM32F410RB,实现智能电表与传感器的远距离连接

面向广泛物联网应用的低功耗 NB-IoT 连接解决方案

NB IoT 6 Click with Nucleo 64 with STM32F410RB MCU

已发布 9月 24, 2025

点击板

NB IoT 6 Click

开发板

Nucleo 64 with STM32F410RB MCU

编译器

NECTO Studio

微控制器单元

STM32F410RB

为智慧城市、农业及工业监测系统增添超低功耗蜂窝连接能力

A

A

硬件概览

它是如何工作的?

NB IoT 6 Click 基于移远通信(Quectel)的 BC65,这是一款高性能、多频段 NB-IoT 模块,具有超低功耗设计,符合 3GPP Release 13 和 Release 14 标准。该模块工作频率范围宽广,覆盖约 700MHz 至 2.2GHz,支持 LTE B1、B3、B5、B8、B20 和 B28 多个频段,支持 UDP、TCP 和 MQTT 协议栈,具备 23dBm ±2dB 的发射功率以及 –114dBm 的优异接收灵敏度(无需重传)。本板为嵌入式应用带来可靠的窄带物联网连接,非常适合智能抄表、资产追踪、智慧停车、可穿戴设备、智慧城市解决方案、农业、环境监测以及需要广域覆盖与稳定通信的家电应用。BC65 与主机 MCU 之间通过 UART 接口通信,使用标准的 RX、TX 引脚和 Ring Indicator 进行数据传输。模块默认通信速率为 115200bps,支持通过 AT 

指令进行数据交换。板上还提供多种增强功能以提升可用性和控制体验:PWR 按键可轻松开启或关闭模块,RESET 按键用于快速复位模块,这些功能同样可以通过 mikroBUS™ 引脚 PWR 和 RST 实现数字化控制,进一步提高灵活性。除了主 UART 接口之外,NB IoT 6 Click 还集成了丰富的附加通信与外设接口,使其能适应多样化的开发场景。板载调试 UART(DBG)专用于固件调试与升级,辅助 UART(AUX)则支持灵活的 AT 指令通信与数据传输,给予开发者更多的控制与连接选项。模块还集成了一个通用 10 位 ADC 通道,支持 0\~1.8V 输入电平,方便获取外部传感器或其他模拟信号。硬件特性方面,板上配有 SMA 接口,可连接 MIKROE 提供的 LTE 天线(如 LTE Flat Rotation Antenna)。蓝色 NET

 LED 用于指示当前网络状态:慢闪表示设备已成功注册网络,快闪表示正在进行数据传输,完全熄灭表示设备处于关机或省电模式(PSM)。此外,还带有一个 micro SIM 卡座,兼容 1.8V 和 3.0V uSIM 卡,方便用户根据应用需求选择合适的运营商服务。该 Click 板可通过 VCC SEL 跳线选择使用 3.3V 或 5V 逻辑电平工作。由于 BC65 模块工作电压为 4.07V,通信电平为 1.8V,因此板上集成了 TXB0104 电平转换器以确保信号转换准确,从而保证 3.3V 与 5V MCU 都能正常使用通信接口。此外,NB IoT 6 Click 还配备了配套软件库,提供易用的函数和示例代码,可作为后续开发的参考。

NB IoT 6 Click hardware overview image

功能概述

开发板

Nucleo-64 搭载 STM32F410RB 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 STM32C031C6 MCU double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

ARM Cortex-M4

MCU 内存 (KB)

128

硅供应商

STMicroelectronics

引脚数

64

RAM (字节)

32768

你完善了我!

配件

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

使用的MCU引脚

mikroBUS™映射器

Module Wake-Up
PC0
AN
Reset
PC12
RST
ID COMM
PB12
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Module Power-On
PC8
PWM
Ring Indicator
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™ 原理图

NB IoT 6 Click Schematic schematic

一步一步来

项目组装

Click Shield for Nucleo-64 accessories 1 image hardware assembly

从选择您的开发板和Click板™开始。以Nucleo 64 with STM32F410RB 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
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
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

软件支持

库描述

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

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

关键功能:

  • nbiot6_cfg_setup - 此函数将 Click 配置结构体初始化为默认值。

  • nbiot6_init - 此函数初始化该 Click 开发板所需的全部引脚和外设。

  • nbiot6_set_power_state - 此函数通过在特定时间序列下切换 PWR、RST 和 WUP 引脚来设置所需的电源状态。

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

  • nbiot6_cmd_set - 此函数为 Click 模块的指定命令设置参数值。

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

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

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

  • NBIOT6_POWER_UP - 启动设备并读取系统信息。

  • NBIOT6_CONFIG_CONNECTION - 配置设备以便能够连接到网络。

  • NBIOT6_CHECK_CONNECTION - 等待 CEREG 指令返回的网络注册结果,然后检查信号质量报告。

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

  • NBIOT6_EXAMPLE - 根据所选示例,发送 SMS 短信(PDU 或 TXT 模式)或 TCP/UDP 消息。默认情况下选择 TCP/UDP 示例。

开源

代码示例

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

/*!
 * @file main.c
 * @brief NB IoT 6 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:
 *  - NBIOT6_POWER_UP:
 * Powers up the device and reads system information.
 *
 *  - NBIOT6_CONFIG_CONNECTION:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - NBIOT6_CHECK_CONNECTION:
 * Waits for the network registration indicated via CEREG command and then checks the signal quality report.
 *
 *  - NBIOT6_CONFIG_EXAMPLE:
 * Configures device for the selected example.
 *
 *  - NBIOT6_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 nbiot6_clear_app_buf ( void )
 * - static void nbiot6_log_app_buf ( void )
 * - static err_t nbiot6_process ( nbiot6_t *ctx )
 * - static err_t nbiot6_read_response ( nbiot6_t *ctx, uint8_t *rsp, uint32_t max_rsp_time )
 * - static err_t nbiot6_power_up ( nbiot6_t *ctx )
 * - static err_t nbiot6_config_connection ( nbiot6_t *ctx )
 * - static err_t nbiot6_check_connection ( nbiot6_t *ctx )
 * - static err_t nbiot6_config_example ( nbiot6_t *ctx )
 * - static err_t nbiot6_example ( nbiot6_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 "nbiot6.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                             ""              // 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                     "NB IoT 6 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
{
    NBIOT6_POWER_UP = 1,
    NBIOT6_CONFIG_CONNECTION,
    NBIOT6_CHECK_CONNECTION,
    NBIOT6_CONFIG_EXAMPLE,
    NBIOT6_EXAMPLE

} nbiot6_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 nbiot6_app_state_t app_state = NBIOT6_POWER_UP;

static nbiot6_t nbiot6;
static log_t logger;

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

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

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

/**
 * @brief NB IoT 6 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 #nbiot6_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 nbiot6_read_response ( nbiot6_t *ctx, uint8_t *rsp, uint32_t max_rsp_time );

/**
 * @brief NB IoT 6 power up function.
 * @details This function powers up the device and reads system information.
 * @param[in] ctx : Click context object.
 * See #nbiot6_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 nbiot6_power_up ( nbiot6_t *ctx );

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

/**
 * @brief NB IoT 6 check connection function.
 * @details This function checks the connection to network.
 * @param[in] ctx : Click context object.
 * See #nbiot6_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 nbiot6_check_connection ( nbiot6_t *ctx );

/**
 * @brief NB IoT 6 config example function.
 * @details This function configures device for the selected example.
 * @param[in] ctx : Click context object.
 * See #nbiot6_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 nbiot6_config_example ( nbiot6_t *ctx );

/**
 * @brief NB IoT 6 example function.
 * @details This function executes SMS or TCP/UDP example depending on the DEMO_EXAMPLE macro.
 * @param[in] ctx : Click context object.
 * See #nbiot6_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 nbiot6_example ( nbiot6_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nbiot6_cfg_t nbiot6_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.
    nbiot6_cfg_setup( &nbiot6_cfg );
    NBIOT6_MAP_MIKROBUS( nbiot6_cfg, MIKROBUS_1 );
    if ( UART_ERROR == nbiot6_init( &nbiot6, &nbiot6_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

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

void application_task ( void ) 
{
    switch ( app_state )
    {
        case NBIOT6_POWER_UP:
        {
            if ( NBIOT6_OK == nbiot6_power_up( &nbiot6 ) )
            {
                app_state = NBIOT6_CONFIG_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CONFIG CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case NBIOT6_CONFIG_CONNECTION:
        {
            if ( NBIOT6_OK == nbiot6_config_connection( &nbiot6 ) )
            {
                app_state = NBIOT6_CHECK_CONNECTION;
                log_printf( &logger, ">>> APP STATE - CHECK CONNECTION <<<\r\n\n" );
            }
            break;
        }
        case NBIOT6_CHECK_CONNECTION:
        {
            if ( NBIOT6_OK == nbiot6_check_connection( &nbiot6 ) )
            {
                app_state = NBIOT6_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case NBIOT6_CONFIG_EXAMPLE:
        {
            if ( NBIOT6_OK == nbiot6_config_example( &nbiot6 ) )
            {
                app_state = NBIOT6_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case NBIOT6_EXAMPLE:
        {
            nbiot6_example( &nbiot6 );
            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 nbiot6_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void nbiot6_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 nbiot6_process ( nbiot6_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = nbiot6_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 NBIOT6_OK;
    }
    return NBIOT6_ERROR;
}

static err_t nbiot6_read_response ( nbiot6_t *ctx, uint8_t *rsp, uint32_t max_rsp_time ) 
{
    uint32_t timeout_cnt = 0;
    nbiot6_clear_app_buf( );
    nbiot6_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, NBIOT6_RSP_ERROR ) ) )
    {
        nbiot6_process( ctx );
        if ( timeout_cnt++ > max_rsp_time )
        {
            nbiot6_log_app_buf( );
            nbiot6_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return NBIOT6_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    nbiot6_process( ctx );
    nbiot6_log_app_buf( );
    if ( strstr( app_buf, rsp ) )
    {
        log_printf( &logger, "--------------------------------\r\n" );
        return NBIOT6_OK;
    }
    return NBIOT6_ERROR_CMD;
}

static err_t nbiot6_power_up ( nbiot6_t *ctx )
{
    err_t error_flag = NBIOT6_OK;
    
    log_printf( &logger, ">>> Power up device.\r\n" );
    nbiot6_set_power_state ( ctx, NBIOT6_POWER_STATE_ON );

    log_printf( &logger, ">>> Perform device hardware reset.\r\n" );
    nbiot6_set_power_state ( ctx, NBIOT6_POWER_STATE_RESET );
    
    log_printf( &logger, ">>> Check communication.\r\n" );
    nbiot6_clear_app_buf ( );
    for ( ; ; )
    {
        // Wake up AT interface
        nbiot6_set_power_state ( ctx, NBIOT6_POWER_STATE_WAKEUP );
        nbiot6_cmd_run( ctx, NBIOT6_CMD_AT );
        nbiot6_process( ctx );
        if ( app_buf_len > 0 )
        {
            break;
        }
    }
    nbiot6_cmd_run( ctx, NBIOT6_CMD_AT );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    
    log_printf( &logger, ">>> Get device model ID.\r\n" );
    nbiot6_cmd_run( ctx, NBIOT6_CMD_GET_MODEL_ID );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Get manufacturer revision number.\r\n" );
    nbiot6_cmd_run( ctx, NBIOT6_CMD_GET_MANUFACTURER_REVISION );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Get device serial number.\r\n" );
    nbiot6_cmd_run( ctx, NBIOT6_CMD_GET_SERIAL_NUM );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    return error_flag;
}

static err_t nbiot6_config_connection ( nbiot6_t *ctx )
{
    err_t error_flag = NBIOT6_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Enable NETLIGHT indication.\r\n" );
    #define NETLIGHT_ENABLE "1"
    nbiot6_cmd_set( ctx, NBIOT6_CMD_CONFIG_NETLIGHT_LED, NETLIGHT_ENABLE );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Deregister from network.\r\n" );
    #define DEREGISTER_FROM_NETWORK "2"
    nbiot6_cmd_set( ctx, NBIOT6_CMD_OPERATOR_SELECTION, DEREGISTER_FROM_NETWORK );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_COPS );

    log_printf( &logger, ">>> Set SIM APN.\r\n" );
    nbiot6_set_sim_apn( &nbiot6, SIM_APN );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    
    log_printf( &logger, ">>> Enable full functionality.\r\n" );
    #define FULL_FUNCTIONALITY "1"
    nbiot6_cmd_set( ctx, NBIOT6_CMD_SET_UE_FUNCTIONALITY, FULL_FUNCTIONALITY );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_CFUN );

    log_printf( &logger, ">>> Enable network registration.\r\n" );
    #define ENABLE_REG "2"
    nbiot6_cmd_set( ctx, NBIOT6_CMD_NETWORK_REGISTRATION, ENABLE_REG );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    log_printf( &logger, ">>> Set automatic registration.\r\n" );
    #define AUTOMATIC_REGISTRATION "0"
    nbiot6_cmd_set( ctx, NBIOT6_CMD_OPERATOR_SELECTION, AUTOMATIC_REGISTRATION );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_COPS );
#endif
    return error_flag;
}

static err_t nbiot6_check_connection ( nbiot6_t *ctx )
{
    err_t error_flag = NBIOT6_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    log_printf( &logger, ">>> Check network registration.\r\n" );
    nbiot6_cmd_get( &nbiot6, NBIOT6_CMD_NETWORK_REGISTRATION );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    if ( strstr( app_buf, NBIOT6_URC_NETWORK_REGISTERED ) )
    {
        Delay_ms ( 1000 );
        log_printf( &logger, ">>> Check signal quality.\r\n" );
        nbiot6_cmd_run( &nbiot6, NBIOT6_CMD_SIGNAL_QUALITY_REPORT );
        error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    }
    else
    {
        error_flag = NBIOT6_ERROR;
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
#endif
    return error_flag;
}

static err_t nbiot6_config_example ( nbiot6_t *ctx )
{
    err_t error_flag = NBIOT6_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    log_printf( &logger, ">>> Activate PDP context.\r\n" );
    #define ACTIVATE_PDP_CONTEXT "1,1"
    nbiot6_cmd_set( &nbiot6, NBIOT6_CMD_ACTIVATE_PDP_CONTEXT, ACTIVATE_PDP_CONTEXT );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_CGACT );

    log_printf( &logger, ">>> Show PDP address.\r\n" );
    #define PDP_CID "1"
    nbiot6_cmd_set( &nbiot6, NBIOT6_CMD_SHOW_PDP_ADDRESS, PDP_CID );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    log_printf( &logger, ">>> Select SMS format.\r\n" );
    nbiot6_cmd_set( &nbiot6, NBIOT6_CMD_SELECT_SMS_FORMAT, SMS_MODE );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
#endif
    return error_flag;
}

static err_t nbiot6_example ( nbiot6_t *ctx )
{
    err_t error_flag = NBIOT6_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ NBIOT6_TX_DRV_BUFFER_SIZE ] = { 0 };

    // 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, ">>> Open TCP connection.\r\n" );
    #define PDP_CID "1"
    #define TCP_SOCKET_NUM   "0"
    #define TCP_SERVICE_TYPE "TCP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, TCP_SERVICE_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_URC_OPEN_SOCKET, NBIOT6_MAX_RSP_TIME_URC );
    
    log_printf( &logger, ">>> Open UDP connection.\r\n" );
    #define UDP_SOCKET_NUM   "1"
    #define UDP_SERVICE_TYPE "UDP"
    strcpy( cmd_buf, PDP_CID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_SOCKET_NUM );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, UDP_SERVICE_TYPE );
    strcat( cmd_buf, "\",\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"," );
    strcat( cmd_buf, REMOTE_PORT );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_OPEN_SOCKET, cmd_buf );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_URC_OPEN_SOCKET, NBIOT6_MAX_RSP_TIME_URC );

    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 );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_SEND_TCP_IP_TEXT_DATA, cmd_buf );
    Delay_ms ( 100 );
    nbiot6_generic_write ( &nbiot6, MESSAGE_CONTENT, message_len );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_URC_RECEIVED_DATA, NBIOT6_MAX_RSP_TIME_URC );
    log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
    nbiot6_cmd_set( &nbiot6, NBIOT6_CMD_RECEIVE_TCP_IP_DATA, cmd_buf );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );

    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 );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_SEND_TCP_IP_TEXT_DATA, cmd_buf );
    Delay_ms ( 100 );
    nbiot6_generic_write ( &nbiot6, MESSAGE_CONTENT, message_len );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_URC_RECEIVED_DATA, NBIOT6_MAX_RSP_TIME_URC );
    log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
    nbiot6_cmd_set( &nbiot6, NBIOT6_CMD_RECEIVE_TCP_IP_DATA, cmd_buf );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    
    log_printf( &logger, ">>> Close TCP connection.\r\n" );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_CLOSE_SOCKET, TCP_SOCKET_NUM );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_CLOSE_OK, NBIOT6_MAX_RSP_TIME_QICLOSE );
    
    log_printf( &logger, ">>> Close UDP connection.\r\n" );
    nbiot6_cmd_set ( &nbiot6, NBIOT6_CMD_CLOSE_SOCKET, UDP_SOCKET_NUM );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_CLOSE_OK, NBIOT6_MAX_RSP_TIME_QICLOSE );
    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" );
    nbiot6_cmd_get( &nbiot6, NBIOT6_CMD_SELECT_SMS_FORMAT );
    error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_DEFAULT );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        log_printf( &logger, ">>> Send SMS in PDU mode.\r\n" );
        nbiot6_send_sms_pdu( &nbiot6, SIM_SMSC, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_CMGS );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        log_printf( &logger, ">>> Send SMS in TXT mode.\r\n" );
        nbiot6_send_sms_text ( &nbiot6, PHONE_NUMBER, MESSAGE_CONTENT );
        error_flag |= nbiot6_read_response( ctx, NBIOT6_RSP_OK, NBIOT6_MAX_RSP_TIME_CMGS );
    }
    // 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

额外支持

资源

喜欢这个项目吗?

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