add air firmware upgrade support

This commit is contained in:
Dmitry Borisenko
2020-03-04 00:27:35 +03:00
parent a6117db46e
commit f8a50e09b7
5 changed files with 161 additions and 61 deletions

View File

@@ -1,79 +1,158 @@
void initUpgrade() { void initUpgrade() {
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest * request) { server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest * request) {
Serial.println("start update..."); #ifdef ESP32
new_version = getURL("http://91.204.228.124:1100/update/esp32/version.txt");
#endif
#ifdef ESP8266
new_version = getURL("http://91.204.228.124:1100/update/esp8266/version.txt");
#endif
//WiFiClient client_for; Serial.println(new_version);
String tmp = "{}";
//httpUpdate.rebootOnUpdate(false); if (new_version != "error") {
if (new_version == firmware_version) {
jsonWrite(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>Последняя версия прошивки уже установлена.");
jsonWrite(tmp, "class", "pop-up");
} else {
upgrade_flag = true;
jsonWrite(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>Идет обновление прошивки... После завершения устройство перезагрузится.");
jsonWrite(tmp, "class", "pop-up");
}
} else {
jsonWrite(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>Ошибка... Cервер не найден. Попробуйте позже...");
jsonWrite(tmp, "class", "pop-up");
}
//t_httpUpdate_return ret = httpUpdate.updateSpiffs(client_for, "http://91.204.228.124:1100/update/esp32/esp32-esp8266_iot-manager_modules_firmware.spiffs.bin"); request->send(200, "text/text", tmp);
//if (ret == HTTP_UPDATE_OK) {
Serial.println("update Spiffs done!");
//t_httpUpdate_return ret = httpUpdate.update(client_for, "http://91.204.228.124:1100/update/esp32/esp32-esp8266_iot-manager_modules_firmware.ino.bin");
// switch (ret) {
// case HTTP_UPDATE_FAILED:
// Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
// break;
// case HTTP_UPDATE_NO_UPDATES:
// Serial.println("HTTP_UPDATE_NO_UPDATES");
// break;
// case HTTP_UPDATE_OK:
// Serial.println("HTTP_UPDATE_OK");
// break;
// }
// }
//UpdateStatus(ret);
/* #ifdef ESP8266
String new_version = getURL("http://91.204.228.124:1100/update/esp8266/version.txt");
#endif
#ifdef ESP32
String new_version = getURL("http://91.204.228.124:1100/update/esp32/version.txt");
#endif
Serial.println(new_version);
String tmp = "{}";
if (new_version == firmware_version) {
jsonWrite(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button> Установленна последняя версия: " + new_version);
jsonWrite(tmp, "class", "pop-up");
} else {
jsonWrite(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button> Начитаню обновление до версии: " + new_version);
jsonWrite(tmp, "class", "pop-up");
webUpgrade();
}*/
request->send(200, "text/text", "ok");
}); });
} }
void UpdateStatus(t_httpUpdate_return set) { void upgrade_firmware() {
String scenario_for_update;
String config_for_update;
String configSetup_for_update;
scenario_for_update = readFile("firmware.scenario.txt", 2048);
config_for_update = readFile("firmware.config.txt", 2048);
configSetup_for_update = configSetup;
Serial.println("Start upgrade SPIFFS, please wait...");
WiFiClient client_for_upgrade;
#ifdef ESP32
httpUpdate.rebootOnUpdate(false);
t_httpUpdate_return ret = httpUpdate.updateSpiffs(client_for_upgrade, "http://91.204.228.124:1100/update/esp32/esp32-esp8266_iot-manager_modules_firmware.spiffs.bin");
#endif
#ifdef ESP8266
ESPhttpUpdate.rebootOnUpdate(false);
t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(client_for_upgrade, "http://91.204.228.124:1100/update/esp8266/esp32-esp8266_iot-manager_modules_firmware.spiffs.bin");
#endif
if (ret == HTTP_UPDATE_OK) {
writeFile("firmware.scenario.txt", scenario_for_update);
writeFile("firmware.config.txt", config_for_update);
writeFile("config.json", configSetup_for_update);
saveConfig();
Serial.println("SPIFFS upgrade done!");
Serial.println("Start upgrade BUILD, please wait...");
#ifdef ESP32
httpUpdate.rebootOnUpdate(true);
t_httpUpdate_return ret = httpUpdate.update(client_for_upgrade, "http://91.204.228.124:1100/update/esp32/esp32-esp8266_iot-manager_modules_firmware.ino.bin");
#endif
#ifdef ESP8266
ESPhttpUpdate.rebootOnUpdate(true);
t_httpUpdate_return ret = ESPhttpUpdate.update(client_for_upgrade, "http://91.204.228.124:1100/update/esp8266/esp32-esp8266_iot-manager_modules_firmware.ino.bin");
#endif
Serial.println("BUILD upgrade done!");
Serial.println("Restart ESP....");
} else {
//upgrade_status(t_httpUpdate_return ret);
}
}
void handle_upgrade() {
if (upgrade_flag) {
upgrade_flag = false;
upgrade_firmware();
}
}
/*
void upgrade_status(t_httpUpdate_return set) {
switch (set) { switch (set) {
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str()); Serial.printf("UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
break; break;
case HTTP_UPDATE_NO_UPDATES: case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES"); Serial.println("NO_UPDATES");
break; break;
case HTTP_UPDATE_OK: case HTTP_UPDATE_OK:
Serial.println("HTTP_UPDATE_OK"); Serial.println("HTTP_UPDATE_OK");
break; break;
} }
} }
*/
/* /*
// ----------------------- Обновление с сайта // ----------------------- Обновление с сайта
void webUpgrade() { void webUpgrade() {

View File

@@ -19,6 +19,18 @@
"type": "h4", "type": "h4",
"title": "IP address: {{ip}}" "title": "IP address: {{ip}}"
}, },
{
"type": "h4",
"title": "Uptime: {{uptime}}"
},
{
"type": "h4",
"title": "Build version: {{firmware_version}}"
},
{
"type": "h4",
"title": "SPIFFS version: 2.2"
},
{ {
"type": "link", "type": "link",
"title": "Конфигурация устройства", "title": "Конфигурация устройства",

View File

@@ -25,6 +25,8 @@ void setup() {
Serial.setDebugOutput(0); Serial.setDebugOutput(0);
#endif #endif
jsonWrite(configSetup, "firmware_version", firmware_version);
prex = prefix + "/" + chipID; prex = prefix + "/" + chipID;
Serial.println(chipID); Serial.println(chipID);
//-------------------------------------------------------------- //--------------------------------------------------------------
@@ -58,10 +60,11 @@ void setup() {
getMemoryLoad("[i] After loading"); getMemoryLoad("[i] After loading");
// ts.add(TEST, 20000, [&](void*) { ts.add(TEST, 14400, [&](void*) {
// }, nullptr, true); statistics();
}, nullptr, false);
} }
void loop() { void loop() {
@@ -81,4 +84,5 @@ void loop() {
handleScenario(); handleScenario();
ts.update(); ts.update();
handle_upgrade();
} }

View File

@@ -137,8 +137,7 @@ String getURL(String urls) {
if (httpCode == HTTP_CODE_OK) { if (httpCode == HTTP_CODE_OK) {
answer = http.getString(); answer = http.getString();
} }
else else {
{
answer = "error"; answer = "error";
} }
http.end(); http.end();

8
set.h
View File

@@ -1,4 +1,6 @@
String firmware_version = "2.2"; String firmware_version = "2.2";
String new_version;
//#define OTA_enable //#define OTA_enable
//#define MDNS_enable //#define MDNS_enable
@@ -71,7 +73,7 @@ AsyncEventSource events("/events");
#include "time.h" #include "time.h"
#include <TickerScheduler.h> #include <TickerScheduler.h>
TickerScheduler ts(30); TickerScheduler ts(30);
enum { ROUTER_SEARCHING, WIFI_MQTT_CONNECTION_CHECK, LEVEL, ANALOG_, DALLAS, DHTT, DHTH, DHTC, DHTP, DHTD, ANALOG_LOG, LEVEL_LOG, DALLAS_LOG, PH_LOG, CMD , TIMER_COUNTDOWN, TIMERS, TIME, TEST}; enum {ROUTER_SEARCHING, WIFI_MQTT_CONNECTION_CHECK, LEVEL, ANALOG_, DALLAS, DHTT, DHTH, DHTC, DHTP, DHTD, ANALOG_LOG, LEVEL_LOG, DALLAS_LOG, PH_LOG, CMD , TIMER_COUNTDOWN, TIMERS, TIME, TEST};
//ssl//#include "dependencies/WiFiClientSecure/WiFiClientSecure.h" //using older WiFiClientSecure //ssl//#include "dependencies/WiFiClientSecure/WiFiClientSecure.h" //using older WiFiClientSecure
#include <PubSubClient.h> #include <PubSubClient.h>
@@ -136,3 +138,7 @@ int wifi_lost_error = 0;
int mqtt_lost_error = -1; int mqtt_lost_error = -1;
String var; String var;
boolean upgrade_flag = false;
boolean get_url_flag = false;