Demo:wifi sta

来自Bouffalo Lab Docs
武楷轩留言 | 贡献2025年6月26日 (四) 05:40的版本 (添加ota)
跳转到导航 跳转到搜索

基本介绍:

该系列demo存放在SDK 的路径为:bouffalo_sdk/examples/wifi/sta 。

该系列代码需要将开发板作为STA节点连接到WiFi或者路由器后进行一些基本的通讯数据功能性测试。

以下该系列demo的基本功能说明。

MQTT相关Demo说明:

wifi_mqtt_pub:

该demo将开发板作为mqtt中的publisher,向节点内发布主题信息。

要测试该节点需要以下前置:

1.建立一个mqtt服务器

2.使用mqttbox或者其他相关工具从服务器上订阅对应的主题。

3.可以向串口发送指令。

建议本地下载mosquito,在本地搭建一个mqtt服务器,或者使用emqX公用服务器。该案例的defult server是:`test.mosquitto.org:1883`

向串口发送指令连接WIFI并订阅:

wifi_sta_connect your_wifi_id your_passwd

mqtts_pub $local_ip 8883   #mqtt_pub  如果不带参数会默认发送到上述的公有服务器

#会得到以下输出:

bouffalolab />mqtt_pub is ready to begin publishing the time.
Press ENTER to publish the current time.
Press CTRL-C to exit.
mqtt_pub published : "{"hello mqtt by bl616/8 !"}
Now time is 2023-06-16 00:00:00"
mqtt_pub published : "{"hello mqtt by bl616/8 !"}
Now time is 2023-06-16 00:00:03"
mqtt_pub stop publish to test.mosquitto.org

订阅方会受到当前时间的字符串。

wifi_mqtt_sub:

和上述一样,但是该案例是submiter ,也就是消息订阅方。

前置条件:同上。

同样向串口发送指令来验证:

bouffalolab />wifi_sta_connect your_wifi_id your_pwd
bouffalolab />mqtt_sub your_server <port> 

#得到输出

bouffalolab />mqtt_sub listening for '<theme>' messages.
Press CTRL-C to exit.
Received publish('<theme>'):  {"hello mqtt"}
Received publish('<theme>'):  {"hello mqtt"}
mqtt_sub disconnecting from xxx.your_server

HTTP与HTTPS相关demo说明:

wifi_http:

该demo会向网络服务器发送请求,根据返回结果判断http请求功能是否正常。

前置条件为:一个可共上网的wifi,可以向串口发送指令。

bouffalolab />wifi_sta_connect your_wifi your_pwd
bouffalolab />wifi_http_test www.xxx.com

#以下为输出

bouffalolab />Http client task start ...
Host:www.xxx.com, Server ip Address : the ip of websit server
Http client connect server success!
Press CTRL-C to exit.
HTTP/1.1 200 OK

wifi_https:

该demo会自动发加密数据到www.baidu.com,根据结果判断加密功能是否正常

前置条件:同上

bouffalolab />wifi_https_test
bouffalolab />dst_addr is 842C9824

#以下为输出
[I: 124359488][bl_https.c:126]    HTTP/S> bouffalo connect fd = 0x62fd0dc0
[I: 124377783][bl_https.c:136]    HTTP/S> ret = -28
[I: 124598639][bl_https.c:156]    HTTP/S> ret = 0
[I: 124598987][bl_https.c:160]    HTTP/S> total time:471 ms
[I: 124679688][bl_https.c:168]    HTTP/S> rcv_ret = 1179
[I: 124680077][bl_https.c:172]    HTTP/S> proc_head_r 0, status_code 200, body_start_off 400
[I: 124680533][bl_https.c:180]    HTTP/S> Copy to resp @off 0, len 779, 1st char 3C
bl_TcpSslDisconnect end
[I: 124683390][bl_https.c:264]    HTTP/S> total time:555 ms
[I: 124683715][bl_https.c:265]    HTTP/S> test_1: status_code 200, resp_len 779
[I: 124684395][bl_https.c:267]    HTTP/S> resp body: <!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.

如果你想要更改测试服务器目标,可以在https.c源码中添加test,或者直接更改这部分代码:

static void test_1(void)
{
    const char *host = "www.baidu.com";
    const uint8_t send_buf[] = "GET / HTTP/1.1\r\n"
                               "Host: www.baidu.com\r\n"
                               "User-Agent: curl/7.58.0\r\n"
                               "accept: application/json\r\n"
                               "\r\n";
    uint8_t *buf;
    int resp_len;
    int status_code;

    buf = malloc(16 * 1024);
    if (NULL == buf) {
        LOG_E("malloc\r\n");
        return;
    }

    resp_len = 16 * 1024;

    uint32_t start_time = 0, stop_time = 0;
    start_time = xTaskGetTickCount();
    status_code = https_request(host, 443, send_buf, sizeof(send_buf) - 1, buf, &resp_len);
    stop_time = xTaskGetTickCount();

    LOG_I("total time:%d ms\r\n", (stop_time - start_time));
    LOG_I("test_1: status_code %d, resp_len %d\r\n", status_code, resp_len);
    buf[resp_len] = '\0';
    LOG_I("resp body: %s\r\n", buf);

    free(buf);
}

上述函数被调用用来执行测试,更改该部分代码可以实现更改测试目标的目的。

TCP的Demo说明:

wifi_tcp:

该部分代码编写了一个server.c和一个client.c,分别用于测试TCP服务端测试和客户端测试。

前置条件:PC端和开发板共同在一个局域网内,可以向串口发送指令。

使用picocom工具在串口中执行指令。

server测试:

bouffalolab />wifi_sta_connect your_wifi_id your_wifi_pwd
bouffalolab />wifi_tcp_echo_test 3365

#服务器建立成功

bouffalolab />tcp server task start ...
Server ip Address : 0.0.0.0:3365

# 执行 nc -v ipaddr port 连接板子的tcp服务器

new client connected from (192.168.1.2, 57480)
Press CTRL-C to exit.
recv 7 len data

client测试:

#首先执行 nc -l -p 3365 在本地架起一个服务器

bouffalolab />wifi_sta_connect BL_TEST 12345678
bouffalolab />wifi_tcp_test localip 3365

#输出

bouffalolab />tcp client task start ...
Server ip Address : 192.168.1.2:3365
TCP client connect server success!
Press CTRL-C to exit.

UDP的demo说明:

该部分代码将使用开发板作为sta节点连接wifi并且架起一个udp服务器。

前置条件:PC端和开发板在同一个局域网内,可以向串口发送指令。

测试过程:

#连接开发板终端并发送指令

bouffalolab />wifi_sta_connect your_wifi_id 12345678
bouffalolab />wifi_udp_echo 3365
bouffalolab />udp server task start ...

#服务其成功架起输出
ip Address : ip:3365
udp bind port success!
Press CTRL-C to exit.

#在PC端执行  nc -u server_ip server_port
#直接发送 1234567890

#成功接受到数据
recv from 192.168.1.2
recv:X
recv from 192.168.1.2
recv:X
recv from 192.168.1.2
recv:X
recv from 192.168.1.2
recv:X
recv from 192.168.1.2
recv:1234567890

OTA的demo说明:

wifi_ota:

该部分代码测试了基本tcp升级固件,使用pc端作为服务器,在同一局域网下,通过命令使得开发板连接ota服务器,接收固件包。

用于测试的固件包被放在了编译出的 build/build_out 目录下。

前置条件:PC端和开发板在同一个局域网内,可以向串口发送指令。

测试过程:

#该程序需要分区表,所以在编译之后需要执行make whole来烧录whole bin
make whole
cd build/build_out
cat wifi_ota_bl616.bin.ota | nc -nvl 3365   #在pc端架服务器并发送固件

#向串口发送指令连接WiFi并连接服务器开始测试
bouffalolab />wifi_sta_connect your_ssid your_passwd
bouffalolab />wifi_ota_test PC_ip_addr 3365

#收到以下回复则表示测试成功
[STA CONSOLE] Calculated SHA256 Checksum:9F5EA3181316DA72589CD4CB0702AA25ED3094F7BED2B5898187044A0A01A077
[STA CONSOLE] Header SET SHA256 Checksum:9F5EA3181316DA72589CD4CB0702AA25ED3094F7BED2B5898187044A0A01A077
[STA CONSOLE] [OTA] [TCP] prepare OTA partition info
[STA CONSOLE] [OTA] [TCP] Update PARTITION, partition len is 804560
reboot
[STA CONSOLE] [OTA] [TCP] Rebooting
[STA CONSOLE] dynamic memory init success, ocram heap size = 263 Kbyte

该源码可以支持配置自动连接WIFI,可以修改源码中的该部分内容:

#define AUTO_CONNECT_WIFI (0)

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

struct bflb_device_s *gpio;
static struct bflb_device_s *uart0;

static TaskHandle_t wifi_fw_task;
#if AUTO_CONNECT_WIFI
struct bflb_device_s *wdg;
static TaskHandle_t ota_task_handle;
#endif
static wifi_conf_t conf = {
    .country_code = "CN",
};

#if AUTO_CONNECT_WIFI
// clang-format off
/* config your wifi ssid and password */
static const uint8_t wifi_sta_connet[] = "wifi_sta_connect H3C_21A0AD\r";
/* config your ota server and port */
static const uint8_t wifi_ota_test[] = "wifi_ota_test 192.168.123.120 3365\r";
/* reboot command */
// static const uint8_t send_buf1[] = "reboot\r";
// clang-format on

extern Ring_Buffer_Type shell_rb;
extern void shell_release_sem(void);

SemaphoreHandle_t sem_wifi_init_done;
SemaphoreHandle_t sem_wifi_connect_done;
SemaphoreHandle_t sem_wifi_disconnect;
#endif

将宏定义 AUTO_CONNECT_WIFI 设定为1,并修改 wifi_sta_connect[] wifi_ota_test[] 字符串为你的对应命令即可。