为智慧城市、农业及工业监测系统增添超低功耗蜂窝连接能力
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 还配备了配套软件库,提供易用的函数和示例代码,可作为后续开发的参考。
功能概述
开发板
Curiosity PIC32 MZ EF 开发板是一个完全集成的 32 位开发平台,特点是高性能的 PIC32MZ EF 系列(PIC32MZ2048EFM),该系列具有 2MB Flash、512KB RAM、集成的浮点单元(FPU)、加密加速器和出色的连接选项。它包括一个集成的程序员和调试器,无需额外硬件。用户可以通过 MIKROE
mikroBUS™ Click™ 适配器板扩展功能,通过 Microchip PHY 女儿板添加以太网连接功能,使用 Microchip 扩展板添加 WiFi 连接能力,并通过 Microchip 音频女儿板添加音频输入和输出功能。这些板完全集成到 PIC32 强大的软件框架 MPLAB Harmony 中,该框架提供了一个灵活且模块化的接口
来应用开发、一套丰富的互操作软件堆栈(TCP-IP、USB)和易于使用的功能。Curiosity PIC32 MZ EF 开发板提供了扩展能力,使其成为连接性、物联网和通用应用中快速原型设计的绝佳选择。
微控制器概述
MCU卡片 / MCU

建筑
PIC32
MCU 内存 (KB)
2048
硅供应商
Microchip
引脚数
100
RAM (字节)
524288
使用的MCU引脚
mikroBUS™映射器
“仔细看看!”
Click board™ 原理图

一步一步来
项目组装
软件支持
库描述
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
额外支持
资源
类别:LTE 物联网
































