代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。