写这个的库的初衷说来也尴尬。github上找不到好用的库,有几个star挺高的,但是并不好用,而且写的也不怎么好。
本地的环境是freertos+stm32 所以对模块的初始化和使用有异步的需求。
大体的设计思路就是 1,串口 DMA+IDLE实现回调函数获取串口数据。 2,处理串口数据进行字符匹配 3,状态机和标志位实现指令流程。(这里没用,蓝牙模块有用)
githu链接地址:
git@github.com:writeing/wifilib.git
上代码
const static stu_wifiCmdRpyInfo sg_stuWifiCmdStatusBuff[] =
{
{"WIFI GOT IP",WIFI_RPY_REPLY},
{"ALREADY",WIFI_RPY_ALREADY},
{"ERROR",WIFI_RPY_ERROR},
{"CONNECTED",WIFI_RPY_CONNECT},
{"CONNECT",WIFI_RPY_CONNECT},
{"CLOSE",WIFI_RPY_CLOSE,wifi_rpyCallBack},
{"DISCONNECT",WIFI_RPY_DISCONNECT,wifi_rpyCallBack},
{">",WIFI_RPY_TRANSPART},
{"OK",WIFI_RPY_OK},
};
这个结构图记录返回字符对应的状态和回调函数
void setWifiData(uint8_t *oriData,int len)
{
for(int i = 0 ; i < ARRAY_LEN(sg_stuWifiCmdStatusBuff) ; i ++)
{
if(strstr(oriData,sg_stuWifiCmdStatusBuff[i].revCmdBuff) != NULL)
{
sg_rpych |= sg_stuWifiCmdStatusBuff[i].reyStatus;
if(sg_stuWifiCmdStatusBuff[i].callback != NULL)
{
sg_stuWifiCmdStatusBuff[i].callback(sg_stuWifiCmdStatusBuff[i].revCmdBuff);
}
}
}
if(strstr((char *)oriData,sg_revCmdBuff) != NULL)
{
sg_wifi_cmd_status = 1;
}
}
进行字符匹配,因为wifi&BT的状态位会返回好几个标志,所以采用了或运算,
int waitWifiCmdStatus(int timeOut,int rpy)
{
for(int i = 0; i < timeOut ; i ++)
{
osDelay(1);
if(sg_wifi_cmd_status && (rpy & rpy))
return WIFI_RPY_SUCCESS;
}
return sg_rpych;
}
int wifi_SetCipmode(int mode)
{
char buff[50];
int len = sprintf(buff,"AT+CIPMODE=%d\r\n",mode);
setWifiCmdBuff("CIPMODE");
wifi_sendCmdData((uint8_t *)buff,len);
int rpy = waitWifiCmdStatus(500,WIFI_RPY_OK);
if(rpy == WIFI_RPY_SUCCESS)
return 1;
else
return 0;
}
int wifi_SetRfPower(int Power_0_to_82)
{
char buff[50];
int len = sprintf(buff,"AT+RFPOWER=%d\r\n",Power_0_to_82);
setWifiCmdBuff("RFPOWER");
wifi_sendCmdData((uint8_t *)buff,len);
int rpy = waitWifiCmdStatus(500,WIFI_RPY_OK);
if(rpy == WIFI_RPY_SUCCESS)
return 1;
else
return 0;
}
int wifi_Station_DhcpEnable(int Enable)
{
char buff[50];
int len = sprintf(buff,"AT+CWDHCP_CUR=1,%d\r\n",Enable);
setWifiCmdBuff("CWDHCP_CUR");
wifi_sendCmdData((uint8_t *)buff,len);
int rpy = waitWifiCmdStatus(500,WIFI_RPY_OK);
if(rpy == WIFI_RPY_SUCCESS)
return 1;
else
return 0;
}
int wifi_TcpIp_StartTcpConnection(char *RemoteIp,uint16_t RemotePort,uint16_t TimeOut)
{
char buff[100];
int len = sprintf(buff,"AT+CIPSTART=\"TCP\",\"%s\",%d,%d\r\n",RemoteIp,RemotePort,TimeOut);
for(int j = 0 ; j <10 ; j ++)
{
setWifiCmdBuff("CIPSTART");
wifi_sendCmdData((uint8_t *)buff,len);
osDelay(1000);
for(int i = 0; i < 10 ; i ++)
{
if(sg_rpych & WIFI_RPY_REPLY)
continue;
if(WIFI_RPY_ALREADY & sg_rpych)
return 1;
if(sg_rpych & WIFI_RPY_CONNECT && sg_wifi_cmd_status == 1)
return 1;
osDelay(500);
}
}
return 0;
}
这就是发送指令的函数, waitWifiCmdStatus 函数判断返回的指令和状态是期望值,就认为通过。
int setWifiCmdStatus(int rpy)
{
static int wifiStatus = 1;
wifiStatus &= rpy;
if(!wifiStatus)
{
printf("%s cmd had error rpyflag = %d\n",sg_revCmdBuff,sg_rpych);
}
return wifiStatus;
}
void wifi_userMain()
{
if(wifi_waitAT() == 0)
{
printf("AT CMD not return\r\n");
return;
}
wifi_RST();
setWifiCmdStatus(wifi_SetRfPower(82));
wifi_TcpIp_Close();
setWifiCmdStatus(wifi_Station_DhcpEnable(1));
setWifiCmdStatus(wifi_Station_ConnectToAp(SSID,PASSWD));
setWifiCmdStatus(wifi_TcpIp_StartTcpConnection(REMOTE_IP,REMOTE_PORT,CONNECT_TIME_OUT));
setWifiCmdStatus(wifi_SetCipmode(1));
sg_wifiInitFlag = setWifiCmdStatus(wifi_cipSend());
printf("sg_wifiInitFlag = %d\r\n",sg_wifiInitFlag);
}
这里是初始化代码,同时对每个函数做一个简单的状态记录。
库配置了一个init函数和当前状态返回和处理函数,可以在user添加一个loop做实时监控,如果wifi或者tcp断开链接可以及时处理
|