OTA (experimental)
This commit is contained in:
parent
414dd02f1d
commit
0667ee5744
32
src/OTA.cpp
32
src/OTA.cpp
@ -49,24 +49,25 @@ void start_ota_update() {
|
|||||||
ESP_LOGI(TAG, "connected to %s", WIFI_SSID);
|
ESP_LOGI(TAG, "connected to %s", WIFI_SSID);
|
||||||
|
|
||||||
checkFirmwareUpdates(); // gets and flashes new firmware and restarts
|
checkFirmwareUpdates(); // gets and flashes new firmware and restarts
|
||||||
ESP.restart(); // reached only if update was not successful
|
ESP.restart(); // reached only if update was not successful
|
||||||
|
|
||||||
} // start_ota_update
|
} // start_ota_update
|
||||||
|
|
||||||
void checkFirmwareUpdates() {
|
void checkFirmwareUpdates() {
|
||||||
// Fetch the latest firmware version
|
// Fetch the latest firmware version
|
||||||
ESP_LOGI(TAG, "Checking latest firmware version...");
|
ESP_LOGI(TAG, "OTA mode, checking latest firmware version on server...");
|
||||||
const String latest = bintray.getLatestVersion();
|
const String latest = bintray.getLatestVersion();
|
||||||
if (latest.length() == 0) {
|
if (latest.length() == 0) {
|
||||||
ESP_LOGI(TAG, "Could not load info about the latest firmware, so nothing "
|
ESP_LOGI(
|
||||||
"to update. Continue ...");
|
TAG,
|
||||||
|
"Could not load info about the latest firmware. Rebooting to runmode.");
|
||||||
return;
|
return;
|
||||||
} else if (atoi(latest.c_str()) <= VERSION) {
|
} else if (atoi(latest.c_str()) <= VERSION) {
|
||||||
ESP_LOGI(TAG, "The current firmware is up to date. Continue ...");
|
ESP_LOGI(TAG, "Current firmware is up to date. Rebooting to runmode.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "There is a new version of firmware available: v.%s",
|
ESP_LOGI(TAG, "New firmware version v%s available. Downloading...",
|
||||||
latest.c_str());
|
latest.c_str());
|
||||||
processOTAUpdate(latest);
|
processOTAUpdate(latest);
|
||||||
}
|
}
|
||||||
@ -82,7 +83,7 @@ inline String getHeaderValue(String header, String headerName) {
|
|||||||
void processOTAUpdate(const String &version) {
|
void processOTAUpdate(const String &version) {
|
||||||
String firmwarePath = bintray.getBinaryPath(version);
|
String firmwarePath = bintray.getBinaryPath(version);
|
||||||
if (!firmwarePath.endsWith(".bin")) {
|
if (!firmwarePath.endsWith(".bin")) {
|
||||||
ESP_LOGI(TAG, "Unsupported binary format. OTA update cannot be performed!");
|
ESP_LOGI(TAG, "Unsupported binary format, OTA update cancelled.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,8 +104,7 @@ void processOTAUpdate(const String &version) {
|
|||||||
client.stop();
|
client.stop();
|
||||||
client.setCACert(bintray.getCertificate(currentHost));
|
client.setCACert(bintray.getCertificate(currentHost));
|
||||||
if (!client.connect(currentHost.c_str(), port)) {
|
if (!client.connect(currentHost.c_str(), port)) {
|
||||||
ESP_LOGI(TAG,
|
ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s",
|
||||||
"Redirect detected! Cannot connect to %s for some reason!",
|
|
||||||
currentHost.c_str());
|
currentHost.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -120,7 +120,7 @@ void processOTAUpdate(const String &version) {
|
|||||||
unsigned long timeout = millis();
|
unsigned long timeout = millis();
|
||||||
while (client.available() == 0) {
|
while (client.available() == 0) {
|
||||||
if (millis() - timeout > RESPONSE_TIMEOUT_MS) {
|
if (millis() - timeout > RESPONSE_TIMEOUT_MS) {
|
||||||
ESP_LOGI(TAG, "Client Timeout !");
|
ESP_LOGI(TAG, "Client Timeout.");
|
||||||
client.stop();
|
client.stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -146,7 +146,7 @@ void processOTAUpdate(const String &version) {
|
|||||||
"new address");
|
"new address");
|
||||||
redirect = true;
|
redirect = true;
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Could not get a valid firmware url");
|
ESP_LOGI(TAG, "Could not get a valid firmware url.");
|
||||||
// Unexptected HTTP response. Retry or skip update?
|
// Unexptected HTTP response. Retry or skip update?
|
||||||
redirect = false;
|
redirect = false;
|
||||||
}
|
}
|
||||||
@ -184,20 +184,20 @@ void processOTAUpdate(const String &version) {
|
|||||||
// check whether we have everything for OTA update
|
// check whether we have everything for OTA update
|
||||||
if (contentLength && isValidContentType) {
|
if (contentLength && isValidContentType) {
|
||||||
if (Update.begin(contentLength)) {
|
if (Update.begin(contentLength)) {
|
||||||
ESP_LOGI(TAG, "Starting Over-The-Air update. This may take some time to "
|
ESP_LOGI(TAG, "Starting OTA update. This will take some time to "
|
||||||
"complete ...");
|
"complete...");
|
||||||
size_t written = Update.writeStream(client);
|
size_t written = Update.writeStream(client);
|
||||||
|
|
||||||
if (written == contentLength) {
|
if (written == contentLength) {
|
||||||
ESP_LOGI(TAG, "Written %d successfully", written);
|
ESP_LOGI(TAG, "Written %d bytes successfully", written);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Written only %d / %d Retry?", written, contentLength);
|
ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", written, contentLength);
|
||||||
// Retry??
|
// Retry??
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Update.end()) {
|
if (Update.end()) {
|
||||||
if (Update.isFinished()) {
|
if (Update.isFinished()) {
|
||||||
ESP_LOGI(TAG, "OTA update has successfully completed. Rebooting ...");
|
ESP_LOGI(TAG, "OTA update completed. Rebooting to runmode.");
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished "
|
ESP_LOGI(TAG, "Something went wrong! OTA update hasn't been finished "
|
||||||
|
@ -24,7 +24,6 @@ licenses. Refer to LICENSE.txt file in repository for more details.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Basic Config
|
// Basic Config
|
||||||
#include "globals.h"
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
configData_t cfg; // struct holds current device configuration
|
configData_t cfg; // struct holds current device configuration
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
#ifndef _MAIN_H
|
#ifndef _MAIN_H
|
||||||
#define _MAIN_H
|
#define _MAIN_H
|
||||||
|
|
||||||
//#include "led.h"
|
#include "globals.h"
|
||||||
|
#include "led.h"
|
||||||
#include "macsniff.h"
|
#include "macsniff.h"
|
||||||
#include "wifiscan.h"
|
#include "wifiscan.h"
|
||||||
#include "configmanager.h"
|
#include "configmanager.h"
|
||||||
|
Loading…
Reference in New Issue
Block a user