添加双WiFi和蓝牙® LE 5连接,非常适用于物联网和智能家居应用
A
A
硬件概览
它是如何工作的?
Stephano-I Click 基于 Würth Elektronik 的 Stephano-I(2617011025000)无线电模块,专为实现 WiFi 和 Bluetooth® LE 功能而设计。该模块支持 2.4GHz 频段的 IEEE 802.11 b/g/n 标准,并具备 Bluetooth® LE 5 的外围设备与中心设备角色,确保与各种智能设备进行可靠的低功耗通信。其双射频支持结合基于 UART 的 AT 指令接口,使其成为多种无线 IoT 应用的理想选择。WiFi 接口的数据速率为 1Mbps,最大输出功率为 13.4dBm,接收灵敏度为 -87dBm,频率范围为 2412 至 2484MHz;Bluetooth® LE 接口最大输出功率为 4.5dBm,灵敏度为 -89dBm,频率范围为 2402 至 2480MHz。
Stephano-I 模块与主控 MCU 之间通过 UART 接口进行通信,使用标准 UART RX 和 TX 引脚,以及 CTS/RTS 硬件流控制引脚。默认通信速率为 115200bps,确保高效数据传输。模块可通过 AT 指令轻松配置,配置完成后可自动管理无线连接,从而释放主控系统的处理资源。除了通信引脚外,该板还配有 RST 引脚和 RESET 按钮,用于对模块进行硬复位,WUP 引脚则可用于唤醒模块退出休眠模式。此外,Stephano-I Click 板上还设计有多个未焊接的扩展引脚,供高级用户使用。DEBUG 区域的引脚为可选的调试 UART 接口和固件烧录控制接口,主要用于终端设备认证过程。Stephano-I 模块上的调试 UART
接口允许开发者加载测试固件并在开发与验证过程中运行测试模式。另一组未焊接引脚用于引导控制功能,包括 CT0 和 CT1 引脚,在复位期间设定为特定逻辑电平即可触发模块进入 Bootloader 模式,用于固件更新。此外,还包含 IO3 引脚,作为通用 GPIO 可用,也可配置为模拟输入,实现唤醒源或模数采集功能,进一步扩展模块在低功耗与数据采集方面的应用灵活性。此 Click board™ 仅支持 3.3V 逻辑电平。若主控 MCU 使用不同逻辑电平,需进行电平转换。此外,该板还配套提供了包含易用函数和示例代码的软件库,可作为进一步开发的参考。
功能概述
开发板
UNI-DS v8 是一款专为快速开发嵌入式应用的需求而特别设计的开发板。它支持广泛的微控制器,如各种 STM32、Kinetis、TIVA、CEC、MSP、PIC、dsPIC、PIC32 和 AVR MCU,无论它们的引脚数量如何,并且具有一系列独特功能,例如首次通过 WiFi 的嵌入式调试器/程序员。开发板布局合理,设计周到,使得最终用户可以在一个地方找到所有必要的元素,如开关、按钮、指示灯、连接器等。得益于创新的制造技术,UNI-DS v8 提供了流畅而沉浸式的工作 体验,允许在任何情况下、任何地方、任何时候都能
访问。UNI-DS v8 开发板的每个部分都包含了使同一板块运行最高效的必要组件。一个先进的集成CODEGRIP程序/调试模块提供许多有价值的编程/调 试选项,包括对 JTAG、SWD 和 SWO Trace(单线输出)的支持,并与 Mikroe 软件环境无缝集成。此外,它还包括一个干净且调节过的开发板电源供应模块。它可以使用广泛的外部电源,包括电池、外部 12V 电源供应和通过 USB Type-C(USB-C)连接器 的电源。通信选项如 USB-UART、USB HOST/DEVICE、CAN(如果 MCU 卡支持的话)和
以太网也包括在内。此外,它还拥有广受好评的 mikroBUS™ 标准,为 MCU 卡提供了标准化插座(SiBRAIN 标准),以及两种显示选项,用于 TFT 板线产品和基于字符的 LCD。UNI-DS v8 是 Mikroe 快速开发生态系统的一个组成部分。它由 Mikroe 软件工具原生支持,得益于大量不同的 Click 板™(超过一千块板),其数量每天都在增长,它涵盖了原型制作和开发的许多方面。
微控制器概述
MCU卡片 / MCU

类型
8th Generation
建筑
ARM Cortex-M4
MCU 内存 (KB)
256
硅供应商
Texas Instruments
引脚数
100
RAM (字节)
65536
使用的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”替换为要显示的参数。

软件支持
库描述
Stephano-I Click 演示应用程序使用 NECTO Studio开发,确保与 mikroSDK 的开源库和工具兼容。该演示设计为即插即用,可与所有具有 mikroBUS™ 插座的 开发板、入门板和 mikromedia 板完全兼容,用于快速实现和测试。
示例描述
该应用示例展示了设备连接至 WiFi 网络并向回显服务器发送 TCP/UDP 消息,或处理来自连接的 BLE 设备数据的能力。
关键功能:
stephanoi_cfg_setup
- 初始化 Click 配置结构为默认值的函数。stephanoi_init
- 初始化所有用于该 Click 板的引脚和外设的函数。stephanoi_cmd_run
- 向 Click 模块发送指定命令的函数。stephanoi_cmd_set
- 向 Click 模块指定命令设置值的函数。stephanoi_cmd_get
- 从 Click 模块获取指定命令值的函数。
应用初始化
初始化驱动程序和日志记录器。
应用任务
应用任务分为几个阶段:
STEPHANOI_POWER_UP:
为设备上电,执行出厂重置并读取系统信息。STEPHANOI_CONFIGURE_CONNECTION:
根据所选示例配置 WiFi 或 BLE 连接。STEPHANOI_EXAMPLE:
根据所选的演示示例,通过 WiFi 网络向回显服务器发送 TCP/UDP 消息,或处理来自连接 BLE 设备的所有数据,并返回相应的响应消息。
默认选择 WiFi TCP/UDP 示例。
开源
代码示例
完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio。 应用程序代码也可以在MIKROE的GitHub账户中找到。
/*!
* @file main.c
* @brief Stephano-I Click Example.
*
* # Description
* Application example shows device capability of connecting to a WiFi network and
* sending TCP/UDP messages to an echo server, or processing data from a connected BLE device.
*
* The demo application is composed of two sections :
*
* ## Application Init
* Initializes the driver and logger.
*
* ## Application Task
* Application task is split in few stages:
* - STEPHANOI_POWER_UP:
* Powers up the device, performs a factory reset and reads system information.
*
* - STEPHANOI_CONFIGURE_CONNECTION:
* Configures connection to WiFi or BLE depending on the selected example.
*
* - STEPHANOI_EXAMPLE:
* Depending on the selected demo example, it sends a TCP/UDP message to an echo server over a WiFi network or
* processes all data from a connected BLE device and sends back an adequate response message.
*
* By default, the WiFi TCP/UDP example is selected.
*
* ## Additional Function
* - static void stephanoi_clear_app_buf ( void )
* - static void stephanoi_log_app_buf ( void )
* - static err_t stephanoi_process ( stephanoi_t *ctx )
* - static err_t stephanoi_read_response ( stephanoi_t *ctx, uint8_t *rsp )
* - static err_t stephanoi_power_up ( stephanoi_t *ctx )
* - static err_t stephanoi_config_connection ( stephanoi_t *ctx )
* - static err_t stephanoi_example ( stephanoi_t *ctx )
*
* @note
* We have used the Serial Bluetooth Terminal smartphone application for the BLE example test.
*
* @author Stefan Filipovic
*
*/
#include "board.h"
#include "log.h"
#include "stephanoi.h"
#include "conversions.h"
#include "generic_pointer.h"
// Example selection macros
#define EXAMPLE_WIFI 0 // Example of sending messages to a TCP/UDP echo server over WiFi
#define EXAMPLE_BLE 1 // Example of processing data from BLE connected device
#define DEMO_EXAMPLE EXAMPLE_WIFI // Example selection macro
// WiFi credentials
#define WIFI_SSID "MikroE Public"
#define WIFI_PASSWORD "mikroe.guest"
// WiFi 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
// BLE device name
#define BLE_NAME "Stephano-I Click"
// Message content
#define MESSAGE_CONTENT "Stephano-I Click board - demo example."
// Application buffer size
#define APP_BUFFER_SIZE 600
#define PROCESS_BUFFER_SIZE 200
/**
* @brief Example states.
* @details Predefined enum values for application example state.
*/
typedef enum
{
STEPHANOI_POWER_UP = 1,
STEPHANOI_CONFIGURE_CONNECTION,
STEPHANOI_EXAMPLE
} stephanoi_app_state_t;
static stephanoi_t stephanoi;
static log_t logger;
static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static stephanoi_app_state_t app_state = STEPHANOI_POWER_UP;
/**
* @brief Stephano-I clearing application buffer.
* @details This function clears memory of application buffer and reset its length.
* @note None.
*/
static void stephanoi_clear_app_buf ( void );
/**
* @brief Stephano-I log application buffer.
* @details This function logs data from application buffer to USB UART.
* @note None.
*/
static void stephanoi_log_app_buf ( void );
/**
* @brief Stephano-I data reading function.
* @details This function reads data from device and concatenates data to application buffer.
* @param[in] ctx : Click context object.
* See #stephanoi_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 stephanoi_process ( stephanoi_t *ctx );
/**
* @brief Stephano-I 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 #stephanoi_t object definition for detailed explanation.
* @param[in] rsp Expected response.
* @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 stephanoi_read_response ( stephanoi_t *ctx, uint8_t *rsp );
/**
* @brief Stephano-I power up function.
* @details This function powers up the device, performs a factory reset and reads system information.
* @param[in] ctx : Click context object.
* See #stephanoi_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 stephanoi_power_up ( stephanoi_t *ctx );
/**
* @brief Stephano-I config connection function.
* @details This function configures connection to WiFi or BLE depending on the selected example.
* @param[in] ctx : Click context object.
* See #stephanoi_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 stephanoi_config_connection ( stephanoi_t *ctx );
/**
* @brief Stephano-I example function.
* @details This function performs a WiFi TCP/UDP or a BLE device terminal example.
* @param[in] ctx : Click context object.
* See #stephanoi_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 stephanoi_example ( stephanoi_t *ctx );
void application_init ( void )
{
log_cfg_t log_cfg; /**< Logger config object. */
stephanoi_cfg_t stephanoi_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.
stephanoi_cfg_setup( &stephanoi_cfg );
STEPHANOI_MAP_MIKROBUS( stephanoi_cfg, MIKROBUS_1 );
if ( UART_ERROR == stephanoi_init( &stephanoi, &stephanoi_cfg ) )
{
log_error( &logger, " Communication init." );
for ( ; ; );
}
log_info( &logger, " Application Task " );
app_state = STEPHANOI_POWER_UP;
log_printf( &logger, ">>> APP STATE - POWER UP <<<\r\n\n" );
}
void application_task ( void )
{
switch ( app_state )
{
case STEPHANOI_POWER_UP:
{
if ( STEPHANOI_OK == stephanoi_power_up( &stephanoi ) )
{
app_state = STEPHANOI_CONFIGURE_CONNECTION;
log_printf( &logger, ">>> APP STATE - CONFIGURE CONNECTION <<<\r\n\n" );
}
break;
}
case STEPHANOI_CONFIGURE_CONNECTION:
{
if ( STEPHANOI_OK == stephanoi_config_connection( &stephanoi ) )
{
app_state = STEPHANOI_EXAMPLE;
log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
}
break;
}
case STEPHANOI_EXAMPLE:
{
stephanoi_example( &stephanoi );
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 stephanoi_clear_app_buf ( void )
{
memset( app_buf, 0, app_buf_len );
app_buf_len = 0;
}
static void stephanoi_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 stephanoi_process ( stephanoi_t *ctx )
{
uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
int32_t overflow_bytes = 0;
int32_t rx_cnt = 0;
int32_t rx_size = stephanoi_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 STEPHANOI_OK;
}
return STEPHANOI_ERROR;
}
static err_t stephanoi_read_response ( stephanoi_t *ctx, uint8_t *rsp )
{
#define READ_RESPONSE_TIMEOUT_MS 60000
uint32_t timeout_cnt = 0;
stephanoi_clear_app_buf( );
stephanoi_process( ctx );
while ( ( 0 == strstr( app_buf, rsp ) ) &&
( 0 == strstr( app_buf, STEPHANOI_RSP_ERROR ) ) )
{
stephanoi_process( ctx );
if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
{
stephanoi_log_app_buf( );
stephanoi_clear_app_buf( );
log_error( &logger, " Timeout!" );
return STEPHANOI_ERROR_TIMEOUT;
}
Delay_ms( 1 );
}
Delay_ms ( 200 );
stephanoi_process( ctx );
stephanoi_log_app_buf( );
if ( strstr( app_buf, rsp ) )
{
log_printf( &logger, "--------------------------------\r\n" );
return STEPHANOI_OK;
}
return STEPHANOI_ERROR_CMD;
}
static err_t stephanoi_power_up ( stephanoi_t *ctx )
{
err_t error_flag = STEPHANOI_OK;
log_printf( &logger, ">>> Perform device hardware reset.\r\n" );
stephanoi_reset_device ( ctx );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_URC_READY );
log_printf( &logger, ">>> Check communication.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_AT );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Reset to factory settings.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_FACTORY_RESET );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_URC_READY );
log_printf( &logger, ">>> Get device version.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_GET_VERSION );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
return error_flag;
}
static err_t stephanoi_config_connection ( stephanoi_t *ctx )
{
err_t error_flag = STEPHANOI_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_WIFI )
log_printf( &logger, ">>> Set WiFi mode to station.\r\n" );
#define WIFI_MODE_STATION "1"
stephanoi_cmd_set( ctx, STEPHANOI_CMD_SET_WIFI_MODE, WIFI_MODE_STATION );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Connect to WiFi access point.\r\n" );
stephanoi_clear_app_buf( );
strcpy ( app_buf, "\"" );
strcat ( app_buf, WIFI_SSID );
strcat ( app_buf, "\",\"" );
strcat ( app_buf, WIFI_PASSWORD );
strcat ( app_buf, "\"" );
stephanoi_cmd_set( ctx, STEPHANOI_CMD_CONNECT_TO_AP, app_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Enable multiple connections.\r\n" );
#define CONNECTION_MODE_MULTIPLE "1"
stephanoi_cmd_set( ctx, STEPHANOI_CMD_SET_MULTIPLE_CONNECTION_MODE, CONNECTION_MODE_MULTIPLE );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Set socket receiving mode to passive.\r\n" );
#define RECEIVING_MODE_PASSIVE "1"
stephanoi_cmd_set( ctx, STEPHANOI_CMD_SET_SOCKET_RECEIVING_MODE, RECEIVING_MODE_PASSIVE );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
#elif ( DEMO_EXAMPLE == EXAMPLE_BLE )
log_printf( &logger, ">>> Initialize BLE role as server.\r\n" );
#define BLE_INIT_SERVER "2"
stephanoi_cmd_set( ctx, STEPHANOI_CMD_BLE_INIT, BLE_INIT_SERVER );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Set BLE device name.\r\n" );
stephanoi_clear_app_buf( );
strcpy ( app_buf, "\"" );
strcat ( app_buf, BLE_NAME );
strcat ( app_buf, "\"" );
stephanoi_cmd_set( ctx, STEPHANOI_CMD_BLE_NAME, app_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Get BLE address.\r\n" );
stephanoi_cmd_get( ctx, STEPHANOI_CMD_BLE_ADDRESS );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Create GATT server.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_BLE_GATTS_CREATE );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Start GATT server.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_BLE_GATTS_START );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Start BLE advertising.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_BLE_START_ADVERTISING );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Set BLE advertising data.\r\n" );
#define BLE_ADV_DATA_FLAGS "020106"
#define BLE_ADV_DATA_COMPLETE_LOCAL_NAME "09"
uint8_t ble_name[ 32 ] = { 0 };
uint8_t hex_buf[ 3 ] = { 0 };
stephanoi_clear_app_buf( );
strcpy ( app_buf, "\"" );
strcat ( app_buf, BLE_ADV_DATA_FLAGS );
uint8_to_hex( strlen( BLE_NAME ) + 1, hex_buf );
strcat ( app_buf, hex_buf );
strcat ( app_buf, BLE_ADV_DATA_COMPLETE_LOCAL_NAME );
strcpy ( ble_name, BLE_NAME );
for ( int16_t cnt = 0; cnt < strlen( BLE_NAME ); cnt++ )
{
uint8_to_hex ( ble_name[ cnt ], hex_buf );
strcat ( app_buf, hex_buf );
}
strcat ( app_buf, "\"" );
stephanoi_cmd_set( ctx, STEPHANOI_CMD_BLE_SET_ADVERTISING_DATA, app_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
#else
#error "No demo example selected"
#endif
return error_flag;
}
static err_t stephanoi_example ( stephanoi_t *ctx )
{
err_t error_flag = STEPHANOI_OK;
#if ( DEMO_EXAMPLE == EXAMPLE_WIFI )
uint8_t cmd_buf[ 100 ] = { 0 };
log_printf( &logger, ">>> Start TCP connection.\r\n" );
#define TCP_LINK_NUM "0"
#define TCP_CONN_TYPE "TCP"
strcpy( cmd_buf, TCP_LINK_NUM );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, TCP_CONN_TYPE );
strcat( cmd_buf, "\",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_ESTABLISH_TCP_UDP_CONNECTION, cmd_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Start UDP connection.\r\n" );
#define UDP_LINK_NUM "1"
#define UDP_CONN_TYPE "UDP"
strcpy( cmd_buf, UDP_LINK_NUM );
strcat( cmd_buf, ",\"" );
strcat( cmd_buf, UDP_CONN_TYPE );
strcat( cmd_buf, "\",\"" );
strcat( cmd_buf, REMOTE_IP );
strcat( cmd_buf, "\"," );
strcat( cmd_buf, REMOTE_PORT );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_ESTABLISH_TCP_UDP_CONNECTION, cmd_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
// 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, ">>> Write message to TCP connection.\r\n" );
strcpy( cmd_buf, TCP_LINK_NUM );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_SEND_DATA_VIA_CONNECTION, cmd_buf );
Delay_ms ( 100 );
stephanoi_generic_write ( &stephanoi, MESSAGE_CONTENT, message_len );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_URC_RECEIVED_DATA );
log_printf( &logger, ">>> Read response from TCP connection.\r\n" );
strcpy( cmd_buf, TCP_LINK_NUM );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_RECEIVE_DATA_VIA_CONNECTION, cmd_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Write message to UDP connection.\r\n" );
strcpy( cmd_buf, UDP_LINK_NUM );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_SEND_DATA_VIA_CONNECTION, cmd_buf );
Delay_ms ( 100 );
stephanoi_generic_write ( &stephanoi, MESSAGE_CONTENT, message_len );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_URC_RECEIVED_DATA );
log_printf( &logger, ">>> Read response from UDP connection.\r\n" );
strcpy( cmd_buf, UDP_LINK_NUM );
strcat( cmd_buf, "," );
strcat( cmd_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_RECEIVE_DATA_VIA_CONNECTION, cmd_buf );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Close TCP connection.\r\n" );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_CLOSE_TCP_UDP_CONNECTION, TCP_LINK_NUM );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
log_printf( &logger, ">>> Close UDP connection.\r\n" );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_CLOSE_TCP_UDP_CONNECTION, UDP_LINK_NUM );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
Delay_ms ( 1000 );
Delay_ms ( 1000 );
Delay_ms ( 1000 );
Delay_ms ( 1000 );
Delay_ms ( 1000 );
#elif ( DEMO_EXAMPLE == EXAMPLE_BLE )
uint8_t * __generic_ptr urc_buf_ptr = 0;
uint8_t ble_conn_index[ 2 ] = { 0 };
uint8_t message_len_buf[ 10 ] = { 0 };
uint32_t timeout_cnt = 0;
#define BLE_TERMINAL_TIMEOUT_MS 60000
#define BLE_TERMINAL_MESSAGE_FREQ_MS 5000
#define TERMINATION_CMD "END"
#define TERMINATION_RESPONSE "Acknowledged, the connection will be terminated in a few seconds."
#define TERMINATION_TIMEOUT "Timeout, closing the connection in a few seconds."
#define NEW_LINE_STRING "\r\n"
#define SRV_AND_CHAR_INDEX "1,2"
log_printf( &logger, ">>> Waiting for a BLE peer to establish connection with the Click board...\r\n" );
while ( STEPHANOI_OK != stephanoi_read_response( ctx, STEPHANOI_URC_BLE_CONNECTED ) );
urc_buf_ptr = strstr( app_buf, STEPHANOI_URC_BLE_CONNECTED ) + strlen ( STEPHANOI_URC_BLE_CONNECTED );
ble_conn_index[ 0 ] = *urc_buf_ptr;
log_printf( &logger, ">>> Waiting for data (up to 60 seconds)...\r\n" );
log_printf( &logger, ">>> Connection will be terminated if the Click receives an \"END\" string.\r\n" );
for ( ; ; )
{
stephanoi_clear_app_buf( );
if ( STEPHANOI_OK == stephanoi_process( ctx ) )
{
Delay_ms ( 100 );
timeout_cnt = 0;
stephanoi_process( ctx );
stephanoi_log_app_buf( );
if ( strstr( app_buf, STEPHANOI_URC_BLE_DISCONNECTED ) )
{
log_printf( &logger, ">>> BLE client has terminated the connection.\r\n" );
break;
}
else if ( strstr( app_buf, TERMINATION_CMD ) )
{
log_printf( &logger, ">>> Terminate connection on demand.\r\n" );
stephanoi_clear_app_buf( );
strcpy( app_buf, ble_conn_index );
strcat( app_buf, "," );
strcat( app_buf, SRV_AND_CHAR_INDEX );
strcat( app_buf, "," );
uint16_to_str( strlen( TERMINATION_RESPONSE ) + 2, message_len_buf );
l_trim( message_len_buf );
r_trim( message_len_buf );
strcat( app_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_BLE_GATTS_NOTIFY, app_buf );
stephanoi_generic_write ( ctx, TERMINATION_RESPONSE, strlen ( TERMINATION_RESPONSE ) );
stephanoi_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_BLE_DISCONNECT, ble_conn_index );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
break;
}
}
timeout_cnt++;
if ( 0 == ( timeout_cnt % BLE_TERMINAL_MESSAGE_FREQ_MS ) )
{
log_printf( &logger, ">>> Sending \"%s\" message to connected device.\r\n", ( char * ) MESSAGE_CONTENT );
stephanoi_clear_app_buf( );
strcpy( app_buf, ble_conn_index );
strcat( app_buf, "," );
strcat( app_buf, SRV_AND_CHAR_INDEX );
strcat( app_buf, "," );
uint16_to_str( strlen( MESSAGE_CONTENT ) + 2, message_len_buf );
l_trim( message_len_buf );
r_trim( message_len_buf );
strcat( app_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_BLE_GATTS_NOTIFY, app_buf );
stephanoi_generic_write ( ctx, MESSAGE_CONTENT, strlen ( MESSAGE_CONTENT ) );
stephanoi_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
}
if ( BLE_TERMINAL_TIMEOUT_MS < timeout_cnt )
{
log_printf( &logger, ">>> Terminate connection due to 60s timeout expiration.\r\n" );
stephanoi_clear_app_buf( );
strcpy( app_buf, ble_conn_index );
strcat( app_buf, "," );
strcat( app_buf, SRV_AND_CHAR_INDEX );
strcat( app_buf, "," );
uint16_to_str( strlen( TERMINATION_TIMEOUT ) + 2, message_len_buf );
l_trim( message_len_buf );
r_trim( message_len_buf );
strcat( app_buf, message_len_buf );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_BLE_GATTS_NOTIFY, app_buf );
stephanoi_generic_write ( ctx, TERMINATION_TIMEOUT, strlen ( TERMINATION_TIMEOUT ) );
stephanoi_generic_write ( ctx, NEW_LINE_STRING, strlen ( NEW_LINE_STRING ) );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
stephanoi_cmd_set( &stephanoi, STEPHANOI_CMD_BLE_DISCONNECT, ble_conn_index );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
break;
}
Delay_ms ( 1 );
}
log_printf( &logger, ">>> Start BLE advertising.\r\n" );
stephanoi_cmd_run( ctx, STEPHANOI_CMD_BLE_START_ADVERTISING );
error_flag |= stephanoi_read_response( ctx, STEPHANOI_RSP_OK );
#else
#error "No demo example selected"
#endif
return error_flag;
}
// ------------------------------------------------------------------------ END
额外支持
资源
类别:无线网络