中级
30 分钟

使用 BC66-NA 和 PIC18F2553 实现无故障 NB-IoT 连接

在任何环境中保持连接和响应

NB IoT 2 Click with EasyPIC v8

已发布 6月 24, 2024

点击板

NB IoT 2 Click

开发板

EasyPIC v8

编译器

NECTO Studio

微控制器单元

PIC18F2553

通过NB-IoT技术,以全新的视角体验物联网,重新定义可能,实现无限创新。

A

A

硬件概览

它是如何工作的?

NB IoT 2 Click基于Quectel Wireless Solutions的BC66-NA,这是一款高性能NB-IoT模块,具有极低的功耗。它旨在通过NB-IoT无线协议(3GPP Rel.13和3GPP Rel.14)与移动网络运营商的基础设施进行通信。支持的频段广泛,包括B1/B2/B3/B4/B5/B8/B12/B13/B17/B18/B19/B20/B25/B28/B66/B71/B85。该模块还提供了多种接口,如UART和SPI,以及诸如UDP/TCP、MQTT、LwM2M等协议栈。这些协议允许使用NB技术进行数据和SMS传输,使该模块成为构建物联网应用(如智能燃气和水表)的理想选择,即使无需外部MCU单元。要启动此Click板™,用户必须按住板载的PWRKEY按钮至少500ms,这代表点火(开机)按钮。

PWRKEY按钮不能持续按下,否则模块无法进入省电模式。黄灯标记为CONNECT,用于指示设备的运行状态。NB IoT 2 Click使用UART接口作为其默认通信协议,用户也可以选择使用其他接口(如SPI)来配置模块并自行编写库。它支持自动波特率检测,默认配置下的运行速率为115200 bps,用于数据传输和与主机MCU交换AT命令。此外,BC66-NA使用了连接到mikroBUS™插座的两个GPIO引脚。WUP引脚连接到mikroBUS™的INT引脚,代表外部中断引脚,用于从省电模式唤醒设备;mikroBUS™插座上的RST引脚可以通过将该引脚置于低电平来执行硬件复位功能。此Click板™还配备了一个微型USB接口,允许通过个人计算机为模块供

电和配置。此外,它还有一个标记为ADC IN的额外头部,因为BC66-NA提供了一个10位ADC输入通道,用于读取电压值。该接口在活动模式下可用,并且在睡眠模式下必须先唤醒以确保可用性。NB IoT 2 Click配有50Ω阻抗的SMA天线连接器。此Click板™可用于连接MIKROE提供的适当天线。除了SMA连接器,它还有一个SIM卡槽,提供多种连接和接口选项。此Click板™只能在3.3V逻辑电压电平下运行,并通过适当的电压电平转换器TXB0106进行适当的逻辑电压电平转换。在与不同逻辑电平的MCU一起使用之前,板子必须完成适当的逻辑电压电平转换。然而,此Click板™配备了一个库,包含函数和示例代码,可作为进一步开发的参考。

NB IoT 2 Click hardware overview image

功能概述

开发板

EasyPIC v8 是一款专为快速开发嵌入式应用的需求而特别设计的开发板。它支持许多高引脚计数的8位PIC微控制器,来自Microchip,无论它们的引脚数量如何,并且具有一系列独特功能,例如首次集成的调试器/程序员。开发板布局合理,设计周到,使得最终用户可以在一个地方找到所有必要的元素,如开关、按钮、指示灯、连接器等。得益于创新的制造技术,EasyPIC v8 提供了流畅而沉浸式的工作体验,允许在任何情况下、任何地方、任何时候都能访问。

EasyPIC v8 开发板的每个部分都包含了使同一板块运行最高效的必要组件。除了先进的集成CODEGRIP程 序/调试模块,该模块提供许多有价值的编程/调试选项和与Mikroe软件环境的无缝集成外,该板还包括一个干净且调节过的开发板电源供应模块。它可以使用广泛的外部电源,包括电池、外部12V电源供应和通过USB Type-C(USB-C)连接器的电源。通信选项如USB-UART、USB DEVICE和CAN也包括在内,包括 广受好评的mikroBUS™标准、两种显示选项(图形和

基于字符的LCD)和几种不同的DIP插座。这些插座覆盖了从最小的只有八个至四十个引脚的8位PIC MCU的广泛范围。EasyPIC v8 是Mikroe快速开发生态系统的一个组成部分。它由Mikroe软件工具原生支持,得益于大量不同的Click板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作和开发的许多方面。

EasyPIC v8 horizontal image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

28

RAM (字节)

2048

你完善了我!

配件

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

NB IoT 2 Click accessories image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset
RA0
RST
SPI Chip Select
RA5
CS
SPI Clock
RC3
SCK
SPI Data OUT
RC4
MISO
SPI Data IN
RC5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Wake-Up Interrupt
RB1
INT
UART TX
RC6
TX
UART RX
RC7
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
2

“仔细看看!”

Click board™ 原理图

NB IoT 2 Click Schematic schematic

一步一步来

项目组装

EasyPIC v8 front image hardware assembly

从选择您的开发板和Click板™开始。以EasyPIC v8作为您的开发板开始。

EasyPIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
MCU DIP 40 hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyPIC v8 Access DIPMB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto DIP image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image 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”替换为要显示的参数。

软件支持

库描述

该库包含 NB IoT 2 Click 驱动程序的 API。

关键功能:

  • nbiot2_set_rst_pin_state - 此功能设置RST引脚状态

  • nbiot2_hw_reset - NB IoT 2硬件复位功能

  • nbiot2_send_cmd - NB IoT 2发送命令功能

开源

代码示例

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

/*!
 * @file main.c
 * @brief NB IoT 2 Click Example.
 *
 * # Description
 * This example reads and processes data from NB IoT 2 clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver, wake-up module and sets default configuration 
 * for connecting device to network.
 *
 * ## Application Task
 * This application is split into two examples:
 * - TCP/UDP example - connecting to echo server and writing and reading data.
 * - SMS example - Sending SMS message to desired number using TEXT or PDU mode. 
 *
 * ## Additional Function
 * - static void nbiot2_clear_app_buf ( void )
 * - static err_t nbiot2_process ( void )
 * - static void nbiot2_config_device_for_example ( void )
 * - static void nbiot2_check_connection ( void )
 * - static void nbiot2_log_response ( void )
 * - static void nbiot2_error_check( err_t error_flag );
 * - static err_t nbiot2_check_response ( void )
 * - static void nbiot2_config_device_for_network ( void )
 * - static void nbiot2_send_sms ( void )
 * - static void nbiot2_config_device_for_tcp_udp ( void )
 * - static void nbiot2_tcp_udp_example ( void )
 *
 * @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 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 Ilic
 *
 */

#include "board.h"
#include "log.h"
#include "conversions.h"
#include "nbiot2.h"

// Example selection macros
#define NBIOT2_TCP_UDP_EXAMPLE
// #define NBIOT2_SMS_EXAMPLE

// SMS example selection macros
// #define SMS_PDU_MODE
#define SMS_TEXT_MODE

// SMS example parameters
#define PHONE_NUMBER_TO_MESSAGE             ""              // Set Phone number to message
#define SIM_SMSC                            ""              // Set SIM SMSC

// 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

// SIM APN config
#define SIM_APN                             ""
#define MESSAGE_CONTENT                     "NB IoT 2 Click Example"   // Message content

#define PROCESS_BUFFER_SIZE                 200

#define CONFIGURATION_FOR_EXAMPLE           0
#define EXAMPLE                             1

static nbiot2_t nbiot2;
static log_t logger;

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 app_error_flag;

static uint8_t app_connection_status = CONFIGURATION_FOR_EXAMPLE;

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

/**
 * @brief NB IoT 2 data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nbiot2_process ( void );

/**
 * @brief Configure device for example.
 * @details Configure device for sending messages or sending messages to TCP/UDP Echo server.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_example ( void );

/**
 * @brief Wait for connection signal.
 * @details Wait for connection signal from CREG URC.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_check_connection ( void );

/**
 * @brief Logs application buffer.
 * @details This function logs data from application buffer.
 * @note None.
 */
static void nbiot2_log_response ( 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.
 * @note None.
 */
static void nbiot2_error_check( err_t error_flag );

/**
 * @brief NB IoT 2 response read function.
 * @details This function reads the response of the sent command and writes it on the USB UART.
 * @return @li @c  0 - Response OK.
 *         @li @c -1 - Response ERROR.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t nbiot2_check_response ( void );

/**
 * @brief Configure device for connection to the network.
 * @details Sends commands to configure and enable connection to the specified network.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_network ( void );

/**
 * @brief Check for connection to the network.
 * @details Sends commands to check connection to the specified network.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_check_connection_to_network ( void );

/**
 * @brief Send SMS example.
 * @details Sends commands to send SMS messages in TEXT or PDU mode.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_send_sms ( void );

/**
 * @brief Configure device for TCP/UDP example.
 * @details Sends commands to connect with TCP/UDP server.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_config_device_for_tcp_udp ( void );

/**
 * @brief TCP/UDP example.
 * @details Sends message to TCP/UDP server and receives data from it.
 * @return Nothing.
 * @note None.
 */
static void nbiot2_tcp_udp_example ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nbiot2_cfg_t nbiot2_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.
    nbiot2_cfg_setup( &nbiot2_cfg );
    NBIOT2_MAP_MIKROBUS( nbiot2_cfg, MIKROBUS_1 );
    if ( UART_ERROR == nbiot2_init( &nbiot2, &nbiot2_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_printf( &logger, " - Reseting device... \r\n" );
    nbiot2_hw_reset( &nbiot2 );
    Delay_ms( 4000 );
    app_error_flag = nbiot2_process( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_AT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_ATI );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CFUN, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QSPCHSC, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CPSMS, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );
    
    nbiot2_set_apn( &nbiot2, SIM_APN );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );   
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QRST, "1" );
    Delay_ms( 2000 );
    app_error_flag = nbiot2_process( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QBAND, "1,20" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );      
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CEREG, "2" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );   
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CSCON, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QNBIOTEVENT, "1,1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_SM, "LOCK" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QSCLK, "0" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    log_info( &logger, " Application Task " );
    
    app_connection_status = CONFIGURATION_FOR_EXAMPLE;
    
    app_buf_len = 0;
    app_buf_cnt = 0;
}

void application_task ( void ) 
{   
    switch( app_connection_status )
    {
        case CONFIGURATION_FOR_EXAMPLE:
        {
            nbiot2_config_device_for_example( );
            break;
        }
        case EXAMPLE:
        {
            #if defined( NBIOT2_TCP_UDP_EXAMPLE )
                nbiot2_tcp_udp_example( );
            #endif   
            #if defined( NBIOT2_SMS_EXAMPLE )
                nbiot2_send_sms( );
                Delay_ms( 10000 );
            #endif
            break;
        }
        default:
        {
            log_error( &logger, "Application status error!" );
            app_connection_status = CONFIGURATION_FOR_EXAMPLE;
            Delay_ms( 1000 );
            break;
        }
    }
}

void main ( void ) 
{
    application_init( );

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

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

static err_t nbiot2_process ( void ) 
{
    int32_t rx_size;
    char rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    rx_size = nbiot2_generic_read( &nbiot2, rx_buf, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;
        if ( ( app_buf_len + rx_size ) > PROCESS_BUFFER_SIZE ) 
        {
            nbiot2_clear_app_buf(  );
            return NBIOT2_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_buf[ rx_cnt ] != 0 ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buf[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }
        }
        return NBIOT2_OK;
    }
    return NBIOT2_ERROR;
}

static void nbiot2_config_device_for_example ( void )
{
#if defined( NBIOT2_TCP_UDP_EXAMPLE )
    nbiot2_config_device_for_tcp_udp( );
#endif
#if defined( NBIOT2_SMS_EXAMPLE )
    nbiot2_config_device_for_network( );
    for( ; ; )
    {
        nbiot2_check_connection_to_network( );
        if ( CONFIGURATION_FOR_EXAMPLE != app_connection_status )
        {
            break;
        }
    }
#endif 
}

static void nbiot2_check_connection( void )
{
    #define CONNECTED "+CGATT: 1"

    if ( strstr( app_buf, CONNECTED ) != 0 )
    {
        app_connection_status = EXAMPLE;
    }
}

static err_t nbiot2_check_response ( void )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 100000;
    err_t error_flag;
    while ( ( 0 == strstr( app_buf, NBIOT2_RSP_OK ) ) && 
            ( 0 == strstr( app_buf, NBIOT2_RSP_ERROR ) ) )
    {
        error_flag = nbiot2_process( );
        if ( ( NBIOT2_OK != error_flag ) && ( NBIOT2_ERROR != error_flag ) )
        {
            return error_flag;
        }
        if ( timeout_cnt++ > timeout )
        {
            nbiot2_clear_app_buf( );
            return NBIOT2_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    if ( strstr( app_buf, NBIOT2_RSP_OK ) )
    {
        return NBIOT2_OK;
    }
    else if ( strstr( app_buf, NBIOT2_RSP_ERROR ) )
    {
        return NBIOT2_ERROR;
    }
    else
    {
        return NBIOT2_ERROR;
    }
}

static void nbiot2_log_response ( void )
{    
    if ( app_buf_len > 0 ) 
    {
        log_printf( &logger, "%s", app_buf );
        nbiot2_check_connection();
    }
    log_printf( &logger, "-----------------------------------\r\n" );
}

static void nbiot2_error_check( err_t error_flag )
{
    switch ( error_flag )
    {
        case NBIOT2_OK:
        {
            nbiot2_log_response( );
            break;
        }
        case NBIOT2_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case NBIOT2_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    nbiot2_clear_app_buf(  );
    Delay_ms( 500 );
}

static void nbiot2_config_device_for_network( void )
{
    nbiot2_send_cmd( &nbiot2, NBIOT2_CMD_CIMI );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_set_apn( &nbiot2, SIM_APN );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CREG, "2" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
}

static void nbiot2_check_connection_to_network( void )
{
    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_CGATT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_CEREG );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    nbiot2_send_cmd_check( &nbiot2, NBIOT2_CMD_COPS );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    if ( CONFIGURATION_FOR_EXAMPLE != app_connection_status )
    {
        log_info( &logger, "CONNECTED TO NETWORK" );
    }
}

static void nbiot2_send_sms ( void )
{
#if defined( SMS_TEXT_MODE )
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CMGF, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  

    log_printf( &logger, "> Sending message to phone number...\r\n" );
    nbiot2_send_sms_text_mode( &nbiot2, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    if ( NBIOT2_OK != app_error_flag )
    {
        log_printf( &logger, "> Message sent...\r\n" );
        Delay_ms( 10000 );
    }
#elif defined( SMS_PDU_MODE )
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CMGF, "0" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    nbiot2_send_sms_pdu ( &nbiot2, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    if ( NBIOT2_OK != app_error_flag )
    {
        log_printf( &logger, "> Message sent...\r\n" );
        Delay_ms( 10000 );
    }
#endif
}

static void nbiot2_config_device_for_tcp_udp ( void )
{
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_CGATT, "1" );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    
    if ( NBIOT2_OK == app_error_flag )
    {
        app_connection_status = EXAMPLE;
    }
}

static void nbiot2_tcp_udp_example ( void )
{
    #define CONTEXTID       "1"
    #define TCP_PROTOCOL    "0"
    #define UDP_PROTOCOL    "1"
    #define URC_READ_SOCKET_DATA "+QIURC: \"recv\""
    
    char cmd_buf[ 100 ] = { 0 };
    uint16_t timeout_cnt = 0;
    uint16_t timeout = 30000;
    
    // 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 );
    
    // Create TCP socket
    strcpy( cmd_buf, CONTEXTID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, TCP_PROTOCOL );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, "TCP" );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "," ); 
    strcat( cmd_buf, REMOTE_PORT ); 
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIOPEN, cmd_buf );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    Delay_ms( 2000 );
    
    if ( NBIOT2_OK == app_error_flag )
    {
        log_info( &logger, "SENDING MESSAGES TO TCP" );
        // Write message to TCP socket
        strcpy( cmd_buf, TCP_PROTOCOL );
        strcat( cmd_buf, "," );
        strcat( cmd_buf, message_len_buf );
        strcat( cmd_buf, ",\"" );
        strcat( cmd_buf, MESSAGE_CONTENT );
        strcat( cmd_buf, "\"" );
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QISEND, cmd_buf );
    
        // Read response message from TCP socket
        for ( ; ; )
        {
            nbiot2_process( );
            uint8_t * __generic_ptr start_response_buf = strstr( app_buf, URC_READ_SOCKET_DATA );
            if ( start_response_buf )
            {
                log_info( &logger, "RECEIVED MESSAGE" );
                strcpy( cmd_buf, TCP_PROTOCOL );
                strcat( cmd_buf, "," );
                strcat( cmd_buf, "256" );
                nbiot2_clear_app_buf( );
                nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIRD, cmd_buf );
                app_error_flag = nbiot2_check_response( );
                nbiot2_error_check( app_error_flag );  
            
                if ( NBIOT2_OK == app_error_flag )
                {
                    break;
                }
            }
            if ( timeout_cnt++ > timeout )
            {
                log_error( &logger, "TIMEOUT!!!" );
                break;
            }   
            Delay_ms( 1 );
        }
        timeout_cnt = 0;
        Delay_ms( 2000 );
        
        // Close TCP socket
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QICLOSE, TCP_PROTOCOL );
        app_error_flag = nbiot2_check_response( );
        nbiot2_error_check( app_error_flag );  
    }
    else
    {
        log_error( &logger, "Not connected to the TCP server" );
    }
    
    // Create UDP socket
    strcpy( cmd_buf, CONTEXTID );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, UDP_PROTOCOL );
    strcat( cmd_buf, ",\"" );
    strcat( cmd_buf, "UDP" );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "," ); 
    strcat( cmd_buf, REMOTE_PORT ); 
    
    nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIOPEN, cmd_buf );
    app_error_flag = nbiot2_check_response( );
    nbiot2_error_check( app_error_flag );  
    Delay_ms( 2000 );
    
    if ( NBIOT2_OK == app_error_flag )
    {
        log_info( &logger, "SENDING MESSAGES TO UDP" );
        // Write message to UDP socket
        strcpy( cmd_buf, UDP_PROTOCOL );
        strcat( cmd_buf, "," );
        strcat( cmd_buf, message_len_buf );
        strcat( cmd_buf, ",\"" );
        strcat( cmd_buf, MESSAGE_CONTENT );
        strcat( cmd_buf, "\"" );
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QISEND, cmd_buf );
    
        // Read response message from UDP socket
        for ( ; ; )
        {
            nbiot2_process( );
            uint8_t * __generic_ptr start_response_buf = strstr( app_buf, URC_READ_SOCKET_DATA );
            if ( start_response_buf )
            {
                log_info( &logger, "RECEIVED MESSAGE" );
                strcpy( cmd_buf, UDP_PROTOCOL );
                strcat( cmd_buf, "," );
                strcat( cmd_buf, "256" );
                nbiot2_clear_app_buf( );
                nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QIRD, cmd_buf );
                app_error_flag = nbiot2_check_response( );
                nbiot2_error_check( app_error_flag );  
            
                if ( NBIOT2_OK == app_error_flag )
                {
                    break;
                }
            }
            if ( timeout_cnt++ > timeout )
            {
                log_error( &logger, "TIMEOUT!!!" );
                break;
            }   
            Delay_ms( 1 );
        }
        timeout_cnt = 0;
        Delay_ms( 2000 );
        
        // Close UDP socket
        nbiot2_send_cmd_with_parameter( &nbiot2, NBIOT2_CMD_QICLOSE, UDP_PROTOCOL );
        app_error_flag = nbiot2_check_response( );
        nbiot2_error_check( app_error_flag );  
    }
    else
    {
        log_error( &logger, "Not connected to the UDP server" );
    }
    
    Delay_ms( 5000 );
}




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

额外支持

资源

喜欢这个项目吗?

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