добавлено OTA и взаимодействие с сервером

This commit is contained in:
Dmitry Borisenko
2022-02-18 20:06:25 +01:00
parent bd30cbdde2
commit 2366e027b9
7 changed files with 158 additions and 7 deletions

File diff suppressed because one or more lines are too long

View File

@@ -3,8 +3,8 @@
"name": "IoTmanagerVer4",
"apssid": "IoTmanager",
"appass": "",
"routerssid": "rise",
"routerpass": "hostel3333",
"routerssid": "WLAN1-Y1GYEF",
"routerpass": "2egY69YTA8DDR7En",
"timezone": 1,
"ntp": "pool.ntp.org",
"weblogin": "admin",
@@ -13,5 +13,6 @@
"mqttPort": 8021,
"mqttPrefix": "/ver4test",
"mqttUser": "rise",
"mqttPass": "hostel3333"
"mqttPass": "hostel3333",
"serverip": "http://206.189.49.244:8081"
}

View File

@@ -1,7 +1,7 @@
#pragma once
//Это версия прошивки
#define FIRMWARE_VERSION 404
//Версия прошивки
#define FIRMWARE_VERSION 405
#ifdef esp8266_4mb
#define FIRMWARE_NAME "esp8266_4mb"

24
include/UpgradeFirm.h Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include "Global.h"
//#include "Upgrade.h"
#ifdef ESP8266
//#include "ESP8266.h"
#else
#include <HTTPUpdate.h>
#endif
struct updateFirm {
String settingsFlashJson;
String configJson;
};
extern void upgradeInit();
extern void getLastVersion();
extern void upgrade_firmware(int type);
extern bool upgradeFS();
extern bool upgradeBuild();
extern void restartEsp();
extern const String getBinPath(String file);
extern void putUserDataToRam();
extern void saveUserDataToFlash();

View File

@@ -3,6 +3,7 @@
#include "utils/WiFiUtils.h"
#include "DeviceList.h"
#include "ESPConfiguration.h"
#include "UpgradeFirm.h"
#ifdef STANDARD_WEB_SOCKETS
extern void standWebSocketsInit();

120
src/UpgradeFirm.cpp Normal file
View File

@@ -0,0 +1,120 @@
#include "UpgradeFirm.h"
updateFirm update;
void upgrade_firmware(int type) {
putUserDataToRam();
// only build
if (type == 1) {
if (upgradeBuild()) {
saveUserDataToFlash();
restartEsp();
}
}
// only littlefs
else if (type == 2) {
if (upgradeFS()) {
saveUserDataToFlash();
restartEsp();
}
}
// littlefs and build
else if (type == 3) {
if (upgradeFS()) {
saveUserDataToFlash();
if (upgradeBuild()) {
restartEsp();
}
}
}
}
bool upgradeFS() {
bool ret = false;
WiFiClient wifiClient;
Serial.printf("Start upgrade %s, please wait...", FS_NAME);
if (getBinPath("") == "error") {
SerialPrint("E", F("Update"), F("FS Path error"));
return ret;
}
#ifdef ESP8266
ESPhttpUpdate.rebootOnUpdate(false);
t_httpUpdate_return retFS = ESPhttpUpdate.updateFS(wifiClient, getBinPath("littlefs.bin"));
#endif
#ifdef ESP32
httpUpdate.rebootOnUpdate(false);
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, getBinPath("spiffs.bin"));
#endif
//если FS обновилась успешно
if (retFS == HTTP_UPDATE_OK) {
SerialPrint("I", F("Update"), F("FS upgrade done!"));
ret = true;
}
return ret;
}
bool upgradeBuild() {
bool ret = false;
WiFiClient wifiClient;
Serial.println(F("Start upgrade BUILD, please wait..."));
if (getBinPath("") == "error") {
SerialPrint("E", F("Update"), F("Build Path error"));
return ret;
}
#ifdef esp8266_4mb
ESPhttpUpdate.rebootOnUpdate(false);
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, getBinPath("firmware.bin"));
#endif
#ifdef esp32_4mb
httpUpdate.rebootOnUpdate(false);
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, getBinPath("firmware.bin"));
#endif
//если BUILD обновился успешно
if (retBuild == HTTP_UPDATE_OK) {
SerialPrint("I", F("Update"), F("BUILD upgrade done!"));
ret = true;
}
return ret;
}
void restartEsp() {
Serial.println(F("Restart ESP...."));
delay(1000);
ESP.restart();
}
const String getBinPath(String file) {
String path = "error";
if (file != "") {
int targetVersion = 0;
String serverip;
if (jsonRead(errorsHeapJson, F("chver"), targetVersion)) {
if (targetVersion >= 400) {
if (jsonRead(settingsFlashJson, F("serverip"), serverip)) {
if (serverip != "") {
path = jsonReadStr(settingsFlashJson, F("serverip")) + "/iotm/" + String(FIRMWARE_NAME) + "/" + String(targetVersion) + "/" + file;
}
}
}
}
SerialPrint("i", F("Update"), "path: " + path);
}
return path;
}
void putUserDataToRam() {
update.configJson = readFile("config.json", 4096);
update.settingsFlashJson = readFile("settings.json", 4096);
}
void saveUserDataToFlash() {
writeFile("/config.json", update.configJson);
writeFile("/settings.json", update.settingsFlashJson);
}

View File

@@ -113,8 +113,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
//**отправка**//
if (headerStr == "/system|") {
standWebSocket.sendTXT(num, errorsHeapJson);
standWebSocket.sendTXT(num, settingsFlashJson);
}
//**сохранение**//
//переписать любое поле в errors json
if (headerStr == "/rorre|") {
writeUint8tValueToJsonString(payload, length, headerLenth, errorsHeapJson);
}
@@ -123,7 +125,10 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
if (headerStr == "/reboot|") {
ESP.restart();
}
//**команда обновления esp**//
if (headerStr == "/update|") {
upgrade_firmware(3);
}
} break;
case WStype_BIN: {