一、配网流程
- 首先手机连接智能硬件的 无线网络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 %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;
/*********************************************************************
* 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: %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);
}
/**
@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;
}
/**
@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] = '\0';
}
/**
@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);
}
/**
@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, "wifi") == 0)
{
configWifi(pRequestData);
}
}
}
}
/**
@brief 配置WIFI接口
@param pRequestData -[in] 请求的数据
@return 无
*/
static void ICACHE_FLASH_ATTR
configWifi(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 *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子节点station字段内容字段内容
if(pStation)
{
char ssid[32] = {0};
char password[64] = {0};
cJSON *pSsid = cJSON_GetObjectItem(pStation, "ssid");
cJSON *pPassword = cJSON_GetObjectItem(pStation, "password");
if(!pSsid)
{
os_sprintf(sendData, "%s", "No ssid Item");
sendPostResponse(false, sendData);
cJSON_Delete(pRoot);
return ;
}
if(!pPassword)
{
os_sprintf(sendData, "%s", "No password Item");
sendPostResponse(false, sendData);
cJSON_Delete(pRoot);
return ;
}
os_strcpy(ssid, pSsid->valuestring);
os_strcpy(password, pPassword->valuestring);
os_sprintf(sendData, "%s", "Config station succeed");
sendPostResponse(true, sendData);
ConfigStationMode(ssid, password);
UdpSendDeviceInfo(); // UDP发送设备信息
}
else
{
os_sprintf(sendData, "%s", "No station Item");
sendPostResponse(false, sendData);
}
cJSON_Delete(pRoot);
}
/**
@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));
}
/**
@brief JSON格式封装响应数据
@param responseOk -[in] 响应是否成功
@param pSendData -[in&out] 要封装的发送数据
@return 无
*/
static void ICACHE_FLASH_ATTR
jsonPackageResponseData(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_AddStringToObject(pRoot, "message", pSendData);
char *tempBuffer = cJSON_Print(pRoot);
os_sprintf(pSendData, "%s", tempBuffer);
os_free((void *) tempBuffer);
cJSON_Delete(pRoot);
}
四、配网
4.1 user_wifimode.h
#ifndef _USER_WIFI_MODE_H_
#define _USER_WIFI_MODE_H_
/*********************************************************************
* INCLUDES
*/
/*********************************************************************
* API FUNCTIONS
*/
void ConfigStationMode(char *pSsid, char *pPassword);
#endif /* _USER_WIFI_MODE_H_ */
4.2 user_wifimode.c
/*********************************************************************
* INCLUDES
*/
#include "osapi.h"
#include "user_interface.h"
#include "user_wifimode.h"
/**
@brief 配置STA模式属性
@param pSsid -[in] 要接入的WIFI名字
@param pPassword -[in] 要连入的WIFI密码
@return 无
*/
void ICACHE_FLASH_ATTR
ConfigStationMode(char *pSsid, char *pPassword)
{
if(!pSsid || !pPassword)
{
return ;
}
struct station_config station;
uint8 wifiMode = wifi_get_opmode();
if((wifiMode == STATION_MODE) || (wifiMode == STATIONAP_MODE))
{
os_strcpy(station.ssid, pSsid);
os_strcpy(station.password, pPassword);
wifi_station_set_config(&station);
wifi_station_disconnect();
wifi_station_connect();
}
}
五、搭建UDP客户端
5.1 user_udp.h
#ifndef _USER_UDP_H_
#define _USER_UDP_H_
/*********************************************************************
* INCLUDES
*/
/*********************************************************************
* API FUNCTIONS
*/
void UdpInit(void);
void UdpSendDeviceInfo(void);
#endif /* _USER_UDP_H_ */
5.2 user_udp.c
/*********************************************************************
* INCLUDES
*/
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "user_udp.h"
#include "cJSON.h"
/*********************************************************************
* CONSTANT
*/
const uint16 UDP_DEV_INFO_PERIOD = 1000;
/*********************************************************************
* LOCAL VARIABLES
*/
static struct espconn s_devInfoUdpEspconn; // 设备信息UDP广播结构体
static os_timer_t s_udpDevInfoTimer; // UDP广播设备信息的定时器
static uint8 s_udpDevInfoSendCount; // UDP广播设备信息次数
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief UDP通讯初始化
@param 无
@return 无
*/
void ICACHE_FLASH_ATTR
UdpInit(void)
{
udpDeviceInfoEspconn();
udpBleDataEspconn();
}
/**
@brief UDP发送设备信息
@param 无
@return 无
*/
void ICACHE_FLASH_ATTR
UdpSendDeviceInfo(void)
{
startUdpDeviceInfoTimer();
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief UDP广播设备信息
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
udpDeviceInfoEspconn(void)
{
s_devInfoUdpEspconn.type = ESPCONN_UDP;
s_devInfoUdpEspconn.proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
s_devInfoUdpEspconn.proto.udp->local_port = espconn_port(); // 获取可用端口作为本地端口
s_devInfoUdpEspconn.proto.udp->remote_port = 5000; // 目标端口5050
espconn_regist_sentcb(&s_devInfoUdpEspconn, udpSendDataCallback);
espconn_create(&s_devInfoUdpEspconn);
startUdpDeviceInfoTimer();
}
/**
@brief UDP发送数据的回调函数
@param arg -[in] 指向传递给这个回调函数来使用的参数
@return 无
*/
static void ICACHE_FLASH_ATTR
udpSendDataCallback(void *arg)
{
}
/**
@brief 开始UDP广播设备信息的定时器
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
startUdpDeviceInfoTimer(void)
{
s_udpDevInfoSendCount = 0;
os_timer_disarm(&s_udpDevInfoTimer);
os_timer_setfn(&s_udpDevInfoTimer, (os_timer_func_t *) udpDeviceInfoTimerCallback, NULL);
os_timer_arm(&s_udpDevInfoTimer, UDP_DEV_INFO_PERIOD, true);
}
/**
@brief 停止UDP广播设备信息的定时器
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
stopUdpDeviceInfoTimer(void)
{
os_timer_disarm(&s_udpDevInfoTimer);
}
/**
@brief UDP广播设备信息定时器的回调函数
@param 无
@return 无
*/
static void ICACHE_FLASH_ATTR
udpDeviceInfoTimerCallback(void)
{
uint8 connectApStatus = wifi_station_get_connect_status(); // 获取STA模式下连接路由状态
if(s_udpDevInfoSendCount > 60)
{
stopUdpDeviceInfoTimer();
return ;
}
if(connectApStatus == STATION_GOT_IP)
{
wifi_set_broadcast_if(STATIONAP_MODE); // STA和AP模式都UDP广播
struct ip_info ipInfo;
wifi_get_ip_info(STATION_IF, &ipInfo); // 查询 WIFI被分配的IP地址
const char udpRemoteIp[4] = { 192, 168, ip4_addr3(&ipInfo.ip), 255 }; // 目标IP地址(广播)
os_memcpy(s_devInfoUdpEspconn.proto.udp->remote_ip, udpRemoteIp, 4);
char sendData[UDP_SEND_BUFFER_SIZE] = {0};
jsonPackageDeviceInfo(sendData);
espconn_send(&s_devInfoUdpEspconn, sendData, strlen(sendData)); // UDP发送数据
s_udpDevInfoSendCount++;
}
}
/**
@brief JSON格式封装设备信息
@param pSendData -[in&out] 要封装的发送数据
@return 无
*/
static void ICACHE_FLASH_ATTR
jsonPackageDeviceInfo(char *pSendData)
{
if(!pSendData)
{
return ;
}
cJSON *pRoot = cJSON_CreateObject();
uint8 ip[21] = {0};
uint8 wifiMac[23] = {0};
struct ip_info ipconfig;
wifi_get_ip_info(STATION_IF, &ipconfig); // 查询 WIFI被分配的IP地址
os_sprintf(ip, IPSTR, IP2STR(&ipconfig.ip));
uint8 macAddr[23] = {0};
wifi_get_macaddr(STATION_IF, macAddr); // 获取STA模式的MAC地址
os_sprintf(wifiMac, MACSTR, MAC2STR(macAddr)); // MAC地址
cJSON_AddNumberToObject(pRoot, "status", 0x01);
cJSON_AddNumberToObject(pRoot, "send_times", s_udpDevInfoSendCount);
cJSON_AddStringToObject(pRoot, "ip", ip);
cJSON_AddStringToObject(pRoot, "wifi_mac", wifiMac);
char *tempBuffer = cJSON_Print(pRoot);
os_sprintf(pSendData, "%s", tempBuffer);
os_free((void *) tempBuffer);
cJSON_Delete(pRoot);
}
• 由 Leung 写于 2019 年 9 月 10 日
• 参考:Esp8266学习之旅⑦ 了解softAP热点配网模式原理 设备配网技术之AP配网