通过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板™配备了一个库,包含函数和示例代码,可作为进一步开发的参考。
功能概述
开发板
Nucleo-64 搭载 STM32G474R 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 板在他们的项目中的能力。
微控制器概述
MCU卡片 / MCU

建筑
ARM Cortex-M4
MCU 内存 (KB)
512
硅供应商
STMicroelectronics
引脚数
64
RAM (字节)
128k
你完善了我!
配件
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™。
使用的MCU引脚
mikroBUS™映射器
“仔细看看!”
Click board™ 原理图

一步一步来
项目组装
实时跟踪您的结果
应用程序输出
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