中级
30 分钟

使用C1-RM和ATmega644P将物联网变得更智能、更高效

突破连接限制

NB IoT 4 Click with EasyAVR v7

已发布 6月 24, 2024

点击板

NB IoT 4 Click

开发板

EasyAVR v7

编译器

NECTO Studio

微控制器单元

ATmega644P

通过采用NB-IoT,加入无缝的物联网革命,见证它如何将物联网领域转变为一个更智能、更高效的世界。

A

A

硬件概览

它是如何工作的?

NB IoT 4 Click基于C1-RM,这是一款带有2G回退选项的LTE CAT NB-IoT智能蜂窝模块,基于3GPP Release 13,配备Cavli Wireless的集成eSIM功能,提供全球数据连接。它支持广泛的频段,如NB-IoT: B3/B5/B8/B20/B28和GPRS: GSM850/EGSM900/DCS1800/PCS1900,支持自动搜索频段和通过AT命令选择频段。它还提供了多种接口和协议栈,如UDP/TCP/CoAP/LWM2M等,支持使用NB技术进行数据和短信传输,是构建各种物联网解决方案的理想选择。该模块适用于NB-IoT覆盖率低于100%或即将推出NB-IoT网络的国家,在这些国家可以在2G中部署LPWAN,并在网络准备好后切换到NB-IoT。集成的eSIM功能确保了模块可以在全球范围内部署。C1-RM通过UART接口与MCU通

信,默认使用常用的UART RX和TX引脚作为通信协议,以115200 bps的默认配置与主机MCU传输和交换数据。它还配备了USB type C连接器,使模块可以通过FT230X由个人电脑(PC)供电和配置,FT230X是一个紧凑的USB到串行UART接口桥,设计用于高效操作与USB主控制器。借助FT230X,可以访问C1-RM的调试串行端口,以升级固件和检查日志信息。它还具有RX/TX蓝色LED指示灯,指示桥接是否处于RX或TX模式。用户还可以使用其他接口,如SPI或I2C,自己配置模块和编写库。RI引脚路由到mikroBUS™的INT引脚,代表外部中断引脚,用于从省电模式唤醒设备,而mikroBUS™插座上的RST引脚可以通过将该引脚置于逻辑低状态来执行硬件重置功能。在这些引脚旁边,这款Click board™还提供了一个标

有N/I的白色LED指示灯,以指示网络通信状态,以及一个路由到mikroBUS™插座的AN引脚的模拟数字转换引脚,可以实现外部温度监控并通过AT命令读取电压。NB IoT 4 Click配有阻抗为50Ω的SMA天线连接器,用于连接MIKROE提供的合适天线。除了NB IoT SMA连接器,这款Click board™还具有一个Nano-SIM卡槽,提供多种连接和接口选项。这款Click board™可以与3.3V和5V的MCU一起工作。适当的电压电平转换器TXS0108E执行适当的逻辑电压电平转换,而板载LDO TPS7A7002确保推荐的电压电平为模块供电。此外,这款Click board™配备了一个包含易于使用的功能和示例代码的库,可作为进一步开发的参考。

NB IoT 4 Click hardware overview image

功能概述

开发板

EasyAVR v7 是第七代AVR开发板,专为快速开发嵌入式应用的需求而设计。它支持广泛的16位AVR微控制器,来自Microchip,并具有一系列独特功能,如强大的板载mikroProg程序员和通过USB的在线电路调试器。开发板布局合理,设计周到,使得最终用户可以在一个地方找到所有必要的元素,如开关、按钮、指示灯、连接器等。EasyAVR v7 通过每个端口的四种不同连接器,比以往更高效地连接附件板、传感器和自定义电子产品。EasyAVR v7 开发板的每个部分

都包含了使同一板块运行最高效的必要组件。一个集成的mikroProg,一个快速的USB 2.0程序员,带有mikroICD硬件在线电路调试器,提供许多有价值的编 程/调试选项和与Mikroe软件环境的无缝集成。除此之外,它还包括一个干净且调节过的开发板电源供应模块。它可以使用广泛的外部电源,包括外部12V电源供应,7-12V交流或9-15V直流通过DC连接器/螺丝端子,以及通过USB Type-B(USB-B)连接器的电源。通信选项如USB-UART和RS-232也包括在内,与

广受好评的mikroBUS™标准、三种显示选项(7段、图形和基于字符的LCD)和几种不同的DIP插座一起,覆盖了广泛的16位AVR MCU。EasyAVR v7 是Mikroe快速开发生态系统的一个组成部分。它由Mikroe软件工具原生支持,得益于大量不同的Click板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作和开发的许多方面。

EasyAVR v7 horizontal image

微控制器概述 

MCU卡片 / MCU

建筑

AVR

MCU 内存 (KB)

64

硅供应商

Microchip

引脚数

40

RAM (字节)

4096

你完善了我!

配件

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

NB IoT 4 Click accessories image

使用的MCU引脚

mikroBUS™映射器

Analog Output
PA7
AN
Reset
PA6
RST
SPI Chip Select
PA5
CS
SPI Clock
PB7
SCK
SPI Data OUT
PB6
MISO
SPI Data IN
PB5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Wake-Up Interrupt
PD2
INT
UART TX
PD1
TX
UART RX
PD0
RX
I2C Clock
PC0
SCL
I2C Data
PC1
SDA
Power Supply
5V
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

NB IoT 4 Click Schematic schematic

一步一步来

项目组装

EasyAVR v7 front image hardware assembly

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

EasyAVR v7 front image hardware assembly
GNSS2 Click front image hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyAVR v7 Access DIP MB 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
EasyPIC PRO v7a Display Selection Necto Step 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 4 Click 驱动程序的 API。

关键功能:

  • nbiot4_set_sim_apn - 该功能设置SIM卡的APN

  • nbiot4_send_sms_text - 该功能向电话号码发送短信

  • nbiot4_send_sms_pdu - 该功能以PDU模式向电话号码发送短信

开源

代码示例

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

/*!
 * @file main.c
 * @brief NB IoT 4 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, tests the communication by sending "AT" command, and after that restarts the device.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - NBIOT4_CONFIGURE_FOR_NETWORK:
 * Sets configuration to device to be able to connect to the network.
 *
 *  - NBIOT4_WAIT_FOR_CONNECTION:
 * Waits for the network registration indicated via CEREG URC event and then checks
 * the connection status.
 *
 *  - NBIOT4_CONFIGURE_FOR_EXAMPLE:
 * Sets the device configuration for sending SMS or TCP/UDP messages depending on the selected demo example.
 *
 *  - NBIOT4_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 nbiot4_clear_app_buf ( void )
 * - static err_t nbiot4_process ( void )
 * - static void nbiot4_error_check( err_t error_flag )
 * - static void nbiot4_log_app_buf ( void )
 * - static err_t nbiot4_rsp_check ( uint8_t *rsp )
 * - static err_t nbiot4_configure_for_connection( void )
 * - static err_t nbiot4_check_connection( void )
 * - static err_t nbiot4_configure_for_messages( void )
 * - static err_t nbiot4_send_message( 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 (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 "nbiot4.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                             "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_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                     "NB IoT 4 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
{
    NBIOT4_CONFIGURE_FOR_NETWORK = 1,
    NBIOT4_WAIT_FOR_CONNECTION,
    NBIOT4_CONFIGURE_FOR_EXAMPLE,
    NBIOT4_EXAMPLE

} nbiot4_example_state_t;

static nbiot4_t nbiot4;
static log_t logger;

/**
 * @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 err_t error_flag;
static nbiot4_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 nbiot4_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 nbiot4_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.
 * @param[in] error_flag  Error flag to check.
 */
static void nbiot4_error_check ( err_t error_flag );

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

/**
 * @brief Response check.
 * @details This function checks for response and
 * returns the status of response.
 * @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.
 */
static err_t nbiot4_rsp_check ( uint8_t *rsp );

/**
 * @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 nbiot4_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 nbiot4_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 nbiot4_configure_for_example ( void );

/**
 * @brief Execute example.
 * @details This function executes SMS or TCP/UDP 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 nbiot4_example ( void );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nbiot4_cfg_t nbiot4_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.
    nbiot4_cfg_setup( &nbiot4_cfg );
    NBIOT4_MAP_MIKROBUS( nbiot4_cfg, MIKROBUS_1 );
    if ( UART_ERROR == nbiot4_init( &nbiot4, &nbiot4_cfg ) )
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );
        for ( ; ; );
    }
    
    nbiot4_process( );
    nbiot4_clear_app_buf( );

    // Check communication
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_AT );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    nbiot4_error_check( error_flag );
    
    // Enable command echo
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_ATE1 );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    nbiot4_error_check( error_flag );
    
    // Restart device
    #define RESTART_DEVICE "1,1"
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CFUN, RESTART_DEVICE );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    nbiot4_error_check( error_flag );
    
    log_info( &logger, " Application Task " );
    example_state = NBIOT4_CONFIGURE_FOR_NETWORK;
}

void application_task ( void )
{
    switch ( example_state )
    {
        case NBIOT4_CONFIGURE_FOR_NETWORK:
        {
            if ( NBIOT4_OK == nbiot4_configure_for_network( ) )
            {
                example_state = NBIOT4_WAIT_FOR_CONNECTION;
            }
            break;
        }
        case NBIOT4_WAIT_FOR_CONNECTION:
        {
            if ( NBIOT4_OK == nbiot4_check_connection( ) )
            {
                example_state = NBIOT4_CONFIGURE_FOR_EXAMPLE;
            }
            break;
        }
        case NBIOT4_CONFIGURE_FOR_EXAMPLE:
        {
            if ( NBIOT4_OK == nbiot4_configure_for_example( ) )
            {
                example_state = NBIOT4_EXAMPLE;
            }
            break;
        }
        case NBIOT4_EXAMPLE:
        {
            nbiot4_example( );
            break;
        }
        default:
        {
            log_error( &logger, " Example 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 nbiot4_clear_app_buf ( void )
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static err_t nbiot4_process ( void )
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t rx_size = 0;
    rx_size = nbiot4_generic_read( &nbiot4, rx_buf, PROCESS_BUFFER_SIZE );
    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = app_buf_len;
        if ( ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) && ( app_buf_len > 0 ) ) 
        {
            buf_cnt = APP_BUFFER_SIZE - ( ( app_buf_len + rx_size ) - APP_BUFFER_SIZE );
            memmove ( app_buf, &app_buf[ APP_BUFFER_SIZE - buf_cnt ], buf_cnt );
        }
        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ buf_cnt++ ] = rx_buf[ rx_cnt ];
                if ( app_buf_len < APP_BUFFER_SIZE )
                {
                    app_buf_len++;
                }
            }
        }
        return NBIOT4_OK;
    }
    return NBIOT4_ERROR;
}

static err_t nbiot4_rsp_check ( uint8_t *rsp )
{
    uint32_t timeout_cnt = 0;
    uint32_t timeout = 120000;
    nbiot4_clear_app_buf( );
    nbiot4_process( );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, NBIOT4_RSP_ERROR ) ) )
    {
        nbiot4_process( );
        if ( timeout_cnt++ > timeout )
        {
            nbiot4_clear_app_buf( );
            return NBIOT4_ERROR_TIMEOUT;
        }
        Delay_ms ( 1 );
    }
    Delay_ms ( 100 );
    nbiot4_process( );
    if ( strstr( app_buf, rsp ) )
    {
        return NBIOT4_OK;
    }
    else if ( strstr( app_buf, NBIOT4_RSP_ERROR ) )
    {
        return NBIOT4_ERROR_CMD;
    }
    else
    {
        return NBIOT4_ERROR_UNKNOWN;
    }
}

static void nbiot4_error_check ( err_t error_flag )
{
    switch ( error_flag )
    {
        case NBIOT4_OK:
        {
            nbiot4_log_app_buf( );
            break;
        }
        case NBIOT4_ERROR:
        {
            log_error( &logger, " Overflow!" );
            break;
        }
        case NBIOT4_ERROR_TIMEOUT:
        {
            log_error( &logger, " Timeout!" );
            break;
        }
        case NBIOT4_ERROR_CMD:
        {
            log_error( &logger, " CMD!" );
            break;
        }
        case NBIOT4_ERROR_UNKNOWN:
        default:
        {
            log_error( &logger, " Unknown!" );
            break;
        }
    }
    Delay_ms ( 500 );
}

static void nbiot4_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 nbiot4_configure_for_network ( void )
{
    err_t func_error = NBIOT4_OK;
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    
    // Enable extern SIM card channel
    #define ENABLE_EXTERN_SIM "1"
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_SIMSWAP, ENABLE_EXTERN_SIM );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    
    // Restart the device in order for the change in SIM channel to take effect
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_TRB );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_REBOOTING );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    
    // Enable command echo
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_ATE1 );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    nbiot4_error_check( error_flag );
    
    // Set SIM APN
    nbiot4_set_sim_apn( &nbiot4, SIM_APN );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );

    // Enable full functionality
    #define FULL_FUNCTIONALITY "1"
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CFUN, FULL_FUNCTIONALITY );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    
    #define ENABLE_EPS_REG "2"
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CEREG, ENABLE_EPS_REG );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
#endif
    return func_error;
}

static err_t nbiot4_check_connection ( void )
{
#if ( ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP ) || ( DEMO_EXAMPLE == EXAMPLE_SMS ) )
    #define CONNECTED "+CEREG: 2,1"
    nbiot4_send_cmd_check ( &nbiot4, NBIOT4_CMD_CEREG );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    nbiot4_error_check( error_flag );
    if ( strstr( app_buf, CONNECTED ) )
    {
        Delay_ms ( 100 );
        // Check signal quality
        nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_CSQ );
        error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
        nbiot4_error_check( error_flag );
        #define NO_SIGNAL "99,99"
        if ( !strstr( app_buf, NO_SIGNAL ) )
        {
            Delay_ms ( 1000 );
            return error_flag;
        }
    }
    Delay_ms ( 1000 );
    return NBIOT4_ERROR;
#endif
    return NBIOT4_OK;
}

static err_t nbiot4_configure_for_example ( void )
{
    err_t func_error = NBIOT4_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    #define ENABLE_RESPONSE_HEADER "1"
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CIPHEAD, ENABLE_RESPONSE_HEADER );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
#elif ( DEMO_EXAMPLE == EXAMPLE_SMS )
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CMGF, SMS_MODE );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

static err_t nbiot4_example ( void )
{
    err_t func_error = NBIOT4_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_TCP_UDP )
    uint8_t cmd_buf[ 100 ] = { 0 };

    // Open TCP socket.
    #define RESPONSE_CONNECT "CONNECT OK"
    #define TCP_SERVICE_TYPE "\"TCP\","
    strcpy( cmd_buf, TCP_SERVICE_TYPE );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CIPSTART, cmd_buf );
    error_flag = nbiot4_rsp_check( RESPONSE_CONNECT );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );

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

    // Write message to TCP socket
    uint8_t ctrl_z = 0x1A;
    strcpy( cmd_buf, message_len_buf );
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CIPSEND, cmd_buf );
    error_flag = nbiot4_rsp_check( ">" );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    nbiot4_generic_write ( &nbiot4, MESSAGE_CONTENT, message_len );
    nbiot4_generic_write ( &nbiot4, &ctrl_z, 1 );
    
    // Read response
    #define RESPONSE_URC "+IPD"
    error_flag = nbiot4_rsp_check( RESPONSE_URC );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    log_printf( &logger, "\r\n" );
    
    // Close TCP socket
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_CIPCLOSE );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    
    // Open UDP socket.
    #define UDP_SERVICE_TYPE "\"UDP\","
    strcpy( cmd_buf, UDP_SERVICE_TYPE );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, REMOTE_IP );
    strcat( cmd_buf, "\"" );
    strcat( cmd_buf, "," );
    strcat( cmd_buf, REMOTE_PORT );
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CIPSTART, cmd_buf );
    error_flag = nbiot4_rsp_check( RESPONSE_CONNECT );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    
    // Write message to UDP socket
    strcpy( cmd_buf, message_len_buf );
    nbiot4_send_cmd_with_par( &nbiot4, NBIOT4_CMD_CIPSEND, cmd_buf );
    error_flag = nbiot4_rsp_check( ">" );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    nbiot4_generic_write ( &nbiot4, MESSAGE_CONTENT, message_len );
    nbiot4_generic_write ( &nbiot4, &ctrl_z, 1 );
    
    // Read response
    error_flag = nbiot4_rsp_check( RESPONSE_URC );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    log_printf( &logger, "\r\n" );
    
    // Close UDP socket
    nbiot4_send_cmd( &nbiot4, NBIOT4_CMD_CIPCLOSE );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    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"
    nbiot4_send_cmd_check( &nbiot4, NBIOT4_CMD_CMGF );
    error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
    func_error |= error_flag;
    nbiot4_error_check( error_flag );
    if ( strstr( app_buf, CMGF_PDU ) )
    {
        // Send SMS in PDU mode
        nbiot4_send_sms_pdu( &nbiot4, SIM_SMSC, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
        func_error |= error_flag;
        nbiot4_error_check( error_flag );
    }
    else if ( strstr( app_buf, CMGF_TXT ) )
    {
        // Send SMS in TXT mode
        nbiot4_send_sms_text ( &nbiot4, PHONE_NUMBER_TO_MESSAGE, MESSAGE_CONTENT );
        error_flag = nbiot4_rsp_check( NBIOT4_RSP_OK );
        func_error |= error_flag;
        nbiot4_error_check( error_flag );
    }
    // 30 seconds delay
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
#else
    #error "No demo example selected"
#endif
    return func_error;
}

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

额外支持

资源

喜欢这个项目吗?

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