加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
esp32Drv.c 28.84 KB
一键复制 编辑 原始数据 按行查看 历史
xieyangLy 提交于 2020-05-03 19:49 . 1、添加smart config功能
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
#include "string.h"
#include "stdio.h"
#include "cmsis_os2.h" // ::CMSIS:RTOS2
#include "usart.h"
#include "dma.h"
#include "esp32Drv.h"
#include "utilities/utilities.h"
#ifdef EnDebugTools
#include "./xDebugTools/xDebugTools.h"
#define espDrvLog(...) xDebug_Print(0,__VA_ARGS__)
#else
#define espDrvLog(...)
#endif
int32_t espInitDrv(esp32Drv* esp);
/* 基础串口读写*/
int32_t espGetChar(esp32Drv* esp,uint8_t *data);
int32_t espReadLine(esp32Drv* esp,char* pLine,uint32_t len,uint32_t ticks);
int32_t espReadLineBlock(esp32Drv* esp,char* pLine,uint32_t len,uint32_t ticks);
int32_t espSendData(esp32Drv* esp,uint8_t *pBuf,uint32_t dataLength);
int32_t espWaitResponse(esp32Drv* esp,uint8_t *pStr,uint32_t ticks);
/*
* ***************************** 上层功能
*/
int32_t espInit(esp32Drv* esp);
int32_t espSetRTE(esp32Drv* esp,char rte);
int32_t espSetRST(esp32Drv* esp);
int32_t espJoinAp(esp32Drv* esp,uint8_t *Ap,uint8_t *sn,uint32_t timeout);
int32_t espSmartConfigStart(esp32Drv* esp,uint32_t timeout);
int32_t espSmartConfigStop(esp32Drv* esp);
int32_t espExitAp(esp32Drv* esp);
int32_t espScanAp(esp32Drv* esp,WIFI_AP_T *pStr,uint8_t apNumber);
int32_t espSetMode(esp32Drv* esp,uint8_t mode);
int32_t espSetApConfig(esp32Drv* esp,struct espApConfType espConf);
int32_t espSetStaName(esp32Drv* esp,char* name);
int32_t espGetStaIp(esp32Drv* esp,char* Ip);
int32_t espSetMdns(esp32Drv* esp,uint8_t en,char* hostname,char* serverName,uint16_t port);
int32_t espSetApIp(esp32Drv* esp,uint8_t*ip);
int32_t espGetStatus(esp32Drv* esp,uint8_t status); //待补充
int32_t espGetLocalIp(esp32Drv* esp,uint8_t *ip);
int32_t espGetLocalMac(esp32Drv* esp,uint8_t *mac);
int32_t espSetLocalIp(esp32Drv* esp,uint8_t *ip);
int32_t espSetLocalMac(esp32Drv* esp,uint8_t *mac);
/*
* tcp/udp IP 传输
*/
int32_t espSetMuxCon(esp32Drv* esp,uint8_t mode);
int32_t espSSLConfig(esp32Drv* esp,uint8_t linkID,struct espCAConfType caConf);
int32_t espConnectTcp(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t keepAlive,uint8_t linkId);
int32_t espConnectUdp(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t localPort,uint8_t keep,uint8_t linkId);
int32_t espConnectSsl(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t keepAlive,uint8_t linkId);
int32_t espCreateTcpServer(esp32Drv* esp,uint8_t serverEn,uint16_t port,uint8_t enCA);
int32_t espCreateUdpServer(esp32Drv* esp,uint8_t serverEn,uint16_t port,uint8_t enCA);
int32_t espCloseServer(esp32Drv* esp,uint8_t linkID);
int32_t espSendLink(esp32Drv* esp,uint8_t *pBuf,uint32_t dataLength,uint8_t linkId);
int32_t espSendUdp(esp32Drv* esp,uint8_t *ip,uint16_t port,uint8_t *pBuf,uint32_t dataLength,uint8_t linkID);
int32_t espReceiveData(esp32Drv* esp,uint8_t *pBuf, uint32_t *pBufLen, uint32_t buf_size, uint8_t *link_id);
/* define */
/* variables */
extern xySerial xySerial6;
uint8_t espDrv1RevBuf[2000];
struct espDrvType espDrv1 = {
.serial = &xySerial6,
.recvBuffer = espDrv1RevBuf,
.recvBufferLength = 2000,
.initDrv = espInitDrv,
};
/* function */
/*
* **************** 基础读写 **************************
*/
/*
*初始化驱动程序
*/
int32_t espInitDrv(esp32Drv* esp)
{
/*初始化驱动程序*/
esp->initDrv = espInitDrv;
esp->getChar = espGetChar;
esp->readLine = espReadLine;
esp->readLineBlock = espReadLineBlock;
esp->send = espSendData;
esp->WaitResponse = espWaitResponse;
/*esp32 配置功能*/
esp->init = espInit;
esp->receiveData = espReceiveData;
esp->setRTE = espSetRTE;
esp->setRST = espSetRST;
esp->setMode = espSetMode;
esp->joinAp = espJoinAp;
esp->smartConfigStart =espSmartConfigStart;
esp->smartConfigStop = espSmartConfigStop;
esp->exitAp = espExitAp;
esp->scanAp = espScanAp;
esp->setApConfig = espSetApConfig;
esp->setApIp = espSetApIp;
esp->getStatus = espGetStatus;
esp->getLocalIp = espGetLocalIp;
esp->getLocalMac = espGetLocalMac;
esp->setLocalIp = espSetLocalIp;
esp->setLocalMac = espSetLocalMac;
esp->setStaName = espSetStaName;
esp->getStaIp = espGetStaIp;
esp->setMdns = espSetMdns;
/*tcp/udp IP 传输 */
esp->setMutilCon = espSetMuxCon;
esp->connectTcp = espConnectTcp;
esp->connectUdp = espConnectUdp;
esp->createTcpServer = espCreateTcpServer;
esp->createUdpServer = espCreateUdpServer;
esp->closeServer = espCloseServer;
esp->sendLink = espSendLink;
esp->sendUdp = espSendUdp;
xySerialInit(esp->serial);
return 0;
}
/*
*读取一个字节
*/
int32_t espGetChar(esp32Drv* esp,uint8_t *data)
{
int32_t ret;
xySerial* serial = esp->serial;
ret = serial->getChar(serial,data);
if (ret == soft_OK)
{
uint8_t ucData;
static uint8_t s_flag = 0; /* IPD 还是其他消息 */
static uint16_t s_data_len = 0; /* UDP TCP数据长度 */
char *p1;
// static int32_t s_last_rx_time = 0;
static struct softTimerStampType timeStamp= {0};
static uint16_t s_ipd_pos = 0;
/* +IPD,0,7:ledon 1 */
ucData = *data;
/* 如果上次收到的数据距今超过100ms,则重新做帧同步 */
if(hSoftTimer1.checkDelay(&hSoftTimer1,timeStamp,1000) == 1)
{
s_flag = 0;
esp->rxMsgLen = 0; //替换原全局变量 g_RxMsgLen = 0;
s_ipd_pos = 0;
}
// espDrvLog("%c,%d\r\n",ucData,s_ipd_pos);
if (s_flag == 0)
{
if (ucData == 0x0D || ucData == 0xA)
{
if (s_ipd_pos >= 2)
{
esp->rxMsgLen = s_ipd_pos; /* 接收到非数据包的应答 */
/* 识别断线消息 - 此处不处理。交给上层处理 */
{
/* TCP服务器关闭了1# TCP连接 */
if (esp->rxMsgLen >= 8 && memcmp(esp->rxMsgBuf, "1,CLOSED", 8) == 0)
{
//服务器关闭了连接
espCallback(esp,ESP32EVT_LINK1_SERVER_CLOSED);
}
/* TCP服务器关闭了2# TCP连接 */
if (esp->rxMsgLen >= 8 && memcmp(esp->rxMsgBuf, "2,CLOSED", 8) == 0)
{
//服务器关闭了连接
espCallback(esp,ESP32EVT_LINK2_SERVER_CLOSED);
}
/* TCP服务器关闭了3# TCP连接 */
if (esp->rxMsgLen >= 8 && memcmp(esp->rxMsgBuf, "3,CLOSED", 8) == 0)
{
//服务器关闭了连接
espCallback(esp,ESP32EVT_LINK3_SERVER_CLOSED);
}
/* TCP服务器关闭了4# TCP连接 */
if (esp->rxMsgLen >= 8 && memcmp(esp->rxMsgBuf, "4,CLOSED", 8) == 0)
{
//服务器关闭了连接
espCallback(esp,ESP32EVT_LINK4_SERVER_CLOSED);
}
/* TCP服务器关闭了5# TCP连接 */
if (esp->rxMsgLen >= 8 && memcmp(esp->rxMsgBuf, "5,CLOSED", 8) == 0)
{
//服务器关闭了连接
espCallback(esp,ESP32EVT_LINK5_SERVER_CLOSED);
}
/* WIFI路由器断网 */
if (esp->rxMsgLen >= 15 && memcmp(esp->rxMsgBuf, "WIFI DISCONNECT", 15) == 0)
{
//路由器断网
esp->wifiConnected = 1;
espCallback(esp,ESP32EVT_WIFI_DISCONNECT);
}
}
s_ipd_pos = 0;
}else{
esp->rxMsgLen = 0;
s_data_len = 0;
s_ipd_pos = 0;
}
}else{
if (s_ipd_pos < RX_MSG_MAX)
{
esp->rxMsgBuf[s_ipd_pos++] = ucData; /* 保存接收到的数据 */
}
if (esp->rxMsgBuf[0] == '+' && s_ipd_pos > 7 && ucData == ':') //加入linkID验证
{
if(esp->mutileLink)
{
p1 = (char *)&esp->rxMsgBuf[5];
esp->rxLinkId = str_to_int(p1); /* 解析出连接id */
p1 = (char *)&esp->rxMsgBuf[7];
s_data_len = str_to_int(p1); /* 解析出数据包长度 */
}else{
p1 = (char *)&esp->rxMsgBuf[5];
s_data_len = str_to_int(p1); /* 解析出数据包长度 */
}
s_flag = 1; /* 进入数据包接收状态 */
s_ipd_pos = 0;
}
}
}else{ /* 这是接收 +IPD数据包的分之 */
if (s_ipd_pos < esp->recvBufferLength)
{
esp->recvBuffer[s_ipd_pos++] = ucData; /* 保存接收到的UDP,TCP数据体 */
if (s_ipd_pos == s_data_len)
{
s_flag = 0;
s_ipd_pos = 0;
esp->recvLinkId = esp->rxLinkId; //保存linkID
esp->recvRxLength = s_data_len; //保存接收的数据长度
esp->recvRdPrt = 0; //都指针复位
espDrvLog("complete\r\n");
espCallback(esp,ESP32EVT_DATA_ARRIVED);
}
}else{
s_flag = 0;
}
}
hSoftTimer1.getStamp(&hSoftTimer1,&timeStamp);
//s_last_rx_time = HAL_GetTick();
return 1;
}
return 0;
}
/*
*读取一行,非阻塞形式
*/
int32_t espReadLine(esp32Drv* esp,char* pLine,uint32_t len,uint32_t timeout)
{
static uint16_t s_pos = 0;
uint8_t ucData;
uint8_t ret = 0;
if (esp->getChar(esp,&ucData))
{
if (s_pos < len)
{
pLine[s_pos++] = ucData; /* 保存接收到的数据 */
}
if (ucData == 0x0A)
{
pLine[s_pos] = 0;
ret = s_pos; /* 成功。 返回数据长度 */
s_pos = 0;
}
}
return ret;
}
/*
*读取一行,阻塞形式,自动去除/r/n
*/
int32_t espReadLineBlock(esp32Drv* esp,char* pLine,uint32_t len,uint32_t ticks)
{
uint8_t ucData;
uint16_t pos = 0;
uint8_t ret;
uint16_t time_out;
static struct softTimerStampType time1= {0};
hSoftTimer1.getStamp(&hSoftTimer1,&time1);
time_out = ticks; /* 首次超时 */
while (1)
{
//bsp_Idle(); /* CPU空闲执行的操作, 见 bsp.c 和 bsp.h 文件 */
if (ticks > 0)
{
if (hSoftTimer1.checkDelay(&hSoftTimer1,time1,time_out) == 1)
{
pLine[pos] = 0; /* 结尾加0, 便于函数调用者识别字符串结束 */
ret = pos; /* 成功。 返回数据长度 */
break;
}else{
//正在传输中
}
}
if (esp->getChar(esp,&ucData))
{
//time1 = bsp_GetRunTime(); /* 收到首字符后,字符间超时设置为 0.5秒 */
hSoftTimer1.getStamp(&hSoftTimer1,&time1);
time_out = 500;
if (pos < len)
{
pLine[pos++] = ucData; /* 保存接收到的数据 */
}
if (ucData == 0x0A)
{
pLine[pos] = 0;
ret = pos; /* 成功。 返回数据长度 */
break;
}
}
}
return ret;
}
/*
*发送数据
*/
int32_t espSendData(esp32Drv* esp,uint8_t *pBuf,uint32_t dataLength)
{
esp->serial->sendBuf(esp->serial,pBuf,dataLength);
return 0;
}
/*
*等待响应
*/
int32_t espWaitResponse(esp32Drv* esp,uint8_t *pStr,uint32_t ticks)
{
uint8_t ucData;
uint16_t pos = 0;
uint32_t len;
int32_t ret;
static struct softTimerStampType time1= {0};
len = strlen((char*)pStr);
if (len > 255)
{
return 0;
}
hSoftTimer1.getStamp(&hSoftTimer1,&time1);
while (1)
{
if (ticks > 0) /* _usTimeOut == 0 表示无限等待 */
{
if (hSoftTimer1.checkDelay(&hSoftTimer1,time1,ticks) == 1)
{
ret = ESPERR_OVERTIME; /* 超时 */
espDrvLog("esp wait:overtime\r\n");
break;
}
}
if (esp->getChar(esp,&ucData))
{
if (ucData == pStr[pos])
{
pos++;
if (pos == len)
{
ret = 0; /* 收到指定的应答数据,返回成功 */
espDrvLog("esp wait:ok\r\n");
break;
}
} else {
pos = 0;
}
}
}
return ret;
}
/*
* ***************************** 上层功能
*/
int32_t espInit(esp32Drv* esp)
{
/*
*复位
*/
esp->setRST(esp);
/*
* 关闭回显
*/
esp->setRTE(esp,0);
return 0;
}
/*
* esp32 reset
*/
int32_t espSetRST(esp32Drv* esp)
{
xySerial* serial = esp->serial;
int32_t ret;
char buf[100];
espDrvLog("espSetRST\r\n");
serial->sendBuf(serial,(uint8_t*)"AT+RST\r\n",sizeof("AT+RST\r\n"));
while(1)
{
if(esp->readLineBlock(esp,buf,100,5000))
{
espDrvLog("rst get new line:\r\n ");
espDrvLog(buf);
if (memcmp(buf, "ready\r\n", strlen("ready\r\n")) == 0) /* ATE1回显情况,第1次读到的是 命令本身 */
{
ret = ESPERR_OK;
break;
}else{
ret = ESPERR_OVERTIME;
break;
}
}
}
// ret = espWaitResponse(esp,(uint8_t *)"ready\r\n", 5000);
osDelay(4*1000);
return ret;
}
/*
* esp32 ret set
*/
int32_t espSetRTE(esp32Drv* esp,char val)
{
xySerial* serial = esp->serial;
int32_t ret;
if(val)
{
espDrvLog("espSetRTE on\r\n");
serial->sendBuf(serial,(uint8_t*)"ATE1\r\n", sizeof("ATE1\n\r"));
}else{
espDrvLog("espSetRTE off\r\n");
serial->sendBuf(serial,(uint8_t*)"ATE0\r\n", sizeof("ATE0\n\r"));
}
ret = espWaitResponse(esp,(uint8_t*)"OK\r\n", 100);
osDelay(1*1000);
return ret;
}
/*
*加入AP
*/
int32_t espJoinAp(esp32Drv* esp,uint8_t *Ap,uint8_t *sn,uint32_t timeout)
{
xySerial* serial = esp->serial;
/* 如果已经连接上了,则会返回如下信息:
(268)=>AT+CWJAP="MERCURY_603","123456887af"
(269)=>
(2488)<==WIFI CONNECTED
(3208)<==WIFI GOT IP
(3274)<==
OK
OK
(320583)=>AT+CWJAP="Tenda_446248","123456887af"
(320634)=>
(320648)<==
WIFI DISCONNECT
(322820)<==WIFI CONNECTED
WIFI GOT IP
(323649)<==
OK
----------- SSID 和 密码不对的情况 ----------
(158892)=>AT+CWJAP="Tenda_446248","123456887af"
(158893)=>
(173898)<==+CWJAP:3
FAIL
*/
char buf[64];
int32_t err_code = ESPERR_OK;
// if (espCheckSSID(_ssid) == 0 || espCheckPassWd(_pwd) == 0)
// {
// //espDrvLog("WiFi SSID和密码参数异常\r\n");
// return 0;
// }
sprintf(buf, "AT+CWJAP=\"%s\",\"%s\"\r\n", Ap, sn);
espDrvLog("join AP:%s\r\n",buf);
serial->sendBuf(serial,(uint8_t *)buf,strlen(buf));
while (1)
{
// if (ESP32_ReadLine(buf, 64, _timeout))
if(esp->readLineBlock(esp,buf,64,timeout))
{
espDrvLog("smartconfig get new line:\r\n ");
espDrvLog(buf);
if (memcmp(buf, "AT+CWJAP_CUR", 12) == 0) /* ATE1回显情况,第1次读到的是 命令本身 */
{
;
}
else if (memcmp(buf, "WIFI CONNECTED", 14) == 0)
{
;
}
else if (memcmp(buf, "OK", 2) == 0)
{
printf("join AP:OK\r\n");
esp->wifiConnected = 1;
return ESPERR_OK; /* 连接AP OK */
}
else if (memcmp(buf, "+CWJAP:", 7) == 0)
{
err_code = buf[7] - '0'; /* 错误代码,ASCII */
}
else if (memcmp(buf, "FAIL", 4) == 0)
{
break;
}
}
else /* 读超时 */
{
err_code = ESPERR_OVERTIME;
esp->wifiConnected = 0;
break;
}
}
return err_code;
}
/*
*启动smartconfig
*/
int32_t espSmartConfigStart(esp32Drv* esp,uint32_t timeout)
{
#define espSmartConfigStart "AT+CWSTARTSMART\r\n"
xySerial* serial = esp->serial;
char buf[100];
int32_t err_code = ESPERR_OK;
if(esp->espWifiMode != ESP_WIFI_MODE_STA)
{
esp->setMode(esp,ESP_WIFI_MODE_STA);
espDrvLog("chang wifi mode\r\n");
esp->wifiConnected = 0;
osDelay(1*1000);
}
espDrvLog("start smartconfig\r\n");
espDrvLog(espSmartConfigStart);
serial->sendBuf(serial,(uint8_t *)espSmartConfigStart,strlen(espSmartConfigStart));
while(1)
{
if(esp->readLineBlock(esp,buf,100,0))
{
espDrvLog("smartconfig get new line:\r\n ");
espDrvLog(buf);
if (memcmp(buf, espSmartConfigStart,strlen(espSmartConfigStart)) == 0) /* ATE1回显情况,第1次读到的是 命令本身 */
{
;
}else if (memcmp(buf, "smartconfig connect fail", strlen("smartconfig connect fail")) == 0)
{
espDrvLog("smartconfig:fail\r\n");
esp->wifiConnected = 0;
return ESPERR_CMD_FAIL; /* 连接AP 失败*/
}
else if (memcmp(buf, "smartconfig connected wifi", strlen("smartconfig connected wifi")) == 0)
{
espDrvLog("smartconfig:OK\r\n");
return ESPERR_OK; /* 连接AP OK */
}else if(memcmp(buf, "smart find channel", strlen("smart find channel")) == 0)
{
espDrvLog("smart find channel\r\n");
espCallback(esp,ESP32EVT_GET_WIFI_INFO);
}else if(memcmp(buf, "WIFI CONNECTED", strlen("WIFI CONNECTED")) == 0)
{
espDrvLog("wifi connected\r\n");
esp->wifiConnected = 1;
espCallback(esp,ESP32EVT_WIFI_CONNECTED);
}else if(memcmp(buf, "WIFI GOT IP", strlen("WIFI GOT IP")) == 0)
{
espDrvLog("got ip\r\n");
espCallback(esp,ESP32EVT_GOT_IP);
}
}
else /* 读超时 */
{
espDrvLog("smartconfig overtime\r\n");
err_code = ESPERR_OVERTIME;
break;
}
}
return err_code;
}
/*
*退出smartconfig
*/
int32_t espSmartConfigStop(esp32Drv* esp)
{
#define espSmartConfigStop "AT+CWSTOPSMART\r\n"
xySerial* serial = esp->serial;
serial->sendBuf(serial,(uint8_t *)espSmartConfigStop,strlen(espSmartConfigStop));
return espWaitResponse(esp,(uint8_t*)"OK\r\n", 2000);
}
/*
*退出AP
*/
int32_t espExitAp(esp32Drv* esp)
{
xySerial* serial = esp->serial;
int32_t ret;
#define espCmdquitAp "AT+ CWQAP"
serial->sendBuf(serial,(uint8_t *)espCmdquitAp,strlen(espCmdquitAp));
ret = espWaitResponse(esp,(uint8_t*)"OK\r\n", 2000);
if(ret == ESPERR_OK)
{
esp->wifiConnected = 0;
}
return ret;
}
/*
*扫描AP
*/
int32_t espScanAp(esp32Drv* esp,WIFI_AP_T *pList,uint8_t apNumber)
{
uint16_t i;
uint16_t count;
char buf[128];
WIFI_AP_T *p;
char *p1, *p2;
uint16_t timeout;
buf[127] = 0;
xySerial* serial = esp->serial;
//发送扫描命令
serial->sendBuf(serial,(uint8_t *)"AT+CWLAP",strlen("AT+CWLAP"));
p = (WIFI_AP_T *)pList;
count = 0;
timeout = 8000;
for (i = 0; i < apNumber; i++)
{
esp->readLineBlock(esp,buf, 128, timeout);
if (memcmp(buf, "OK", 2) == 0)
{
break;
}
else if (memcmp(buf, "ERROR", 5) == 0)
{
break;
}
else if (memcmp(buf, "+CWLAP:", 7) == 0)
{
p1 = buf;
/* +CWLAP:(4,"BaiTu",-87,"9c:21:6a:3c:89:52",1) */
/* 解析加密方式 */
p1 = strchr(p1, '('); /* 搜索到(*/
p1++;
p->ecn = str_to_int(p1);
/* 解析ssid */
p1 = strchr(p1, '"'); /* 搜索到第1个分号 */
p1++;
p2 = strchr(p1, '"'); /* 搜索到第2个分号 */
memcpy(p->ssid, p1, p2 - p1);
p->ssid[p2 - p1] = 0;
/* 解析 rssi */
p1 = strchr(p2, ','); /* 搜索到逗号*/
p1++;
p->rssi = str_to_int(p1);
/* 解析mac */
p1 = strchr(p1, '"'); /* 搜索到分号*/
p1++;
p2 = strchr(p1, '"'); /* 搜索到分号*/
memcpy(p->mac, p1, p2 - p1);
p->mac[p2 - p1] = 0;
/* 解析ch */
p1 = strchr(p2, ','); /* 搜索到逗号*/
p1++;
p->ch = str_to_int(p1);
/* 有效的AP名字 */
count++;
p++;
timeout = 2000;
}
}
return count;
}
/*
*设置模式
*/
int32_t espSetMode(esp32Drv* esp,uint8_t mode)
{
xySerial* serial = esp->serial;
int32_t ret=0;
switch(mode)
{
case ESP_WIFI_MODE_NONE:
serial->sendBuf(serial,(uint8_t*)"AT+CWMODE=0\r\n", sizeof("AT+CWMODE=0\n\r"));
break;
case ESP_WIFI_MODE_STA:
serial->sendBuf(serial,(uint8_t*)"AT+CWMODE=1\r\n", sizeof("AT+CWMODE=1\n\r"));
break;
case ESP_WIFI_MODE_AP:
serial->sendBuf(serial,(uint8_t*)"AT+CWMODE=2\r\n", sizeof("AT+CWMODE=2\n\r"));
break;
case ESP_WIFI_MODE_STAAP:
serial->sendBuf(serial,(uint8_t*)"AT+CWMODE=3\r\n", sizeof("AT+CWMODE=3\n\r"));
break;
}
ret = esp->WaitResponse(esp,(uint8_t*)"OK\r\n", 2000);
if(ret == ESPERR_OK)
{
esp->espWifiMode = mode;
}
// osDelay(20);
return ret;
}
/*
*配置AP名称和密码
*/
int32_t espSetApConfig(esp32Drv* esp,struct espApConfType espConf)
{
char cmd_buf[110];
uint8_t len;
//检查ssid
if(espCheckSSID(espConf.ssid)) return ESPERR_PARAM_ERR;
//检查sn
if(espCheckSn(espConf.pwd)) return ESPERR_PARAM_ERR;
/* AT+CWSAP="ESP32","1234567890",5,3 */
len=sprintf(cmd_buf, "AT+CWSAP=%s,%s,%d,%d,%d,%d", espConf.ssid, espConf.pwd, espConf.chl,espConf.ecn,espConf.maxConn,espConf.hidden);
esp->send(esp,(uint8_t*)cmd_buf,len);
if (esp->WaitResponse(esp,(uint8_t*)"OK\r\n", 2000) == 0)
{
return 0;
}
return 0;
}
/*
* 配置主机名称
* 最长32个字符
*/
int32_t espSetStaName(esp32Drv* esp,char* name)
{
char buf[33] = {0};
uint8_t namelen;
strncpy(buf,name,32);
namelen=strlen(buf);
esp->send(esp,(uint8_t*)buf,namelen);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n", 2000);
}
/*
* 查询本机IP
*/
int32_t espGetStaIp(esp32Drv* esp,char* Ip)
{
char buf[64];
esp->send(esp,(uint8_t*)"AT+CIPSTA?\r\n",strlen("AT+CIPSTA?\r\n"));
while(1)
{
if(esp->readLineBlock(esp,buf,64,2000))
{
if (memcmp(buf, "+CIPSTA:", strlen("+CIPSTA:")) == 0)
{
//获取到IP
strncpy(esp->localIp,&buf[8],15);
strncpy(Ip,&buf[8],15);
}
else if (memcmp(buf, "OK\r\n", strlen("OK\r\n")) == 0)
{
espDrvLog("OK\r\n");
return ESPERR_OK; /* 连接AP OK */
}
}
else /* 读超时 */
{
return ESPERR_OVERTIME;
}
}
}
/*
* 配置mdns
*/
int32_t espSetMdns(esp32Drv* esp,uint8_t en,char* hostname,char* serverName,uint16_t port)
{
char buf[256];
int32_t len;
if(en)
{
len=sprintf(buf,"AT+MDNS=1,\"%s\",\"%s\",%d",hostname,serverName,port);
}else{
len=sprintf(buf,"AT+MDNS=1");
}
esp->send(esp,(uint8_t*)buf,len);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n", 2000);
}
/*
*检查密码
*/
int32_t espCheckSn(char *sn)
{
uint8_t i;
for (i = 0; i < PASSWORD_MAX_LEN + 2; i++)
{
if (sn[i] > 127)
{
return 0;
}
if (sn[i] == 0)
{
break;
}
}
if (i <= PASSWORD_MAX_LEN)
{
return 1;
}
return 0;
}
/*
*检查ssid
*/
int32_t espCheckSSID(char *ssid)
{
uint8_t i;
for (i = 0; i < SSID_MAX_LEN + 2; i++)
{
if (ssid[i] > 127)
{
return 0;
}
if (ssid[i] == 0)
{
break;
}
}
if (i <= SSID_MAX_LEN)
{
return 1;
}
return 0;
}
/*
*设置AP的IP
*/
int32_t espSetApIp(esp32Drv* esp,uint8_t*ip)
{
return 0;
}
/*
*获取esp32状态
*/
int32_t espGetStatus(esp32Drv* esp,uint8_t status) //待补充
{
return 0;
}
/*
*获取本地IP
*/
int32_t espGetLocalIp(esp32Drv* esp,uint8_t *ip)
{
return 0;
}
/*
*获取本地mac
*/
int32_t espGetLocalMac(esp32Drv* esp,uint8_t *mac)
{
return 0;
}
/*
*设置本地ip
*/
int32_t espSetLocalIp(esp32Drv* esp,uint8_t *ip)
{
return 0;
}
/*
*设置本地mac
*/
int32_t espSetLocalMac(esp32Drv* esp,uint8_t *mac)
{
return 0;
}
/*
* tcp/udp IP 传输
*/
int32_t espSetMuxCon(esp32Drv* esp,uint8_t mode)
{
xySerial* serial = esp->serial;
if(mode)
{
serial->sendBuf(serial,(uint8_t*)"AT+CIPMUX=1\r\n",strlen("AT+CIPMUX=1\r\n"));
}else{
serial->sendBuf(serial,(uint8_t*)"AT+CIPMUX=0\r\n",strlen("AT+CIPMUX=0\r\n"));
}
if(esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000) == ESPERR_OK)
{
esp->mutileLink = mode;
return ESPERR_OK;
}else{
return ESPERR_CMD_FAIL;
}
}
/*
*配置ssl证书
*/
int32_t espSSLConfig(esp32Drv* esp,uint8_t linkID,struct espCAConfType caConf)
{
char cmdBuf[64] = {0};
uint8_t cmLen=0;
xySerial* serial = esp->serial;
if(esp->mutileLink)
{
//多连接时:(AT+CIPMUX=1) AT+CIPSSLCCONF=<link ID>,<type>,<cert_key_ID>,<CA_ID>
cmLen = sprintf(cmdBuf,"AT+CIPSSLCCONF=%d,%d,%d,%d\r\n",linkID,caConf.type,caConf.keyID,caConf.caID);
}else
{
//单连接时:(AT+CIPMUX=0)AT+CIPSSLCCONF=<type>,<cert_key_ID>,<CA_ID>
cmLen = sprintf(cmdBuf,"AT+CIPSSLCCONF=%d,%d,%d\r\n",caConf.type,caConf.keyID,caConf.caID);
}
serial->sendBuf(serial,(uint8_t*)cmdBuf,cmLen);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*连接远程服务器TCP
*/
int32_t espConnectTcp(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t keepAlive,uint8_t linkId)
{
//char buf[64] = {"AT+CIPSTART=\"TCP\",\"192.168.1.53\",6600\r\n"};
char buf[64] = {0};
uint8_t cmLen=0;
xySerial* serial = esp->serial;
if(esp->mutileLink)
{ //多连接AT+CIPSTART=<link ID>,<type>,<remoteIP>,<remote port>[,<TCP keep alive>]
cmLen = sprintf(buf,"AT+CIPSTART=%d,\"TCP\",\"%s\",%d,%d\r\n",linkId,(char*)ip,remotePort,keepAlive);
}else{//单连接 AT+CIPSTART=<type>,<remote IP>,<remoteport>[,<TCP keep alive>]
cmLen = sprintf(buf,"AT+CIPSTART=\"TCP\",\"%s\",%d,%d\r\n",(char*)ip,remotePort,keepAlive);
}
serial->sendBuf(serial,(uint8_t*)buf,cmLen);
espDrvLog("connect:%s\r\n",buf);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*连接远程服务器UDP
*/
int32_t espConnectUdp(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t localPort,uint8_t keep,uint8_t linkId)
{
//char buf[64] = {"AT+CIPSTART=\"TCP\",\"192.168.1.53\",6600\r\n"};
char buf[64] = {0};
uint8_t cmLen=0;
xySerial* serial = esp->serial;
if(localPort)
{
if(esp->mutileLink)
{ //多连接 AT+CIPSTART=<link ID>,<type>,<remoteIP>,<remote port>[,<UDP local port>,<UDPmode>]
cmLen = sprintf(buf,"AT+CIPSTART=%d,\"UDP\",\"%s\",%d,%d,%d\r\n",linkId,(char*)ip,remotePort,localPort,keep);
}else{//单连接 AT+CIPSTART=<type>,<remoteIP>,<remote port>[,<UDP localport>,<UDP mode>]
cmLen = sprintf(buf,"AT+CIPSTART=\"UDP\",\"%s\",%d,%d,%d\r\n",(char*)ip,remotePort,localPort,keep);
}
}else{
if(esp->mutileLink)
{ //多连接 AT+CIPSTART=<link ID>,<type>,<remoteIP>,<remote port>[,<UDP local port>,<UDPmode>]
cmLen = sprintf(buf,"AT+CIPSTART=%d,\"UDP\",\"%s\",%d,%d\r\n",linkId,(char*)ip,remotePort,keep);
}else{//单连接 AT+CIPSTART=<type>,<remoteIP>,<remote port>[,<UDP localport>,<UDP mode>]
cmLen = sprintf(buf,"AT+CIPSTART=\"UDP\",\"%s\",%d,%d\r\n",(char*)ip,remotePort,keep);
}
}
serial->sendBuf(serial,(uint8_t*)buf,cmLen);
espDrvLog("connect:%s\r\n",buf);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*连接远程服务器SSL
*/
int32_t espConnectSsl(esp32Drv* esp,uint8_t *ip,uint16_t remotePort,uint16_t keepAlive,uint8_t linkId)
{
//char buf[64] = {"AT+CIPSTART=\"TCP\",\"192.168.1.53\",6600\r\n"};
char buf[64] = {0};
uint8_t cmLen=0;
xySerial* serial = esp->serial;
//SSL连接必须在多链接模式
//AT+CIPSTART=[<link ID>,]<type>,<remote IP>,<remote port>[,<TCP keep alive>]
cmLen = sprintf(buf,"AT+CIPSTART=%d,\"SSL\",\"%s\",%d,%d\r\n",linkId,(char*)ip,remotePort,keepAlive);
serial->sendBuf(serial,(uint8_t*)buf,cmLen);
espDrvLog("connect:%s\r\n",buf);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*创建tcp服务器
*/
int32_t espCreateTcpServer(esp32Drv* esp,uint8_t serverEn,uint16_t port,uint8_t enCA)
{
char buf[64] = {0};
/* 打开多链接 */
if(esp->mutileLink == 0)
{
esp->setMutilCon(esp,1);
}
if(serverEn) serverEn=1;
if(enCA)
{
sprintf(buf,"AT+CIPSERVER=%d,%d,\"SSL\",%d\r\n",serverEn,port,enCA);
}else{
sprintf(buf,"AT+CIPSERVER=%d,%d\r\n",serverEn,port);
}
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*创建udp服务器 --暂无该功能
*/
int32_t espCreateUdpServer(esp32Drv* esp,uint8_t serverEn,uint16_t port,uint8_t enCA)
{
return 0;
}
/*
*关闭服务器
*/
int32_t espCloseServer(esp32Drv* esp,uint8_t linkID)
{
char buf[64] = {0};
sprintf(buf,"AT+CIPCLOSE=%d\r\n",linkID);
return esp->WaitResponse(esp,(uint8_t*)"OK\r\n",10000);
}
/*
*通过连接id发送数据
*/
int32_t espSendLink(esp32Drv* esp,uint8_t *pBuf,uint32_t dataLength,uint8_t linkId)
{
int32_t sendLen=0;
char sendBuf[20];
xySerial* serial = esp->serial;
sendLen = sprintf(sendBuf,"AT+CIPSEND=%d\r\n",dataLength);
if(!esp->mutileLink)
{
sendLen = sprintf(sendBuf,"AT+CIPSEND=%d\r\n",dataLength);
}else{
sendLen = sprintf(sendBuf,"AT+CIPSEND=%d,%d\r\n",linkId,dataLength);
}
serial->sendBuf(serial,(uint8_t*)sendBuf,sendLen);
if(esp->WaitResponse(esp,(uint8_t*)">",10000)!= ESPERR_OK) return -1;
serial->sendBuf(serial,(uint8_t*)pBuf,dataLength);
return esp->WaitResponse(esp,(uint8_t *)"SEND OK\r\n", 1000);
}
/*
*发送udp数据
*/
int32_t espSendUdp(esp32Drv* esp,uint8_t *ip,uint16_t port,uint8_t *pBuf,uint32_t dataLength,uint8_t linkID)
{
char cmdBuf[48];
uint32_t cmdLen;
xySerial* serial = esp->serial;
if (dataLength > 2048)
{
dataLength = 2048;
}
if(esp->mutileLink)
{//使能linkID
cmdLen = sprintf(cmdBuf, "AT+CIPSEND=%d,%d,\"%s\",%d\r\n",linkID,dataLength,ip,port);
}else{
cmdLen = sprintf(cmdBuf, "AT+CIPSEND=%d,\"%s\",%d\r\n",dataLength,ip, port);
}
serial->sendBuf(serial,(uint8_t*)cmdBuf,cmdLen);
/* 模块返回 > 表示等待数据输入 */
if(esp->WaitResponse(esp,(uint8_t*)">",10000)!= ESPERR_OK) return -1;
serial->sendBuf(serial,(uint8_t*)pBuf,dataLength);
return esp->WaitResponse(esp,(uint8_t *)"SEND OK\r\n", 1000);
}
/*
*接收数据
*接收数据部分已经在espGetChar实现了
*/
int32_t espReceiveData(esp32Drv* esp,uint8_t *pBuf, uint32_t *pBufLen, uint32_t buf_size, uint8_t *link_id)
{
uint8_t transLength=0;
*link_id = esp->recvLinkId;
transLength= buf_size < esp->recvRxLength?buf_size:esp->recvRxLength;
for(int i=0;i<transLength;i++)
{
*pBuf++ = esp->recvBuffer[esp->recvRdPrt++];
}
esp->recvRdPrt = (esp->recvRdPrt >= esp->recvRxLength)?0:esp->recvRdPrt;
esp->recvRxLength -= transLength;
return esp->recvRdPrt;
}
__weak int32_t espCallback(esp32Drv* esp,int32_t evt)
{
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化