一、背景
首先手机APP连接智能插座热点(AP)将网关的SSID和密码通过HTTP协议配置到插座,完成配置后智能插座连接网关。
Post请求和Get请求:
二、流程
2.1 定义相关变量及宏
/*********************************************************************
* LOCAL VARIABLES
*/
static struct espconn s_httpSvrTcpEspconn; // HTTP服务器TCP连接结构体
/*********************************************************************
* DEFINITIONS
*/
#define URL_SIZE 20
#define REQUEST_DATA_SIZE 1024
#define RESPONSE_DATA_SIZE 256
#define GET 0
#define POST 1
#define HTTP_SERVER_RESPONSE_FRAME "HTTP/1.0 %s\r\n\
Content-Length: %d\r\n\
Server: lwIP/1.4.0\r\n\
Content-type: application/json\r\n\
Expires: Fri, 10 Apr 2008 14:00:00 GMT\r\n\
Pragma: no-cache\r\n\r\n\
%s"
/*********************************************************************
* TYPEDEFS
*/
typedef struct urlFrame_t
{
uint8 type;
char select[URL_SIZE];
char command[URL_SIZE];
char filename[URL_SIZE];
} UrlFrame_t;
2.2 初始化HTTP服务器
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief HTTP服务器初始化
@param 无
@return 无
*/
void ICACHE_FLASH_ATTR
HttpServerInit(void)
{
s_httpSvrTcpEspconn.type = ESPCONN_TCP;
s_httpSvrTcpEspconn.state = ESPCONN_NONE;
s_httpSvrTcpEspconn.proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp));
s_httpSvrTcpEspconn.proto.tcp->local_port = 80;
espconn_regist_connectcb(&s_httpSvrTcpEspconn, connectCallback);
espconn_accept(&s_httpSvrTcpEspconn); // 开启监听
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief 连接成功的回调函数
@param arg -[in] 指向传递给这个回调函数来使用的参数
@return 无
*/
static void ICACHE_FLASH_ATTR
connectCallback(void *arg)
{
struct espconn *pEspconn = arg;
espconn_regist_recvcb(pEspconn, receiveDataCallback);
espconn_regist_disconcb(pEspconn, disconnectCallback);
}
/**
@brief 接收数据的回调函数
@param arg -[in] 指向传递给这个回调函数来使用的参数
@param pData -[in] 接收的数据
@param len -[in] 接收的数据长度
@return 无
*/
static void ICACHE_FLASH_ATTR
receiveDataCallback(void *arg, char *pData, unsigned short len)
{
if(checkDataIntegrity(pData, len) == false)
{
return ;
}
os_printf("recvData: %s\n", pData);
UrlFrame_t urlFrame;
char requestData[REQUEST_DATA_SIZE] = {0};
findRequestData(pData, requestData);
parseUrl(pData, &urlFrame);
switch(urlFrame.type)
{
case GET:
os_printf("We have a GET request.\n");
handleGetUrlPath(&urlFrame, requestData);
break;
case POST:
os_printf("We have a POST request.\n");
handlePostUrlPath(&urlFrame, requestData);
break;
default:
break;
}
}
/**
@brief 断连的回调函数
@param arg -[in] 指向传递给这个回调函数来使用的参数
@return 无
*/
static void ICACHE_FLASH_ATTR
disconnectCallback(void *arg)
{
struct espconn *pEspconn = arg;
os_printf("httpserver's %d.%d.%d.%d:%d disconnect\n",
pEspconn->proto.tcp->remote_ip[0], pEspconn->proto.tcp->remote_ip[1],
pEspconn->proto.tcp->remote_ip[2], pEspconn->proto.tcp->remote_ip[3],
pEspconn->proto.tcp->remote_port);
}
2.3 处理接收数据
2.3.1 检查数据完整性
/**
@brief 检查数据完整性
@param pRecvData -[in] 接收的数据
@param recvDatalen -[in] 接收的数据长度
@return 1 - 数据完整;0 - 数据缺失
*/
static bool ICACHE_FLASH_ATTR
checkDataIntegrity(char *pRecvData, uint16 recvDatalen)
{
if(!pRecvData)
{
return false;
}
char lenBuffer[10] = {0};
char *pTemp = NULL;
char *pData = NULL;
char *pTempRecvData;
uint16 tempLen = recvDatalen;
uint32 tempTotalLen = 0;
uint32 dataSumLen = 0;
pTemp = (char *) os_strstr(pRecvData, "\r\n\r\n");
if(pTemp != NULL)
{
tempLen -= pTemp - pRecvData;
tempLen -= 4;
tempTotalLen += tempLen;
pData = (char *) os_strstr(pRecvData, "Content-Length: ");
if(pData != NULL)
{
pData += 16;
pTempRecvData = (char *) os_strstr(pData, "\r\n");
if(pTempRecvData != NULL)
{
os_memcpy(lenBuffer, pData, pTempRecvData - pData);
dataSumLen = atoi(lenBuffer);
os_printf("A_dat:%u,total:%u,lenght:%u\n",dataSumLen, tempTotalLen, tempLen);
if(dataSumLen != tempTotalLen)
{
return false;
}
return true;
}
}
else
{
return true;
}
}
return false;
}
2.3.2 查找请求数据
/**
@brief 查找请求数据
@param pRecvData -[in] 接收的数据
@param pRequestData -[in&out] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
findRequestData(char *pRecvData, char *pRequestData)
{
char *pRequestDataStart = NULL;
char *pRequestDataEnd = NULL;
pRequestDataStart = strchr(pRecvData, '{');
if(pRequestDataStart != NULL)
{
pRequestDataEnd = strrchr(pRecvData, '}');
if(pRequestDataEnd != NULL)
{
os_memcpy(pRequestData, pRequestDataStart, pRequestDataEnd - pRequestDataStart + 1);
}
}
pRequestData[pRequestDataEnd - pRequestDataStart + 1] = '\0';
}
2.3.3 解析URL
/**
@brief 解析URL
@param pRecvData -[in] 接收的数据
@param pUrlFrame -[in&out] URL框架
@return 无
*/
static void ICACHE_FLASH_ATTR
parseUrl(char *pRecvData, UrlFrame_t *pUrlFrame)
{
if(pUrlFrame == NULL || pRecvData == NULL)
{
return ;
}
char *pStr = NULL;
uint8 length = 0;
char *pBuffer = NULL;
char *pBuf = NULL;
pBuffer = (char *) os_strstr(pRecvData, "Host:");
if(pBuffer != NULL)
{
length = pBuffer - pRecvData;
pBuf = (char *)os_zalloc(length + 1);
pBuffer = pBuf;
os_memcpy(pBuffer, pRecvData, length);
os_memset(pUrlFrame->select, 0, URL_SIZE);
os_memset(pUrlFrame->command, 0, URL_SIZE);
os_memset(pUrlFrame->filename, 0, URL_SIZE);
if(os_strncmp(pBuffer, "GET ", 4) == 0)
{
pUrlFrame->type = GET;
pBuffer += 4;
}
else if(os_strncmp(pBuffer, "POST ", 5) == 0)
{
pUrlFrame->type = POST;
pBuffer += 5;
}
pBuffer++;
pStr = (char *) os_strstr(pBuffer, "?");
if(pStr != NULL)
{
length = pStr - pBuffer;
os_memcpy(pUrlFrame->select, pBuffer, length);
pStr++;
pBuffer = (char *) os_strstr(pStr, "=");
if(pBuffer != NULL)
{
length = pBuffer - pStr;
os_memcpy(pUrlFrame->command, pStr, length);
pBuffer++;
pStr = (char *) os_strstr(pBuffer, "&");
if(pStr != NULL)
{
length = pStr - pBuffer;
os_memcpy(pUrlFrame->filename, pBuffer, length);
}
else
{
pStr = (char *) os_strstr(pBuffer, " HTTP");
if(pStr != NULL)
{
length = pStr - pBuffer;
os_memcpy(pUrlFrame->filename, pBuffer, length);
}
}
}
}
os_free(pBuf);
}
}
2.3.4 解析GET请求URL
cJSON使用查看 ESP8266学习笔记(8)——第三方库cJSON使用
以查询继电器状态为例
HTTP Header中
GET /config?command=switch HTTP/1.1
/**
@brief 处理GET请求URL路径
@param pUrlFrame -[in] URL框架
@param pRequestData -[in] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
handleGetUrlPath(UrlFrame_t *pUrlFrame, char *pRequestData)
{
os_printf("Type:%d, Select:%s, Command:%s, Filename:%s ",
pUrlFrame->type, pUrlFrame->select, pUrlFrame->command, pUrlFrame->filename);
if(os_strcmp(pUrlFrame->select, "config") == 0)
{
if(os_strcmp(pUrlFrame->command, "command") == 0)
{
if(os_strcmp(pUrlFrame->filename, "switch") == 0)
{
sendRelayStatusResponse();
}
}
}
}
2.3.4.1 封装继电器状态响应包
/**
@brief 发送继电器状态响应
@param pRequestData -[in] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
sendRelayStatusResponse(void)
{
char sendData[RESPONSE_DATA_SIZE] = {0};
jsonPackageRelayStatusData(true, sendData);
sendGetResponse(true, sendData);
}
/**
@brief JSON格式封装继电器状态数据
@param responseOk -[in] 响应是否成功
@param pSendData -[in&out] 要封装的发送数据
@return 无
*/
static void ICACHE_FLASH_ATTR
jsonPackageRelayStatusData(bool responseOk, char *pSendData)
{
if(!pSendData)
{
return ;
}
cJSON *pRoot = cJSON_CreateObject();
uint16 statusCode;
if(responseOk)
{
statusCode = 200;
}
else
{
statusCode = 400;
}
cJSON_AddNumberToObject(pRoot, "status", statusCode);
cJSON_AddNumberToObject(pRoot, "switch", GetRelayStatus()); // 加入自己的获取继电器状态函数
char *tempBuffer = cJSON_Print(pRoot);
os_sprintf(pSendData, "%s", tempBuffer);
os_free((void *) tempBuffer);
cJSON_Delete(pRoot);
}
2.3.4.2 发送继电器状态响应包
/**
@brief 发送GET请求HTTP响应
@param responseOk -[in] 响应是否成功
@param pResponseData -[in] 响应数据
@return 无
*/
static void ICACHE_FLASH_ATTR
sendGetResponse(bool responseOk, char *pResponseData)
{
char sendData[RESPONSE_DATA_SIZE] = {0};
char responseCode[15] = {0};
if(responseOk)
{
os_sprintf(responseCode, "200 OK", os_strlen(responseCode));
}
else
{
os_sprintf(responseCode, "400 BadRequest", os_strlen(responseCode));
}
os_sprintf(sendData, HTTP_SERVER_RESPONSE_FRAME, responseCode, os_strlen(pResponseData), pResponseData);
espconn_sent(&s_httpSvrTcpEspconn, sendData, os_strlen(sendData));
}
2.3.5 解析POST请求URL
cJSON使用查看 ESP8266学习笔记(8)——第三方库cJSON使用
以设置继电器状态为例
HTTP Header中
POST /config?command=switch HTTP/1.1
/**
@brief 处理POST请求URL路径
@param pUrlFrame -[in] URL框架
@param pRequestData -[in] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
handlePostUrlPath(UrlFrame_t *pUrlFrame, char *pRequestData)
{
os_printf("Type:%d, Select:%s, Command:%s, Filename:%s ",
pUrlFrame->type, pUrlFrame->select, pUrlFrame->command, pUrlFrame->filename);
if(os_strcmp(pUrlFrame->select, "config") == 0)
{
if(os_strcmp(pUrlFrame->command, "command") == 0)
{
if(os_strcmp(pUrlFrame->filename, "switch") == 0)
{
configRelayStatus(pRequestData);
}
}
}
}
2.3.5.1 解析设置继电器状态
/**
@brief 配置继电器开关接口
@param pRequestData -[in] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
configRelayStatus(char *pRequestData)
{
if(!pRequestData)
{
return ;
}
cJSON *pRoot = cJSON_Parse(pRequestData);
if(!pRoot)
{
return ;
}
char sendData[RESPONSE_DATA_SIZE] = {0};
cJSON *pRequest = cJSON_GetObjectItem(pRoot, "request"); // 解析request字段内容
if(!pRequest)
{
os_sprintf(sendData, "%s", "No request Item");
sendPostResponse(false, sendData);
cJSON_Delete(pRoot);
return ;
}
cJSON *pStatus = cJSON_GetObjectItem(pRequest, "status"); // 解析request子节点status字段内容
if(!pStatus)
{
os_sprintf(sendData, "%s", "No status Item");
sendPostResponse(false, sendData);
cJSON_Delete(pRoot);
return ;
}
uint8 relayStatus = pStatus->valueint;
SetRelayStatus(relayStatus); // 加入自己设置继电器状态函数 // 设置继电器状态
os_sprintf(sendData, "%s", "Config switch succeed");
sendPostResponse(true, sendData);
cJSON_Delete(pRoot);
}
2.3.5.2 发送响应包
/**
@brief 发送POST请求的HTTP响应
@param responseOk -[in] 响应是否成功
@param pResponseData -[in] 响应数据
@return 无
*/
static void ICACHE_FLASH_ATTR
sendPostResponse(bool responseOk, char *pResponseData)
{
char sendData[RESPONSE_DATA_SIZE] = {0};
char responseCode[15] = {0};
jsonPackageResponseData(responseOk, pResponseData);
if(responseOk)
{
os_sprintf(responseCode, "200 OK", os_strlen(responseCode));
}
else
{
os_sprintf(responseCode, "400 BadRequest", os_strlen(responseCode));
}
os_sprintf(sendData, HTTP_SERVER_RESPONSE_FRAME, responseCode, os_strlen(pResponseData), pResponseData);
espconn_sent(&s_httpSvrTcpEspconn, sendData, os_strlen(sendData));
}
三、使用方法
• 由 Leung 写于 2019 年 8 月 15 日
网友评论