我是靠谱客的博主 淡然音响,最近开发中收集的这篇文章主要介绍ESP8266学习笔记(15)——SoftAP热点配网一、配网流程二、实验前提三、搭建HTTP服务器四、配网五、搭建UDP客户端,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
一、配网流程
- 首先手机连接智能硬件的 无线网络B
- 手机APP与智能硬件建立 TCP 连接
- 通过 HTTP 协议将 JSON 封装的 无线网络A 信息(SSID和密码)发送到智能硬件
- 智能硬件连接 无线网络A
- 连接成功后,无线网络A 中 UDP 广播分配后的IP地址和自身MAC地址
- 工作模式从AP模式切换为STA模式
二、实验前提
需要用到TCP/UDP通信接口,查看 ESP8266学习笔记(5)——TCP/UDP接口使用
需要用到HTTP服务器接口,查看 ESP8266学习笔记(13)——HTTP服务器
需要用到JSON接口,查看 ESP8266学习笔记(8)——第三方库cJSON使用
三、搭建HTTP服务器
3.1 user_httpserver.h
#ifndef _USER_WEB_SERVER_H_
#define _USER_WEB_SERVER_H_
/*********************************************************************
* INCLUDES
*/
/*********************************************************************
* 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 %srn
Content-Length: %drn
Server: lwIP/1.4.0rn
Content-type: application/jsonrn
Expires: Fri, 10 Apr 2008 14:00:00 GMTrn
Pragma: no-cachernrn
%s"
/*********************************************************************
* TYPEDEFS
*/
typedef struct urlFrame_t
{
uint8 type;
char select[URL_SIZE];
char command[URL_SIZE];
char filename[URL_SIZE];
} UrlFrame_t;
/*********************************************************************
* API FUNCTIONS
*/
void HttpServerInit(void);
void HttpServerListen(void);
#endif /* _USER_WEB_SERVER_H_ */
3.2 user_httpserver.c
/*********************************************************************
* INCLUDES
*/
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "user_httpserver.h"
#include "user_udp.h"
#include "user_wifimode.h"
#include "cJSON.h"
static void connectCallback(void *arg);
static void receiveDataCallback(void *arg, char *pData, unsigned short len);
static void disconnectCallback(void *arg);
static bool checkDataIntegrity(char *pRecvData, uint16 recvDatalen);
static void parseUrl(char *pRecvData, UrlFrame_t *pUrlFrame);
static void findRequestData(char *pRecvData, char *pRequestData);
static void handleGetUrlPath(UrlFrame_t *pUrlFrame, char *pRequestData);
static void handlePostUrlPath(UrlFrame_t *pUrlFrame, char *pRequestData);
static void configWifi(char *pRequestData);
static void sendPostResponse(bool responseOk, char *pResponseData);
static void jsonPackageResponseData(bool responseOk, char *pSendData);
/*********************************************************************
* LOCAL VARIABLES
*/
static struct espconn s_httpSvrTcpEspconn; // HTTP服务器TCP连接结构体
/*********************************************************************
* 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);
}
/**
@brief 监听HTTP服务器
@param 无
@return 无
*/
void ICACHE_FLASH_ATTR
HttpServerListen(void)
{
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: %sn", 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 disconnectn",
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);
}
/**
@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, "rnrn");
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, "rn");
if(pTempRecvData != NULL)
{
os_memcpy(lenBuffer, pData, pTempRecvData - pData);
dataSumLen = atoi(lenBuffer);
os_printf("A_dat:%u,total:%u,lenght:%un",dataSumLen, tempTotalLen, tempLen);
if(dataSumLen != tempTotalLen)
{
return false;
}
return true;
}
}
else
{
return true;
}
}
return false;
}
/**
@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);
}
}
/**
@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] = '