ota.cpp: display dialog while updating

This commit is contained in:
Klaus K Wilting 2018-09-22 19:39:31 +02:00
parent 2f3767759b
commit 7a53b724cb
4 changed files with 62 additions and 11 deletions

View File

@ -4,6 +4,7 @@
#include <U8x8lib.h> #include <U8x8lib.h>
extern uint8_t DisplayState; extern uint8_t DisplayState;
extern HAS_DISPLAY u8x8;
void init_display(const char *Productname, const char *Version); void init_display(const char *Productname, const char *Version);
void refreshtheDisplay(void); void refreshtheDisplay(void);

View File

@ -48,8 +48,6 @@ extern hw_timer_t *channelSwitch, *sendCycle;
extern portMUX_TYPE timerMux; extern portMUX_TYPE timerMux;
extern volatile int SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ, extern volatile int SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ,
ChannelTimerIRQ, ButtonPressedIRQ; ChannelTimerIRQ, ButtonPressedIRQ;
// extern QueueHandle_t LoraSendQueue, SPISendQueue;
extern TaskHandle_t WifiLoopTask;
extern std::array<uint64_t, 0xff>::iterator it; extern std::array<uint64_t, 0xff>::iterator it;
extern std::array<uint64_t, 0xff> beacons; extern std::array<uint64_t, 0xff> beacons;

View File

@ -39,7 +39,6 @@ hw_timer_t *channelSwitch, *displaytimer, *sendCycle, *homeCycle;
volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, SendCycleTimerIRQ = 0, volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, SendCycleTimerIRQ = 0,
DisplayTimerIRQ = 0, HomeCycleIRQ = 0; DisplayTimerIRQ = 0, HomeCycleIRQ = 0;
TaskHandle_t WifiLoopTask = NULL;
TaskHandle_t StateTask = NULL; TaskHandle_t StateTask = NULL;
// RTos send queues for payload transmit // RTos send queues for payload transmit
@ -122,13 +121,6 @@ void setup() {
// read settings from NVRAM // read settings from NVRAM
loadConfig(); // includes initialize if necessary loadConfig(); // includes initialize if necessary
// reboot to firmware update mode if ota trigger switch is set
if (cfg.runmode == 1) {
cfg.runmode = 0;
saveConfig();
start_ota_update();
}
#ifdef VENDORFILTER #ifdef VENDORFILTER
strcat_P(features, " OUIFLT"); strcat_P(features, " OUIFLT");
#endif #endif
@ -240,6 +232,13 @@ void setup() {
timerAlarmEnable(displaytimer); timerAlarmEnable(displaytimer);
#endif #endif
// reboot to firmware update mode if ota trigger switch is set
if (cfg.runmode == 1) {
cfg.runmode = 0;
saveConfig();
start_ota_update();
}
// setup channel rotation trigger IRQ using esp32 hardware timer 1 // setup channel rotation trigger IRQ using esp32 hardware timer 1
channelSwitch = timerBegin(1, 800, true); channelSwitch = timerBegin(1, 800, true);
timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true);

View File

@ -17,6 +17,9 @@
#include "ota.h" #include "ota.h"
#include <string>
using namespace std;
const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE);
// Connection port (HTTPS) // Connection port (HTTPS)
@ -32,9 +35,33 @@ volatile bool isValidContentType = false;
// Local logging tag // Local logging tag
static const char TAG[] = "main"; static const char TAG[] = "main";
void display(const uint8_t x, const uint8_t y, char* text) {
#ifdef HAS_DISPLAY
u8x8.setCursor(x, y);
u8x8.print(text);
#endif
}
void start_ota_update() { void start_ota_update() {
#ifdef HAS_DISPLAY
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.clear();
#ifdef DISPLAY_FLIP
u8x8.setFlipMode(1);
#endif
u8x8.draw2x2String(0, 0, "UPDATING");
u8x8.setCursor(0, 3);
u8x8.print("Wifi connect ..\n");
u8x8.print("Get Update? ..\n");
u8x8.print("Downloading ..\n");
u8x8.print("Flashing ..\n");
u8x8.print("Rebooting ..");
#endif
ESP_LOGI(TAG, "Starting Wifi OTA update"); ESP_LOGI(TAG, "Starting Wifi OTA update");
display(14, 3, "**");
WiFi.begin(WIFI_SSID, WIFI_PASS); WiFi.begin(WIFI_SSID, WIFI_PASS);
@ -47,10 +74,15 @@ void start_ota_update() {
} }
if (i >= 0) { if (i >= 0) {
ESP_LOGI(TAG, "connected to %s", WIFI_SSID); ESP_LOGI(TAG, "connected to %s", WIFI_SSID);
display(14, 3, "OK");
checkFirmwareUpdates(); // gets and flashes new firmware and restarts checkFirmwareUpdates(); // gets and flashes new firmware and restarts
} else } else {
ESP_LOGI(TAG, "could not connect to %s, rebooting.", WIFI_SSID); ESP_LOGI(TAG, "could not connect to %s, rebooting.", WIFI_SSID);
display(14, 3, " E");
}
display(14, 7, "**");
delay(5000);
ESP.restart(); // reached only if update was not successful or no wifi connect ESP.restart(); // reached only if update was not successful or no wifi connect
} // start_ota_update } // start_ota_update
@ -58,19 +90,24 @@ void start_ota_update() {
void checkFirmwareUpdates() { void checkFirmwareUpdates() {
// Fetch the latest firmware version // Fetch the latest firmware version
ESP_LOGI(TAG, "OTA mode, checking latest firmware version on server..."); ESP_LOGI(TAG, "OTA mode, checking latest firmware version on server...");
display(14, 4, "**");
const String latest = bintray.getLatestVersion(); const String latest = bintray.getLatestVersion();
if (latest.length() == 0) { if (latest.length() == 0) {
ESP_LOGI( ESP_LOGI(
TAG, TAG,
"Could not load info about the latest firmware. Rebooting to runmode."); "Could not load info about the latest firmware. Rebooting to runmode.");
display(14, 4, " E");
return; return;
} else if (version_compare(latest, cfg.version) <= 0) { } else if (version_compare(latest, cfg.version) <= 0) {
ESP_LOGI(TAG, "Current firmware is up to date. Rebooting to runmode."); ESP_LOGI(TAG, "Current firmware is up to date. Rebooting to runmode.");
display(14, 4, "NO");
return; return;
} }
ESP_LOGI(TAG, "New firmware version v%s available. Downloading...", ESP_LOGI(TAG, "New firmware version v%s available. Downloading...",
latest.c_str()); latest.c_str());
display(14, 4, "OK");
processOTAUpdate(latest); processOTAUpdate(latest);
} }
@ -83,9 +120,11 @@ inline String getHeaderValue(String header, String headerName) {
* OTA update processing * OTA update processing
*/ */
void processOTAUpdate(const String &version) { void processOTAUpdate(const String &version) {
display(14, 5, "**");
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 cancelled."); ESP_LOGI(TAG, "Unsupported binary format, OTA update cancelled.");
display(14, 5, " E");
return; return;
} }
@ -97,6 +136,7 @@ void processOTAUpdate(const String &version) {
if (!client.connect(currentHost.c_str(), port)) { if (!client.connect(currentHost.c_str(), port)) {
ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str()); ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str());
display(14, 5, " E");
return; return;
} }
@ -108,6 +148,7 @@ void processOTAUpdate(const String &version) {
if (!client.connect(currentHost.c_str(), port)) { if (!client.connect(currentHost.c_str(), port)) {
ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s", ESP_LOGI(TAG, "Redirect detected, but cannot connect to %s",
currentHost.c_str()); currentHost.c_str());
display(14, 5, " E");
return; return;
} }
} }
@ -123,6 +164,7 @@ void processOTAUpdate(const String &version) {
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.");
display(14, 5, " E");
client.stop(); client.stop();
return; return;
} }
@ -182,6 +224,8 @@ void processOTAUpdate(const String &version) {
} }
} }
display(14, 5, "OK");
// check whether we have everything for OTA update // check whether we have everything for OTA update
if (contentLength && isValidContentType) { if (contentLength && isValidContentType) {
@ -196,16 +240,19 @@ void processOTAUpdate(const String &version) {
"Starting OTA update, attempt %d of %d. This will take some " "Starting OTA update, attempt %d of %d. This will take some "
"time to complete...", "time to complete...",
FLASH_MAX_TRY - i, FLASH_MAX_TRY); FLASH_MAX_TRY - i, FLASH_MAX_TRY);
display(14, 6, "**");
written = Update.writeStream(client); written = Update.writeStream(client);
if (written == contentLength) { if (written == contentLength) {
ESP_LOGI(TAG, "Written %d bytes successfully", written); ESP_LOGI(TAG, "Written %d bytes successfully", written);
display(14, 6, "**");
break; break;
} else { } else {
ESP_LOGI(TAG, ESP_LOGI(TAG,
"Written only %d of %d bytes, OTA update attempt cancelled.", "Written only %d of %d bytes, OTA update attempt cancelled.",
written, contentLength); written, contentLength);
display(14, 6, " E");
} }
} }
@ -215,27 +262,33 @@ void processOTAUpdate(const String &version) {
ESP_LOGI( ESP_LOGI(
TAG, TAG,
"OTA update completed. Rebooting to runmode with new version."); "OTA update completed. Rebooting to runmode with new version.");
display(14, 7, "OK");
client.stop(); client.stop();
return; return;
} 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 "
"properly."); "properly.");
display(14, 7, " E");
} }
} else { } else {
ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError()); ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError());
display(14, 7, " E");
} }
} else { } else {
ESP_LOGI(TAG, "There isn't enough space to start OTA update"); ESP_LOGI(TAG, "There isn't enough space to start OTA update");
display(14, 7, " E");
client.flush(); client.flush();
} }
} else { } else {
ESP_LOGI(TAG, ESP_LOGI(TAG,
"There was no valid content in the response from the OTA server!"); "There was no valid content in the response from the OTA server!");
display(14, 7, " E");
client.flush(); client.flush();
} }
ESP_LOGI(TAG, ESP_LOGI(TAG,
"OTA update failed. Rebooting to runmode with current version."); "OTA update failed. Rebooting to runmode with current version.");
display(14, 7, " E");
client.stop(); client.stop();
} }