145 lines
5.0 KiB
C++
145 lines
5.0 KiB
C++
#include <WiFiConfig/Config.h>
|
||
#include <Switch/Switch.h>
|
||
|
||
Switch 状态指示灯_类(D4,0,LOW);
|
||
IPAddress staticIP(192, 168, 1, 22);
|
||
IPAddress gateway(192, 168, 1, 1);
|
||
IPAddress subnet(255, 255, 255, 0);
|
||
ESP8266WebServer server(80); // 创建一个Web服务器对象,监听端口为80
|
||
|
||
static unsigned long ota_progress_millis = 0;
|
||
WiFiConfig::WiFiConfig(int 自动连接等待时间):等待时间(自动连接等待时间)
|
||
{
|
||
|
||
}
|
||
WiFiConfig::~WiFiConfig()
|
||
{
|
||
|
||
}
|
||
/**
|
||
* 一键配网关键代码
|
||
*/
|
||
void WiFiConfig::智能配网()
|
||
{
|
||
WiFi.mode(WIFI_STA);
|
||
串口调试("\n正在执行智能配网");
|
||
延时(500);
|
||
// 等待配网
|
||
WiFi.beginSmartConfig();
|
||
|
||
无限循环
|
||
{
|
||
串口调试(".");
|
||
delay(500);
|
||
状态指示灯_类.切换状态();
|
||
if (WiFi.smartConfigDone())
|
||
{
|
||
串口调试("\n配网完成!WiFi名称:[%s] WiFi密码:[%s]\n",WiFi.SSID().c_str(),WiFi.psk().c_str());
|
||
WiFi.setAutoConnect(true); // 设置自动连接
|
||
break;
|
||
}
|
||
}
|
||
串口调试("\nWiFi连接成功,IP地址:[%s],Mac地址:[%s]\n",WiFi.localIP().toString().c_str(),WiFi.macAddress().c_str());
|
||
状态指示灯_类.关闭();
|
||
}
|
||
|
||
boolean WiFiConfig::自动联网()
|
||
{
|
||
WiFi.begin();
|
||
//如果觉得时间太长可改
|
||
for (int i = 0; i < 等待时间; i++)
|
||
{
|
||
for (size_t j = 0; i < 255; j++)
|
||
{
|
||
状态指示灯_类.设置输出PWM(j++);
|
||
if (WiFi.status() == WL_CONNECTED)
|
||
{
|
||
//WiFi.config(staticIP,gateway,subnet);
|
||
|
||
串口调试("\nWiFi连接成功,WiFi名称:[%s] WiFi密码:[%s],IP地址:[%s],网关:[%s],Mac地址:[%s]\n",
|
||
WiFi.SSID().c_str(),
|
||
WiFi.psk().c_str(),
|
||
WiFi.localIP().toString().c_str(),
|
||
WiFi.gatewayIP().toString().c_str(),
|
||
WiFi.macAddress().c_str());
|
||
|
||
return true;
|
||
}
|
||
}
|
||
for (size_t j = 255; i > 0; j--)
|
||
{
|
||
状态指示灯_类.设置输出PWM(j++);
|
||
if (WiFi.status() == WL_CONNECTED)
|
||
{
|
||
//WiFi.config(staticIP,gateway,subnet);
|
||
|
||
串口调试("\nWiFi连接成功,WiFi名称:[%s] WiFi密码:[%s],IP地址:[%s],网关:[%s],Mac地址:[%s]\n",
|
||
WiFi.SSID().c_str(),
|
||
WiFi.psk().c_str(),
|
||
WiFi.localIP().toString().c_str(),
|
||
WiFi.gatewayIP().toString().c_str(),
|
||
WiFi.macAddress().c_str());
|
||
|
||
return true;
|
||
}
|
||
}
|
||
串口调试("\nWiFi自动配置,超时等待:[%d/%d]",i,等待时间);
|
||
}
|
||
串口打印换行("WiFi自动配置失败!" );
|
||
return false;
|
||
}
|
||
void WiFiConfig::网络升级初始化(){
|
||
// 定义根路径("/")的处理函数
|
||
server.on("/", []() {
|
||
// 使用text/html类型,并在头部指定charset=utf-8
|
||
String html = "<!DOCTYPE html><html><head>";
|
||
html += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
|
||
// 添加一些简单的样式让按钮更美观
|
||
html += "<style>";
|
||
html += "body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }";
|
||
html += ".update-btn { padding: 12px 24px; font-size: 18px; background-color: #4CAF50; color: white;";
|
||
html += " border: none; border-radius: 4px; cursor: pointer; text-decoration: none; }";
|
||
html += ".update-btn:hover { background-color: #45a049; }";
|
||
html += "</style>";
|
||
html += "</head><body>";
|
||
html += "<h1>您好! 这是 狼魂 OTA升级。</h1>";
|
||
// 添加跳转按钮,链接到当前IP的/update页面
|
||
html += "<a href=\"/update\" class=\"update-btn\">点击进行固件升级</a>";
|
||
html += "</body></html>";
|
||
server.send(200, "text/html", html);
|
||
});
|
||
ElegantOTA.setAutoReboot(true);
|
||
ElegantOTA.begin(&server); // 初始化ElegantOTA,传入WebServer实例
|
||
// 设置OTA回调函数
|
||
ElegantOTA.onStart(&WiFiConfig::onOTAStart);
|
||
ElegantOTA.onProgress(&WiFiConfig::onOTAProgress);
|
||
ElegantOTA.onEnd(&WiFiConfig::onOTAEnd); // 注册OTA结束时的回调
|
||
|
||
server.begin(); // 启动Web服务器
|
||
Serial.println("HTTP服务器已启动");
|
||
}
|
||
void WiFiConfig::网络升级(){
|
||
server.handleClient(); // 处理客户端请求
|
||
ElegantOTA.loop(); // 处理OTA相关任务
|
||
}
|
||
|
||
|
||
void WiFiConfig::onOTAStart() {
|
||
Serial.println("OTA更新开始!");
|
||
}
|
||
|
||
void WiFiConfig::onOTAProgress(size_t current, size_t final) {
|
||
if (millis() - ota_progress_millis > 1000) {
|
||
ota_progress_millis = millis();
|
||
Serial.printf("OTA进度 当前: %u 字节, 总大小: %u 字节\n", current, final);
|
||
}
|
||
}
|
||
|
||
void WiFiConfig::onOTAEnd(bool success) {
|
||
if (success) {
|
||
Serial.println("OTA更新成功完成!");
|
||
|
||
} else {
|
||
Serial.println("OTA更新过程中出现错误!");
|
||
}
|
||
} |