|
@@ -15,6 +15,7 @@ void OTAUpdater::checkForUpdates() {
|
|
|
while (true) {
|
|
while (true) {
|
|
|
// 尝试连接 WiFi,超时时间为 30 秒
|
|
// 尝试连接 WiFi,超时时间为 30 秒
|
|
|
if (connectWiFiWithTimeout(30000)) {
|
|
if (connectWiFiWithTimeout(30000)) {
|
|
|
|
|
+ Serial.println("[信息] Wi-Fi 连接成功。");
|
|
|
break; // 连接成功,退出循环
|
|
break; // 连接成功,退出循环
|
|
|
} else {
|
|
} else {
|
|
|
Serial.println("[信息] Wi-Fi 连接失败,进入配置模式...");
|
|
Serial.println("[信息] Wi-Fi 连接失败,进入配置模式...");
|
|
@@ -25,45 +26,44 @@ void OTAUpdater::checkForUpdates() {
|
|
|
Serial.println("正在检查更新...");
|
|
Serial.println("正在检查更新...");
|
|
|
|
|
|
|
|
// 构建完整的 OTA 服务器 URL,使用全局变量 serverAddress 和 _port
|
|
// 构建完整的 OTA 服务器 URL,使用全局变量 serverAddress 和 _port
|
|
|
- String fullServerUrl = "http://" + serverAddress + ":" + String(_port);
|
|
|
|
|
|
|
+ String fullServerUrl = "http://" + serverAddress + ":" + otaPort;
|
|
|
Serial.println("OTA 服务器地址: " + fullServerUrl);
|
|
Serial.println("OTA 服务器地址: " + fullServerUrl);
|
|
|
|
|
|
|
|
// 向服务器发送设备ID和当前固件版本,检查是否有更新
|
|
// 向服务器发送设备ID和当前固件版本,检查是否有更新
|
|
|
HTTPClient http;
|
|
HTTPClient http;
|
|
|
String checkUpdateUrl = fullServerUrl + "/check-update";
|
|
String checkUpdateUrl = fullServerUrl + "/check-update";
|
|
|
http.begin(checkUpdateUrl);
|
|
http.begin(checkUpdateUrl);
|
|
|
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
|
-
|
|
|
|
|
- // 发送设备ID和当前固件版本
|
|
|
|
|
- String postData = "device_id=" + deviceID + "&version=" + _currentVersion;
|
|
|
|
|
- int httpCode = http.POST(postData);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ http.addHeader("Content-Type", "application/json");
|
|
|
|
|
+
|
|
|
|
|
+ // 构建JSON请求体
|
|
|
|
|
+String json = "{\"device_id\":\"" + deviceID + "\",\"version\":\"" + _currentVersion + "\"}";
|
|
|
|
|
+http.addHeader("Content-Type", "application/json");
|
|
|
|
|
+int httpCode = http.POST(json);
|
|
|
|
|
+
|
|
|
|
|
+ // 处理响应
|
|
|
if (httpCode == HTTP_CODE_OK) {
|
|
if (httpCode == HTTP_CODE_OK) {
|
|
|
String response = http.getString();
|
|
String response = http.getString();
|
|
|
- Serial.println("服务器响应: " + response);
|
|
|
|
|
-
|
|
|
|
|
- // 如果服务器返回"update",则下载固件
|
|
|
|
|
if (response == "update") {
|
|
if (response == "update") {
|
|
|
- Serial.println("发现新固件,正在下载...");
|
|
|
|
|
|
|
+ Serial.println("发现新固件,开始下载...");
|
|
|
if (downloadAndUpdateFirmware()) {
|
|
if (downloadAndUpdateFirmware()) {
|
|
|
- Serial.println("固件更新完成,即将重启...");
|
|
|
|
|
|
|
+ Serial.println("固件更新成功,即将重启...");
|
|
|
|
|
+ delay(1000);
|
|
|
|
|
+ Serial.println("1秒后重启...");
|
|
|
|
|
+ delay(1000);
|
|
|
ESP.restart();
|
|
ESP.restart();
|
|
|
- } else {
|
|
|
|
|
- Serial.println("固件更新失败。");
|
|
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- Serial.println("无可用更新。");
|
|
|
|
|
|
|
+ Serial.println("无可用更新");
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
Serial.println("检查更新失败,HTTP错误代码: " + String(httpCode));
|
|
Serial.println("检查更新失败,HTTP错误代码: " + String(httpCode));
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
http.end();
|
|
http.end();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool OTAUpdater::downloadAndUpdateFirmware() {
|
|
bool OTAUpdater::downloadAndUpdateFirmware() {
|
|
|
// 构建完整的 OTA 服务器 URL,使用全局变量 serverAddress 和 _port
|
|
// 构建完整的 OTA 服务器 URL,使用全局变量 serverAddress 和 _port
|
|
|
- String fullServerUrl = "http://" + serverAddress + ":" + String(_port);
|
|
|
|
|
|
|
+ String fullServerUrl = "http://" + serverAddress + ":" + otaPort;
|
|
|
String firmwareUrl = fullServerUrl + "/firmware";
|
|
String firmwareUrl = fullServerUrl + "/firmware";
|
|
|
|
|
|
|
|
HTTPClient http;
|
|
HTTPClient http;
|