mirror of
https://github.com/Dominocs/Project_Cymple.git
synced 2025-09-26 23:49:11 +08:00
更新网页配置WIFI
This commit is contained in:
parent
35586ed170
commit
1faa831b7c
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmp19fxh6p9.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmp19fxh6p9.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmp5xsq82mt.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmp5xsq82mt.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpcgzm3_70.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpcgzm3_70.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpclktt78m.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpclktt78m.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpeu1d4lq7.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpeu1d4lq7.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpga0aipmo.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpga0aipmo.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpos7iqhf_.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpos7iqhf_.tmp
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpva8xpq_a.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpva8xpq_a.tmp
Normal file
File diff suppressed because one or more lines are too long
1
eyeTracker/software/.pio/build/esp32-cam/tmpvit4go9u.tmp
Normal file
1
eyeTracker/software/.pio/build/esp32-cam/tmpvit4go9u.tmp
Normal file
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
|||||||
50c0f5bda4c60df27adaa85711db1bb9473f451c
|
bee6574590ab9e3feca8d634c3f98e554415dd94
|
166
eyeTracker/software/src/wifiUser.cpp
Normal file
166
eyeTracker/software/src/wifiUser.cpp
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
#include "wlanMsg.h"
|
||||||
|
#include "wifiUser.h"
|
||||||
|
|
||||||
|
const byte DNS_PORT = 53; //设置DNS端口号
|
||||||
|
const int webPort = 80; //设置Web端口号
|
||||||
|
|
||||||
|
const char* HOST_NAME = "MY_ESP32"; //设置设备名
|
||||||
|
String scanNetworksID = ""; //用于储存扫描到的WiFi ID
|
||||||
|
|
||||||
|
String wifi_ssid = ""; //暂时存储wifi账号密码
|
||||||
|
String wifi_pass = ""; //暂时存储wifi账号密
|
||||||
|
|
||||||
|
DNSServer dnsServer; //创建dnsServer实例
|
||||||
|
WebServer server(webPort); //开启web服务, 创建TCP SERVER,参数: 端口号,最大连接数
|
||||||
|
|
||||||
|
// 上下两段HTML代码
|
||||||
|
String ROOT_HTML_1 = "<!DOCTYPE html><html><head> <meta charset=\"UTF-8\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /> <title>登录页面</title> <style> #content,.login,.login-card a,.login-card h1,.login-help{text-align:center}body,html{margin:0;padding:0;width:100%;height:100%;display:table}#content{font-family:\'Source Sans Pro\',sans-serif;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;display:table-cell;vertical-align:middle}.login-card{padding:40px;width:274px;background-color:#F7F7F7;margin:0 auto 10px;border-radius:20px;box-shadow:8px 8px 15px rgba(0,0,0,.3);overflow:hidden}.login-card h1{font-weight:400;font-size:2.3em;color:#1383c6}.login-card h1 span{color:#f26721}.login-card img{width:70%;height:70%}.login-card input[type=submit]{width:100%;display:block;margin-bottom:10px;position:relative}.login-card input[type=text],input[type=password]{height:44px;font-size:16px;width:100%;margin-bottom:10px;-webkit-appearance:none;background:#fff;border:1px solid #d9d9d9;border-top:1px solid silver;padding:0 8px;box-sizing:border-box;-moz-box-sizing:border-box}.login-card input[type=text]:hover,input[type=password]:hover{border:1px solid #b9b9b9;border-top:1px solid #a0a0a0;-moz-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);-webkit-box-shadow:inset 0 1px 2px rgba(0,0,0,.1);box-shadow:inset 0 1px 2px rgba(0,0,0,.1)}.login{font-size:14px;font-family:Arial,sans-serif;font-weight:700;height:36px;padding:0 8px}.login-submit{-webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;color:#fff;text-shadow:0 1px rgba(0,0,0,.1);background-color:#4d90fe}.login-submit:disabled{opacity:.6}.login-submit:hover{border:0;text-shadow:0 1px rgba(0,0,0,.3);background-color:#357ae8}.login-card a{text-decoration:none;color:#666;font-weight:400;display:inline-block;opacity:.6;transition:opacity ease .5s}.login-card a:hover{opacity:1}.login-help{width:100%;font-size:12px}.list{list-style-type:none;padding:0}.list__item{margin:0 0 .7rem;padding:0}label{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-align:center;-webkit-align-items:center;-ms-flex-align:center;align-items:center;text-align:left;font-size:14px;}input[type=checkbox]{-webkit-box-flex:0;-webkit-flex:none;-ms-flex:none;flex:none;margin-right:10px;float:left}.error{font-size:14px;font-family:Arial,sans-serif;font-weight:700;height:25px;padding:0 8px;padding-top: 10px; -webkit-appearance:none;-moz-appearance:none;appearance:none;border:0;color:#fff;text-shadow:0 1px rgba(0,0,0,.1);background-color:#ff1215}@media screen and (max-width:450px){.login-card{width:70%!important}.login-card img{width:30%;height:30%}} </style></head><body style=\"background-color: #e5e9f2\"><div id=\"content\"> <form name=\'input\' action=\'/configwifi\' method=\'POST\'> <div class=\"login-card\"> <h1>WiFi登录</h1> <form name=\"login_form\" method=\"post\" action=\"$PORTAL_ACTION$\"> <input type=\"text\" name=\"ssid\" placeholder=\"请输入 WiFi 名称\" id=\"auth_user\" list = \"data-list\"; style=\"border-radius: 10px\"> <datalist id = \"data-list\">";
|
||||||
|
String ROOT_HTML_2 = "<input type=\"password\" name=\"password\" placeholder=\"请输入 WiFi 密码\" id=\"auth_pass\"; style=\"border-radius: 10px\"> <div class=\"login-help\"> <ul class=\"list\"> <li class=\"list__item\"> </li> </ul> </div> <input type=\"submit\" class=\"login login-submit\" value=\"确 定 连 接\" id=\"login\"; disabled; style=\"border-radius: 15px\" > </form> <!-- <form name=\'input\' action=\'/English\' method=\'POST\'> <input type=\"submit\" class=\"login login-submit\" value=\"English\" id=\"login\"; disabled; style=\"border-radius: 15px\" > </form> --></body></html>";
|
||||||
|
/*
|
||||||
|
* 处理网站根目录的访问请求
|
||||||
|
*/
|
||||||
|
void handleRoot()
|
||||||
|
{
|
||||||
|
if (server.hasArg("selectSSID")) {
|
||||||
|
server.send(200, "text/html", ROOT_HTML_1 + scanNetworksID + ROOT_HTML_2); //scanNetWprksID是扫描到的wifi
|
||||||
|
} else {
|
||||||
|
server.send(200, "text/html", ROOT_HTML_1 + scanNetworksID + ROOT_HTML_2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 提交数据后的提示页面
|
||||||
|
*/
|
||||||
|
void handleConfigWifi() //返回http状态
|
||||||
|
{
|
||||||
|
if (server.hasArg("ssid")) //判断是否有账号参数
|
||||||
|
{
|
||||||
|
Serial.print("got ssid:");
|
||||||
|
wifi_ssid = server.arg("ssid"); //获取html表单输入框name名为"ssid"的内容
|
||||||
|
|
||||||
|
Serial.println(wifi_ssid);
|
||||||
|
}
|
||||||
|
else //没有参数
|
||||||
|
{
|
||||||
|
Serial.println("error, not found ssid");
|
||||||
|
server.send(200, "text/html", "<meta charset='UTF-8'>error, not found ssid"); //返回错误页面
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//密码与账号同理
|
||||||
|
if (server.hasArg("password"))
|
||||||
|
{
|
||||||
|
Serial.print("got password:");
|
||||||
|
wifi_pass = server.arg("password"); //获取html表单输入框name名为"pwd"的内容
|
||||||
|
Serial.println(wifi_pass);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("error, not found password");
|
||||||
|
server.send(200, "text/html", "<meta charset='UTF-8'>error, not found password");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
server.send(200, "text/html", "<meta charset='UTF-8'>SSID: " + wifi_ssid + "<br />password:" + wifi_pass + "<br />已取得WiFi信息,正在尝试连接,请手动关闭此页面。"); //返回保存成功页面
|
||||||
|
WiFi.softAPdisconnect(true); //参数设置为true,设备将直接关闭接入点模式,即关闭设备所建立的WiFi网络。
|
||||||
|
server.close(); //关闭web服务
|
||||||
|
|
||||||
|
if (WiFi.status() != WL_CONNECTED) //wifi没有连接成功
|
||||||
|
{
|
||||||
|
pwlanMsgObj->connect(wifi_ssid.c_str(), wifi_pass.c_str());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("提交的配置信息自动连接成功..");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 处理404情况的函数'handleNotFound'
|
||||||
|
*/
|
||||||
|
void handleNotFound() // 当浏览器请求的网络资源无法在服务器找到时通过此自定义函数处理
|
||||||
|
{
|
||||||
|
handleRoot(); //访问不存在目录则返回配置页面
|
||||||
|
// server.send(404, "text/plain", "404: Not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 开启DNS服务器
|
||||||
|
*/
|
||||||
|
void initDNS()
|
||||||
|
{
|
||||||
|
if (dnsServer.start(DNS_PORT, "*", apIP)) //判断将所有地址映射到esp32的ip上是否成功
|
||||||
|
{
|
||||||
|
Serial.println("start dnsserver success.");
|
||||||
|
} else {
|
||||||
|
Serial.println("start dnsserver failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 初始化WebServer
|
||||||
|
*/
|
||||||
|
void initWebServer()
|
||||||
|
{
|
||||||
|
if (MDNS.begin("esp32")) //给设备设定域名esp32,完整的域名是esp32.local
|
||||||
|
{
|
||||||
|
Serial.println("MDNS responder started");
|
||||||
|
}
|
||||||
|
//必须添加第二个参数HTTP_GET,以下面这种格式去写,否则无法强制门户
|
||||||
|
server.on("/", HTTP_GET, handleRoot); // 当浏览器请求服务器根目录(网站首页)时调用自定义函数handleRoot处理,设置主页回调函数,必须添加第二个参数HTTP_GET,否则无法强制门户
|
||||||
|
server.on("/configwifi", HTTP_POST, handleConfigWifi); // 当浏览器请求服务器/configwifi(表单字段)目录时调用自定义函数handleConfigWifi处理
|
||||||
|
|
||||||
|
server.onNotFound(handleNotFound); //当浏览器请求的网络资源无法在服务器找到时调用自定义函数handleNotFound处理
|
||||||
|
|
||||||
|
server.begin(); //启动TCP SERVER
|
||||||
|
|
||||||
|
Serial.println("WebServer started!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 扫描附近的WiFi,为了显示在配网界面
|
||||||
|
*/
|
||||||
|
bool scanWiFi() {
|
||||||
|
Serial.println("scan start");
|
||||||
|
Serial.println("--------->");
|
||||||
|
// 扫描附近WiFi
|
||||||
|
int n = WiFi.scanNetworks();
|
||||||
|
Serial.println("scan done");
|
||||||
|
if (n == 0) {
|
||||||
|
Serial.println("no networks found");
|
||||||
|
scanNetworksID += "<option>no networks found</option>";
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
Serial.print(n);
|
||||||
|
Serial.println(" networks found");
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
// Print SSID and RSSI for each network found
|
||||||
|
Serial.print(i + 1);
|
||||||
|
Serial.print(": ");
|
||||||
|
Serial.print(WiFi.SSID(i));
|
||||||
|
Serial.print(" (");
|
||||||
|
Serial.print(WiFi.RSSI(i));
|
||||||
|
Serial.print(")");
|
||||||
|
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
|
||||||
|
scanNetworksID += "<option>" + WiFi.SSID(i) + "</option>";
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
scanNetworksID += "</datalist>";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* 配置配网功能
|
||||||
|
*/
|
||||||
|
void wifiConfig()
|
||||||
|
{
|
||||||
|
initDNS();
|
||||||
|
initWebServer();
|
||||||
|
scanWiFi();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 检测客户端DNS&HTTP请求
|
||||||
|
*/
|
||||||
|
void checkDNS_HTTP()
|
||||||
|
{
|
||||||
|
dnsServer.processNextRequest(); //检查客户端DNS请求
|
||||||
|
server.handleClient(); //检查客户端(浏览器)http请求
|
||||||
|
}
|
22
eyeTracker/software/src/wifiUser.h
Normal file
22
eyeTracker/software/src/wifiUser.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef __WIFIUSER_H__
|
||||||
|
#define __WIFIUSER_H__
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <DNSServer.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include <ESPmDNS.h> //用于设备域名 MDNS.begin("esp32")
|
||||||
|
#include <esp_wifi.h> //用于esp_wifi_restore() 删除保存的wifi信息
|
||||||
|
|
||||||
|
//===========需要调用的函数===========
|
||||||
|
void checkDNS_HTTP(); //检测客户端DNS&HTTP请求
|
||||||
|
|
||||||
|
//===========内部函数===========
|
||||||
|
void handleRoot(); //处理网站根目录的访问请求
|
||||||
|
void handleConfigWifi() ; //提交数据后的提示页面
|
||||||
|
void handleNotFound(); //处理404情况的函数'handleNotFound'
|
||||||
|
void initDNS(); //开启DNS服务器
|
||||||
|
void initWebServer(); //初始化WebServer
|
||||||
|
bool scanWiFi(); //扫描附近的WiFi,为了显示在配网界面
|
||||||
|
void wifiConfig(); //配置配网功能
|
||||||
|
|
||||||
|
#endif
|
@ -5,6 +5,8 @@
|
|||||||
#include "esp32cam.h"
|
#include "esp32cam.h"
|
||||||
#include "wlanMsg.h"
|
#include "wlanMsg.h"
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
|
#include "wifiUser.h"
|
||||||
|
|
||||||
#define WIFI_MAX_TX_POWER 80
|
#define WIFI_MAX_TX_POWER 80
|
||||||
wlanMsgClass *pwlanMsgObj = NULL;
|
wlanMsgClass *pwlanMsgObj = NULL;
|
||||||
IPAddress peerAddr;
|
IPAddress peerAddr;
|
||||||
@ -119,6 +121,8 @@ void wlanMsgClass::APMode(){
|
|||||||
WiFi.mode(WIFI_AP);
|
WiFi.mode(WIFI_AP);
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.softAP(AP_NAME);
|
WiFi.softAP(AP_NAME);
|
||||||
|
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); //设置AP热点IP和子网掩码
|
||||||
|
wifiConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wlanMsgClass::send(uint8_t *data, size_t len, IPAddress ip, uint16_t port){
|
void wlanMsgClass::send(uint8_t *data, size_t len, IPAddress ip, uint16_t port){
|
||||||
@ -140,6 +144,8 @@ int wlanMsgClass::runFrame(unsigned long currentT){
|
|||||||
APMode();
|
APMode();
|
||||||
tryConCount++;
|
tryConCount++;
|
||||||
}
|
}
|
||||||
|
checkDNS_HTTP(); //检测客户端DNS&HTTP请求,也就是检查配网页面那部分
|
||||||
|
delay(30);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
long long deltaT = (long long)currentT - heartBeatTimer;
|
long long deltaT = (long long)currentT - heartBeatTimer;
|
||||||
|
@ -53,7 +53,7 @@ public:
|
|||||||
int runFrame(unsigned long currentT);
|
int runFrame(unsigned long currentT);
|
||||||
void APMode();
|
void APMode();
|
||||||
};
|
};
|
||||||
|
static IPAddress apIP(192, 168, 4, 1); //设置AP的IP地址
|
||||||
extern wlanMsgClass *pwlanMsgObj;
|
extern wlanMsgClass *pwlanMsgObj;
|
||||||
extern uint8_t ucFlag2;
|
extern uint8_t ucFlag2;
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user