Fix crashbug in ota
This commit is contained in:
31
src/wifi.cpp
31
src/wifi.cpp
@ -22,11 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
#include <ESP8266HTTPClient.h>
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266httpUpdate.h>
|
#include <ESP8266httpUpdate.h>
|
||||||
#else // defined (ESP32)
|
#else // defined (ESP32)
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
@ -276,37 +274,22 @@ bool WifiConnection::updateFirmware() {
|
|||||||
//
|
//
|
||||||
// Download and save file
|
// Download and save file
|
||||||
//
|
//
|
||||||
void WifiConnection::downloadFile(const char *fname) {
|
void WifiConnection::downloadFile(HTTPClient& http, String& fname) {
|
||||||
#if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING)
|
#if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING)
|
||||||
Log.verbose(F("WIFI: Download file %s." CR), fname);
|
Log.verbose(F("WIFI: Download file %s." CR), fname);
|
||||||
#endif
|
#endif
|
||||||
WiFiClient wifi;
|
|
||||||
WiFiClientSecure wifiSecure;
|
|
||||||
HTTPClient http;
|
|
||||||
String serverPath = myConfig.getOtaURL();
|
|
||||||
serverPath += fname;
|
|
||||||
|
|
||||||
if (myConfig.isOtaSSL()) {
|
|
||||||
wifiSecure.setInsecure();
|
|
||||||
Log.notice(F("WIFI: OTA, SSL enabled without validation." CR));
|
|
||||||
http.begin(wifiSecure, serverPath);
|
|
||||||
} else {
|
|
||||||
http.begin(wifi, serverPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
int httpResponseCode = http.GET();
|
int httpResponseCode = http.GET();
|
||||||
|
|
||||||
if (httpResponseCode == 200) {
|
if (httpResponseCode == 200) {
|
||||||
File f = LittleFS.open(fname, "w");
|
File f = LittleFS.open(fname, "w");
|
||||||
http.writeToStream(&f);
|
http.writeToStream(&f);
|
||||||
f.close();
|
f.close();
|
||||||
Log.notice(F("WIFI: Downloaded file %s." CR), fname);
|
Log.notice(F("WIFI: Downloaded file %s." CR), fname.c_str());
|
||||||
} else {
|
} else {
|
||||||
ErrorFileLog errLog;
|
ErrorFileLog errLog;
|
||||||
errLog.addEntry("WIFI: Failed to download html-file " +
|
errLog.addEntry("WIFI: Failed to download html-file " +
|
||||||
String(httpResponseCode));
|
String(httpResponseCode));
|
||||||
}
|
}
|
||||||
http.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -332,6 +315,7 @@ bool WifiConnection::checkFirmwareVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send HTTP GET request
|
// Send HTTP GET request
|
||||||
|
DynamicJsonDocument ver(300);
|
||||||
int httpResponseCode = http.GET();
|
int httpResponseCode = http.GET();
|
||||||
|
|
||||||
if (httpResponseCode == 200) {
|
if (httpResponseCode == 200) {
|
||||||
@ -341,7 +325,6 @@ bool WifiConnection::checkFirmwareVersion() {
|
|||||||
#if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING)
|
#if LOG_LEVEL == 6 && !defined(WIFI_DISABLE_LOGGING)
|
||||||
Log.verbose(F("WIFI: Payload %s." CR), payload.c_str());
|
Log.verbose(F("WIFI: Payload %s." CR), payload.c_str());
|
||||||
#endif
|
#endif
|
||||||
DynamicJsonDocument ver(300);
|
|
||||||
DeserializationError err = deserializeJson(ver, payload);
|
DeserializationError err = deserializeJson(ver, payload);
|
||||||
if (err) {
|
if (err) {
|
||||||
ErrorFileLog errLog;
|
ErrorFileLog errLog;
|
||||||
@ -364,10 +347,10 @@ bool WifiConnection::checkFirmwareVersion() {
|
|||||||
// Compare major version
|
// Compare major version
|
||||||
if (newVer[0] > curVer[0]) _newFirmware = true;
|
if (newVer[0] > curVer[0]) _newFirmware = true;
|
||||||
// Compare minor version
|
// Compare minor version
|
||||||
if (newVer[0] == curVer[0] && newVer[1] > curVer[1])
|
else if (newVer[0] == curVer[0] && newVer[1] > curVer[1])
|
||||||
_newFirmware = true;
|
_newFirmware = true;
|
||||||
// Compare patch version
|
// Compare patch version
|
||||||
if (newVer[0] == curVer[0] && newVer[1] == curVer[1] &&
|
else if (newVer[0] == curVer[0] && newVer[1] == curVer[1] &&
|
||||||
newVer[2] > curVer[2])
|
newVer[2] > curVer[2])
|
||||||
_newFirmware = true;
|
_newFirmware = true;
|
||||||
}
|
}
|
||||||
@ -375,14 +358,14 @@ bool WifiConnection::checkFirmwareVersion() {
|
|||||||
|
|
||||||
// Download new html files to filesystem if they are present.
|
// Download new html files to filesystem if they are present.
|
||||||
if (!ver["html"].isNull() && _newFirmware) {
|
if (!ver["html"].isNull() && _newFirmware) {
|
||||||
Log.notice(F("WIFI: OTA downloading new html files." CR));
|
Log.notice(F("WIFI: OTA checking if html files should be downloaded." CR));
|
||||||
JsonArray htmlFiles = ver["html"].as<JsonArray>();
|
JsonArray htmlFiles = ver["html"].as<JsonArray>();
|
||||||
for (JsonVariant v : htmlFiles) {
|
for (JsonVariant v : htmlFiles) {
|
||||||
String s = v;
|
String s = v;
|
||||||
#if LOG_LEVEL == 6
|
#if LOG_LEVEL == 6
|
||||||
Log.verbose(F("WIFI: OTA listed html file %s" CR), s.c_str());
|
Log.verbose(F("WIFI: OTA listed html file %s" CR), s.c_str());
|
||||||
#endif
|
#endif
|
||||||
downloadFile(s.c_str());
|
downloadFile(http, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,12 @@ SOFTWARE.
|
|||||||
#ifndef SRC_WIFI_HPP_
|
#ifndef SRC_WIFI_HPP_
|
||||||
#define SRC_WIFI_HPP_
|
#define SRC_WIFI_HPP_
|
||||||
|
|
||||||
|
#if defined(ESP8266)
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#else // defined (ESP32)
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define WIFI_DEFAULT_SSID "GravityMon" // Name of created SSID
|
#define WIFI_DEFAULT_SSID "GravityMon" // Name of created SSID
|
||||||
#define WIFI_DEFAULT_PWD "password" // Password for created SSID
|
#define WIFI_DEFAULT_PWD "password" // Password for created SSID
|
||||||
#define WIFI_MDNS "gravitymon" // Prefix for MDNS name
|
#define WIFI_MDNS "gravitymon" // Prefix for MDNS name
|
||||||
@ -35,7 +41,7 @@ class WifiConnection {
|
|||||||
// OTA
|
// OTA
|
||||||
bool _newFirmware = false;
|
bool _newFirmware = false;
|
||||||
bool parseFirmwareVersionString(int (&num)[3], const char* version);
|
bool parseFirmwareVersionString(int (&num)[3], const char* version);
|
||||||
void downloadFile(const char* fname);
|
void downloadFile(HTTPClient& http, String& fname);
|
||||||
void connectAsync();
|
void connectAsync();
|
||||||
bool waitForConnection(int maxTime = 20);
|
bool waitForConnection(int maxTime = 20);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user