一琐谤、配網(wǎng)流程
- 首先手機(jī)連接智能硬件的 無(wú)線(xiàn)網(wǎng)絡(luò)B
- 手機(jī)APP與智能硬件建立 TCP 連接
- 通過(guò) HTTP 協(xié)議將 JSON 封裝的 無(wú)線(xiàn)網(wǎng)絡(luò)A 信息(SSID和密碼)發(fā)送到智能硬件
- 智能硬件連接 無(wú)線(xiàn)網(wǎng)絡(luò)A
- 連接成功后镊绪,無(wú)線(xiàn)網(wǎng)絡(luò)A 中 UDP 廣播分配后的IP地址和自身MAC地址
- 工作模式從AP模式切換為STA模式
二淆游、實(shí)驗(yàn)前提
需要用到TCP/UDP通信接口金度,查看 ESP8266學(xué)習(xí)筆記(5)——TCP/UDP接口使用
需要用到HTTP服務(wù)器接口逾柿,查看 ESP8266學(xué)習(xí)筆記(13)——HTTP服務(wù)器
需要用到JSON接口氛什,查看 ESP8266學(xué)習(xí)筆記(8)——第三方庫(kù)cJSON使用
三七芭、搭建HTTP服務(wù)器
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服務(wù)器TCP連接結(jié)構(gòu)體
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief HTTP服務(wù)器初始化
@param 無(wú)
@return 無(wú)
*/
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 監(jiān)聽(tīng)HTTP服務(wù)器
@param 無(wú)
@return 無(wú)
*/
void ICACHE_FLASH_ATTR
HttpServerListen(void)
{
espconn_accept(&s_httpSvrTcpEspconn);
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief 連接成功的回調(diào)函數(shù)
@param arg -[in] 指向傳遞給這個(gè)回調(diào)函數(shù)來(lái)使用的參數(shù)
@return 無(wú)
*/
static void ICACHE_FLASH_ATTR
connectCallback(void *arg)
{
struct espconn *pEspconn = arg;
espconn_regist_recvcb(pEspconn, receiveDataCallback);
espconn_regist_disconcb(pEspconn, disconnectCallback);
}
/**
@brief 接收數(shù)據(jù)的回調(diào)函數(shù)
@param arg -[in] 指向傳遞給這個(gè)回調(diào)函數(shù)來(lái)使用的參數(shù)
@param pData -[in] 接收的數(shù)據(jù)
@param len -[in] 接收的數(shù)據(jù)長(zhǎng)度
@return 無(wú)
*/
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 斷連的回調(diào)函數(shù)
@param arg -[in] 指向傳遞給這個(gè)回調(diào)函數(shù)來(lái)使用的參數(shù)
@return 無(wú)
*/
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 檢查數(shù)據(jù)完整性
@param pRecvData -[in] 接收的數(shù)據(jù)
@param recvDatalen -[in] 接收的數(shù)據(jù)長(zhǎng)度
@return 1 - 數(shù)據(jù)完整;0 - 數(shù)據(jù)缺失
*/
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] 接收的數(shù)據(jù)
@param pUrlFrame -[in&out] URL框架
@return 無(wú)
*/
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 查找請(qǐng)求數(shù)據(jù)
@param pRecvData -[in] 接收的數(shù)據(jù)
@param pRequestData -[in&out] 請(qǐng)求的數(shù)據(jù)
@return 無(wú)
*/
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請(qǐng)求URL路徑
@param pUrlFrame -[in] URL框架
@param pRequestData -[in] 請(qǐng)求的數(shù)據(jù)
@return 無(wú)
*/
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請(qǐng)求URL路徑
@param pUrlFrame -[in] URL框架
@param pRequestData -[in] 請(qǐng)求的數(shù)據(jù)
@return 無(wú)
*/
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] 請(qǐng)求的數(shù)據(jù)
@return 無(wú)
*/
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字段內(nèi)容
if(!pRequest)
{
os_sprintf(sendData, "%s", "No request Item");
sendPostResponse(false, sendData);
cJSON_Delete(pRoot);
return ;
}
cJSON *pStation = cJSON_GetObjectItem(pRequest, "station"); // 解析request子節(jié)點(diǎn)station字段內(nèi)容字段內(nèi)容
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發(fā)送設(shè)備信息
}
else
{
os_sprintf(sendData, "%s", "No station Item");
sendPostResponse(false, sendData);
}
cJSON_Delete(pRoot);
}
/**
@brief 發(fā)送POST請(qǐng)求的HTTP響應(yīng)
@param responseOk -[in] 響應(yīng)是否成功
@param pResponseData -[in] 響應(yīng)數(shù)據(jù)
@return 無(wú)
*/
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格式封裝響應(yīng)數(shù)據(jù)
@param responseOk -[in] 響應(yīng)是否成功
@param pSendData -[in&out] 要封裝的發(fā)送數(shù)據(jù)
@return 無(wú)
*/
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);
}
四照卦、配網(wǎng)
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 無(wú)
*/
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客戶(hù)端
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; // 設(shè)備信息UDP廣播結(jié)構(gòu)體
static os_timer_t s_udpDevInfoTimer; // UDP廣播設(shè)備信息的定時(shí)器
static uint8 s_udpDevInfoSendCount; // UDP廣播設(shè)備信息次數(shù)
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief UDP通訊初始化
@param 無(wú)
@return 無(wú)
*/
void ICACHE_FLASH_ATTR
UdpInit(void)
{
udpDeviceInfoEspconn();
udpBleDataEspconn();
}
/**
@brief UDP發(fā)送設(shè)備信息
@param 無(wú)
@return 無(wú)
*/
void ICACHE_FLASH_ATTR
UdpSendDeviceInfo(void)
{
startUdpDeviceInfoTimer();
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief UDP廣播設(shè)備信息
@param 無(wú)
@return 無(wú)
*/
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; // 目標(biāo)端口5050
espconn_regist_sentcb(&s_devInfoUdpEspconn, udpSendDataCallback);
espconn_create(&s_devInfoUdpEspconn);
startUdpDeviceInfoTimer();
}
/**
@brief UDP發(fā)送數(shù)據(jù)的回調(diào)函數(shù)
@param arg -[in] 指向傳遞給這個(gè)回調(diào)函數(shù)來(lái)使用的參數(shù)
@return 無(wú)
*/
static void ICACHE_FLASH_ATTR
udpSendDataCallback(void *arg)
{
}
/**
@brief 開(kāi)始UDP廣播設(shè)備信息的定時(shí)器
@param 無(wú)
@return 無(wú)
*/
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廣播設(shè)備信息的定時(shí)器
@param 無(wú)
@return 無(wú)
*/
static void ICACHE_FLASH_ATTR
stopUdpDeviceInfoTimer(void)
{
os_timer_disarm(&s_udpDevInfoTimer);
}
/**
@brief UDP廣播設(shè)備信息定時(shí)器的回調(diào)函數(shù)
@param 無(wú)
@return 無(wú)
*/
static void ICACHE_FLASH_ATTR
udpDeviceInfoTimerCallback(void)
{
uint8 connectApStatus = wifi_station_get_connect_status(); // 獲取STA模式下連接路由狀態(tài)
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); // 查詢(xún) WIFI被分配的IP地址
const char udpRemoteIp[4] = { 192, 168, ip4_addr3(&ipInfo.ip), 255 }; // 目標(biāo)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發(fā)送數(shù)據(jù)
s_udpDevInfoSendCount++;
}
}
/**
@brief JSON格式封裝設(shè)備信息
@param pSendData -[in&out] 要封裝的發(fā)送數(shù)據(jù)
@return 無(wú)
*/
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); // 查詢(xún) 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 寫(xiě)于 2019 年 9 月 10 日
? 參考:Esp8266學(xué)習(xí)之旅⑦ 了解softAP熱點(diǎn)配網(wǎng)模式原理
設(shè)備配網(wǎng)技術(shù)之AP配網(wǎng)