STM32驱动Marvell8801介绍(十五) ---- Marvell8801开启open热点/开启wpa热点/开启wpa2热点

代码工程的GITHUB连接:点进进入GITHUB仓库
https://github.com/sj15712795029/stm32f1_marvell88w8801_marvell8801_wifi

Marvell自己实现驱动系列文章分为几篇介绍:
SDIO wifi Marvell8801/Marvell88w8801 介绍(一) ---- 芯片介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(二) ---- SDIO协议介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(三) ---- 寄存器介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(四) ---- 命令/事件/数据格式
SDIO wifi Marvell8801/Marvell88w8801 介绍(五) ---- TLV
SDIO wifi Marvell8801/Marvell88w8801 介绍(六) ---- 实现初始化功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(七) ---- 实现搜索功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(八) ---- 实现STA功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(九) ---- 实现AP功能
SDIO wifi Marvell8801/Marvell88w8801 介绍(十) ---- 移植TCP/IP协议栈LWIP
SDIO wifi Marvell8801/Marvell88w8801 介绍(十一) ---- 自己编写LWIP没有的DHCP server
SDIO wifi Marvell8801/Marvell88w8801 介绍(十二) ---- MQTT介绍
SDIO wifi Marvell8801/Marvell88w8801 介绍(十三) ---- 百度云操作说明
SDIO wifi Marvell8801/Marvell88w8801 介绍(十四) ---- 上位机STA操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十五) ---- 上位机AP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十六) ---- 上位机UDP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十七) ---- 上位机TCP操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十八) ---- 上位机PING操作/代码
SDIO wifi Marvell8801/Marvell88w8801 介绍(十九) ---- 上位机云服务器(百度云)操作/代码
每篇更新打开专栏可以看到打开Marvell8801/Marvell8801 专栏

1. 上位机照片

在这里插入图片描述
功能如下:
①串口配置区,默认是921600的波特率
②Wi-Fi功能开启区
③STA功能调试区
④AP功能调试区
⑤PING功能调试区
⑥TCP功能调试区
⑦UDP功能调试区
⑧云服务器调试区
⑨Debug日志区
跟AP有关的界面如下:
在这里插入图片描述
①要设置的热点名称,密码,如果是OPEN热点,那么密码会忽略
②是否隐藏热点,隐藏热点的的功能是虽然可以创建热点,但是不能被搜索到
③热点的加密类型,包括open/wap/wpa2,open是没有密码
④建立热点
⑤停止热点
⑥断开某一个STA的连接
⑦目前连接到这个热点的STA

2. 上位机与STM32通信格式

目前上位机跟开发板是通过串口来通信,通信格式是json,如果你没听过json,那么自行百度,跟WIFI AP相关的json格式分别如下:
在这里插入图片描述
在这里插入图片描述

1)上位机给STM32发送json的代码如下:

上位机用的c# winform,用的json库using Newtonsoft.Json,此库需要Newtonsoft.Json.dll库
发送代码如下:

private void json_construction_send(string func, string operate, string param1, string param2, string param3, string param4, string param5, string param6)
{
    json_commmand cmd = new json_commmand();
    cmd.FUNC = func;
    cmd.OPERATE = operate;
    cmd.PARAM1 = param1;
    cmd.PARAM2 = param2;
    cmd.PARAM3 = param3;
    cmd.PARAM4 = param4;
    cmd.PARAM5 = param5;
    cmd.PARAM6 = param6;
    string json_cmd = JsonConvert.SerializeObject(cmd);
#if  CONSOLE_DEBUG
    Console.WriteLine(json_cmd);
#endif
    if (serialPort1.IsOpen)
    {
        serialPort1.WriteLine(json_cmd);
    }
}

其中跟wifi ap相关的operate为:

string operate_wifi_start_ap = "WIFI_START_AP";
string operate_wifi_stop_ap = "WIFI_STOP_AP";
string operate_wifi_ap_disconnect_sta = "WIFI_DISCONNECT_STA";

2)STM32收到json的命令解析框架

uint8_t uart_receive_parse(uint8_t *shell_string)
{
    uint8_t result = HW_ERR_OK;

    cJSON* parse_json = cJSON_Parse((const char *)shell_string);
    uint8_t* func_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"FUNC"))->valuestring;
    uint8_t* operate_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"OPERATE"))->valuestring;
    uint8_t* para1 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM1"))->valuestring;
    uint8_t* para2 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM2"))->valuestring;
    uint8_t* para3 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM3"))->valuestring;
    uint8_t* para4 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM4"))->valuestring;
    uint8_t* para5 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM5"))->valuestring;

    if(strcmp((const char *)func_value,"WIFI") == 0)
    {
        if(hw_strcmp((const char *)operate_value,"WIFI_START_AP") == 0)
        {
            ap_info_t ap_info;
            HW_DEBUG("UART PARSE DEBUG:operate WIFI_START_AP\n");

            hw_memset(&ap_info,0,sizeof(ap_info_t));
            hw_memcpy(ap_info.ssid,para1,hw_strlen((const char*)para1));
            ap_info.ssid_len= hw_strlen((const char*)para1);

            if(hw_strcmp((const char*)para3,"OPEN") == 0)
                ap_info.security = WIFI_SECURITY_NONE;
            else if(hw_strcmp((const char*)para3,"WPA") == 0)
            {
                hw_memcpy(ap_info.pwd,para2,hw_strlen((const char*)para2));
                ap_info.pwd_len= hw_strlen((const char*)para2);
                ap_info.security = WIFI_SECURITY_WPA;
            }
            else if(hw_strcmp((const char*)para3,"WPA2") == 0)
            {
                hw_memcpy(ap_info.pwd,para2,hw_strlen((const char*)para2));
                ap_info.pwd_len= hw_strlen((const char*)para2);
                ap_info.security = WIFI_SECURITY_WPA2;
            }

            if(hw_strcmp((const char*)para4,"HIDDEN") == 0)
                ap_info.broadcast_ssid = 0;
            else
                ap_info.broadcast_ssid = 1;

            wifi_start_ap(&ap_info);
            operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
            goto exit;
        }

        if(hw_strcmp((const char *)operate_value,"WIFI_STOP_AP") == 0)
        {
            HW_DEBUG("UART PARSE DEBUG:operate WIFI_STOP_AP\n");
            wifi_stop_ap();
            operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
            goto exit;
        }

        if(hw_strcmp((const char *)operate_value,"WIFI_DISCONNECT_STA") == 0)
        {
            uint8_t index = 0;
            uint8_t mac_address[6] = {0};
            HW_DEBUG("UART PARSE DEBUG:operate WIFI_DISCONNECT_STA\n");

            for(index = 0; index < 6; index++)
                mac_address[index] = (nibble_for_char(*(para1+index*3)) << 4) | nibble_for_char(*(para1+index*3 + 1));
            wifi_disconnect_sta(mac_address);
            operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
            goto exit;
        }    
    }

    if(hw_strcmp((const char *)shell_string,"shop220811498.taobao.com") == 0)
        HW_DEBUG("welcome to use our stm32f1 camera wifi board\n");
    else
        HW_DEBUG("UART PARSE ERR:HW_ERR_SHELL_NO_CMD\n");

    result = HW_ERR_SHELL_NO_CMD;
exit:
    cJSON_Delete(parse_json);
    return result;
}

3)STM32给上位机发送json的代码如下:

uint8_t uart_send_json(uint8_t *func,uint8_t *operate,uint8_t *status,uint8_t *para1,uint8_t *para2,uint8_t *para3,uint8_t *para4,uint8_t *para5)
{
    uint8_t *wifi_status_string;
    cJSON *wifi_json_status = cJSON_CreateObject();

    cJSON_AddStringToObject(wifi_json_status, "FUNC", (const char*)func);
    cJSON_AddStringToObject(wifi_json_status, "OPERATE", (const char*)operate);
    cJSON_AddStringToObject(wifi_json_status, "STATUS", (const char*)status);

    if(para1)
        cJSON_AddStringToObject(wifi_json_status, "PARAM1", (const char*)para1);
    if(para2)
        cJSON_AddStringToObject(wifi_json_status, "PARAM2", (const char*)para2);
    if(para3)
        cJSON_AddStringToObject(wifi_json_status, "PARAM3", (const char*)para3);
    if(para4)
        cJSON_AddStringToObject(wifi_json_status, "PARAM4", (const char*)para4);
    if(para5)
        cJSON_AddStringToObject(wifi_json_status, "PARAM5", (const char*)para5);
    wifi_status_string = (uint8_t *)cJSON_Print(wifi_json_status);

    HW_DEBUG("%s\n",wifi_status_string);
    cJSON_Delete(wifi_json_status);
    free(wifi_status_string);

    return 0;
}

4)上位机接收STM32 json的代码如下

private void json_status_recv_parse(json_status status)
{
#if  CONSOLE_DEBUG
    Console.WriteLine("----------json_status_recv_parse-------------");
    Console.WriteLine("json func:" + status.FUNC);
    Console.WriteLine("json operate:" + status.OPERATE);
    Console.WriteLine("json status:" + status.STATUS);
    Console.WriteLine("json param1:" + status.PARAM1);
    Console.WriteLine("json param2:" + status.PARAM2);
    Console.WriteLine("json param3:" + status.PARAM3);
    Console.WriteLine("json param4:" + status.PARAM4);
    Console.WriteLine("json param5:" + status.PARAM5);
    Console.WriteLine("----------json_status_recv_parse  end--------");
#endif
    if (status.FUNC == "WIFI")
    {
        if (status.OPERATE == "WIFI_START_AP")
        {
            if (status.STATUS == "SUCCESS")
            {
                //linfo_role.Text = "AP";
                //linfo_status.Text = "开启热点成功";
                bwifi_start_ap.Enabled = false;
                bwifi_stop_ap.Enabled = true;
                bshow_ip.Enabled = false;

            }
        }

        if (status.OPERATE == "WIFI_AP_CON_IND")
        {

            int count = dgwifi_ap_con_result.Rows.Count;
            for (int index = 0; index < count - 1; index++)
            {
                if (dgwifi_ap_con_result.Rows[index].Cells[0].Value.ToString() == status.PARAM1)
                {
#if  CONSOLE_DEBUG
                    Console.WriteLine("数据存在");
#endif
                    return;
                }
            }

            int data_index = dgwifi_ap_con_result.Rows.Add();
            dgwifi_ap_con_result.Rows[data_index].Cells[0].Value = status.PARAM1;
            dgwifi_ap_con_result.Rows[data_index].Cells[1].Value = status.PARAM2;
            dgwifi_ap_con_result.Rows[data_index].Cells[2].Value = status.PARAM3;

            if (dgwifi_ap_con_result.Rows.Count > 0)
            {
                bwifi_disconnect_sta.Enabled = true;
            }

        }

        if (status.OPERATE == "WIFI_AP_DISCON_RET")
        {
            int count = dgwifi_ap_con_result.Rows.Count;
            for (int index = 0; index < count - 1; index++)
            {
                string mac = dgwifi_ap_con_result.Rows[index].Cells[1].Value.ToString();
                if (mac == status.PARAM1)
                {
                    dgwifi_ap_con_result.Rows.RemoveAt(index);
                }
            }

            if (dgwifi_ap_con_result.Rows.Count > 1)
            {
                bwifi_disconnect_sta.Enabled = true;
            }
            else
            {
                bwifi_disconnect_sta.Enabled = false;
            }
        }
    }
}

3.开启热点

在这里插入图片描述
在这里插入图片描述

1)上位机给STM32发送开启热点命令

private void bwifi_start_ap_Click(object sender, EventArgs e)
{
    string security = "OPEN";
    string hidden;
    string ssid = tap_ssid.Text;

    if (ssid == "")
    {
        MessageBox.Show("请填入热点名称", "错误提示");
        return;
    }
    string pwd = tap_pwd.Text;

    if (rsec_open.Checked)
        security = "OPEN";
    else if (rsec_wap.Checked)
        security = "WPA";
    else if (rsec_wpa2.Checked)
        security = "WPA2";

    if (security != "OPEN" && (pwd == ""))
    {
        MessageBox.Show("请填入热点密码", "错误提示");
        return;
    }

    if (cap_hidden.Checked)
        hidden = "HIDDEN";
    else
        hidden = "";

    json_construction_send(wifi_func, operate_wifi_start_ap, ssid, pwd, security, hidden, null, null);      
}

2)STM32接收到cmd执行的动作

if(hw_strcmp((const char *)operate_value,"WIFI_START_AP") == 0)
{
    ap_info_t ap_info;
    HW_DEBUG("UART PARSE DEBUG:operate WIFI_START_AP\n");

    hw_memset(&ap_info,0,sizeof(ap_info_t));
    hw_memcpy(ap_info.ssid,para1,hw_strlen((const char*)para1));
    ap_info.ssid_len= hw_strlen((const char*)para1);

    if(hw_strcmp((const char*)para3,"OPEN") == 0)
        ap_info.security = WIFI_SECURITY_NONE;
    else if(hw_strcmp((const char*)para3,"WPA") == 0)
    {
        hw_memcpy(ap_info.pwd,para2,hw_strlen((const char*)para2));
        ap_info.pwd_len= hw_strlen((const char*)para2);
        ap_info.security = WIFI_SECURITY_WPA;
    }
    else if(hw_strcmp((const char*)para3,"WPA2") == 0)
    {
        hw_memcpy(ap_info.pwd,para2,hw_strlen((const char*)para2));
        ap_info.pwd_len= hw_strlen((const char*)para2);
        ap_info.security = WIFI_SECURITY_WPA2;
    }

    if(hw_strcmp((const char*)para4,"HIDDEN") == 0)
        ap_info.broadcast_ssid = 0;
    else
        ap_info.broadcast_ssid = 1;

    wifi_start_ap(&ap_info);
    operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
    goto exit;
}

3)STM32给上位机发送初始化结果

void wifi_start_ap_result(uint8_t status)
{
    uart_send_json("WIFI","WIFI_START_AP",status==0?(uint8_t*)"SUCCESS":(uint8_t*)"FAIL",0,0,0,0,0);
}

4)上位机接收STM32初始化结果的处理

if (status.OPERATE == "WIFI_START_AP")
{
    if (status.STATUS == "SUCCESS")
    {
        //linfo_role.Text = "AP";
        //linfo_status.Text = "开启热点成功";
        bwifi_start_ap.Enabled = false;
        bwifi_stop_ap.Enabled = true;
        bshow_ip.Enabled = false;

    }
}

4.关闭热点

在这里插入图片描述

1)上位机给STM32发送开启热点命令

private void bwifi_stop_ap_Click(object sender, EventArgs e)
{
    json_construction_send(wifi_func, operate_wifi_stop_ap, null, null, null, null, null, null);
    bwifi_start_ap.Enabled = true;
    bwifi_stop_ap.Enabled = false;
}

2)STM32接收到cmd执行的动作

if(hw_strcmp((const char *)operate_value,"WIFI_STOP_AP") == 0)
{
    HW_DEBUG("UART PARSE DEBUG:operate WIFI_STOP_AP\n");
    wifi_stop_ap();
    operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
    goto exit;
}

5. 热点把某一个设备踢掉

在这里插入图片描述

1)上位机给STM32发送开启热点命令

private void bwifi_disconnect_sta_Click(object sender, EventArgs e)
{
    if (ap_disconnect_sta == "")
    {
        MessageBox.Show("请选择要断开的STA", "错误提示");
        return;
    }

    json_construction_send(wifi_func, operate_wifi_ap_disconnect_sta, ap_disconnect_sta, null, null, null, null, null);
    ap_disconnect_sta = "";
}

2)STM32接收到cmd执行的动作

if(hw_strcmp((const char *)operate_value,"WIFI_DISCONNECT_STA") == 0)
{
    uint8_t index = 0;
    uint8_t mac_address[6] = {0};
    HW_DEBUG("UART PARSE DEBUG:operate WIFI_DISCONNECT_STA\n");

    for(index = 0; index < 6; index++)
        mac_address[index] = (nibble_for_char(*(para1+index*3)) << 4) | nibble_for_char(*(para1+index*3 + 1));
    wifi_disconnect_sta(mac_address);
    operate_stauts_oled_show(func_value,operate_value,"SUCCESS",0,0,0,0,0,0);
    goto exit;
}

6. 热点监测有设备连接

在这里插入图片描述

1)STM32发送连接设备信息给上位机

void wifi_ap_connect_result(uint8_t *name,uint8_t *mac,uint8_t *ip)
{
    uint8_t mac_str[20] = {0};
    sprintf((char*)mac_str,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    HW_DEBUG("wifi_ap_connect_result\n");
    HW_DEBUG("name %s\n",name);
    HW_DEBUG("mac %s\n",mac_str);
    HW_DEBUG("ip %s\n",ip);

    uart_send_json("WIFI","WIFI_AP_CON_IND","SUCCESS",name,mac_str,ip,0,0);
}

2)上位机处理连接设备信息

if (status.OPERATE == "WIFI_AP_CON_IND")
{

    int count = dgwifi_ap_con_result.Rows.Count;
    for (int index = 0; index < count - 1; index++)
    {
        if (dgwifi_ap_con_result.Rows[index].Cells[0].Value.ToString() == status.PARAM1)
        {
#if  CONSOLE_DEBUG
            Console.WriteLine("数据存在");
#endif
            return;
        }
    }

    int data_index = dgwifi_ap_con_result.Rows.Add();
    dgwifi_ap_con_result.Rows[data_index].Cells[0].Value = status.PARAM1;
    dgwifi_ap_con_result.Rows[data_index].Cells[1].Value = status.PARAM2;
    dgwifi_ap_con_result.Rows[data_index].Cells[2].Value = status.PARAM3;

    if (dgwifi_ap_con_result.Rows.Count > 0)
    {
        bwifi_disconnect_sta.Enabled = true;
    }

}

7. 热点监测有设备断开

在这里插入图片描述

1)STM32发送断开设备信息给上位机

void wifi_ap_disconnect_result(uint8_t *mac)
{
    uint8_t mac_str[20] = {0};

    HW_DEBUG("wifi_ap_disconnect_result\n");
    HW_DEBUG("mac %s\n",mac_str);
    sprintf((char*)mac_str,"%02x:%02x:%02x:%02x:%02x:%02x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    uart_send_json("WIFI","WIFI_AP_DISCON_RET","SUCCESS",mac_str,0,0,0,0);
}

2)上位机处理断开设备信息

if (status.OPERATE == "WIFI_AP_DISCON_RET")
{
    int count = dgwifi_ap_con_result.Rows.Count;
    for (int index = 0; index < count - 1; index++)
    {
        string mac = dgwifi_ap_con_result.Rows[index].Cells[1].Value.ToString();
        if (mac == status.PARAM1)
        {
            dgwifi_ap_con_result.Rows.RemoveAt(index);
        }
    }

    if (dgwifi_ap_con_result.Rows.Count > 1)
    {
        bwifi_disconnect_sta.Enabled = true;
    }
    else
    {
        bwifi_disconnect_sta.Enabled = false;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wireless_Link

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值