中级
20 分钟

使用 Stephano-I(2617011025000)和 PIC18F57Q43 添加 WiFi 与 BLE 双重连接功能

适用于物联网、智能家居和工业应用的 2.4GHz WiFi 与蓝牙® LE 5 连接解决方案

Stephano-I Click with Curiosity Nano with PIC18F57Q43

已发布 4月 15, 2025

点击板

Stephano-I Click

开发板

Curiosity Nano with PIC18F57Q43

编译器

NECTO Studio

微控制器单元

PIC18F57Q43

添加双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 使用不同逻辑电平,需进行电平转换。此外,该板还配套提供了包含易用函数和示例代码的软件库,可作为进一步开发的参考。

Stephano-I Click hardware overview image

功能概述

开发板

PIC18F57Q43 Curiosity Nano 评估套件是一款尖端的硬件平台,旨在评估 PIC18-Q43 系列内的微控制器。其设计的核心是包含了功能强大的 PIC18F57Q43 微控制器(MCU),提供先进的功能和稳健的性能。这个评估套件的关键特点包括一个黄 色用户 LED 和一个响应灵敏的机械用户开关,提供无

缝的交互和测试。为一个 32.768kHz 水晶振荡器足迹提供支持,确保精准的定时能力。套件内置的调试器拥有一个绿色电源和状态 LED,使编程和调试变得直观高效。此外,增强其实用性的还有虚拟串行端口 (CDC)和一个调试 GPIO 通道(DGI GPIO),提供广泛的连接选项。该套件通过 USB 供电,拥有由

 MIC5353 LDO 调节器提供支持的可调目标电压功能,确保在 1.8V 至 5.1V 的输出电压范围内稳定运行,最大输出电流为 500mA,受环境温度和电压限制。

PIC18F57Q43 Curiosity Nano double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

PIC

MCU 内存 (KB)

128

硅供应商

Microchip

引脚数

48

RAM (字节)

8196

你完善了我!

配件

Curiosity Nano Base for Click boards 是一款多功能硬件扩展平台,专为简化 Curiosity Nano 套件与扩展板之间的集成而设计,特别针对符合 mikroBUS™ 标准的 Click 板和 Xplained Pro 扩展板。这款创新的基板(屏蔽板)提供了无缝的连接和扩展可能性,简化了实验和开发过程。主要特点包括从 Curiosity Nano 套件提供 USB 电源兼容性,以及为增强灵活性而提供的另一种外部电源输入选项。板载锂离子/锂聚合物充电器和管理电路确保电池供电应用的平稳运行,简化了使用和管理。此外,基板内置了一个固定的 3.3V 电源供应单元,专用于目标和 mikroBUS™ 电源轨,以及一个固定的 5.0V 升压转换器,专供 mikroBUS™ 插座的 5V 电源轨,为各种连接设备提供稳定的电力供应。

Curiosity Nano Base for Click boards accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset / ID SEL
PA7
RST
UART CTS / ID COMM
PD4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Sleep Mode Wake-Up
PB0
PWM
UART RTS
PA6
INT
UART TX
PC3
TX
UART RX
PC2
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

Stephano-I Click Schematic schematic

一步一步来

项目组装

Curiosity Nano Base for Click boards front image hardware assembly

从选择您的开发板和Click板™开始。以Curiosity Nano with PIC18F57Q43作为您的开发板开始。

Curiosity Nano Base for Click boards front image hardware assembly
Charger 27 Click front image hardware assembly
PIC18F47Q10 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Board mapper by product8 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
PIC18F57Q43 Curiosity MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step 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”替换为要显示的参数。

软件支持

库描述

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

额外支持

资源

喜欢这个项目吗?

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