From fb7c32e5c88f7d09f1179e72670fba4f3f956fc2 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 16 Sep 2018 12:18:11 +0200 Subject: [PATCH] OTA (experimental) --- lib/BintrayClient/src/BintrayClient.cpp | 8 ++++---- platformio.ini | 8 +++++--- src/OTA.cpp | 25 ++++++++++++++----------- src/OTA.h | 5 +++-- src/cyclic.cpp | 2 +- src/main.h | 2 +- src/paxcounter.conf | 3 +++ 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/BintrayClient/src/BintrayClient.cpp b/lib/BintrayClient/src/BintrayClient.cpp index 4479f539..b0e7543f 100644 --- a/lib/BintrayClient/src/BintrayClient.cpp +++ b/lib/BintrayClient/src/BintrayClient.cpp @@ -109,7 +109,7 @@ String BintrayClient::getLatestVersion() const const size_t bufferSize = 1024; if (jsonResult.length() > bufferSize) { - ESP_LOGI(TAG, "Error: Could not parse JSON. Input data is too big!"); + Serial.println("Error: Could parse JSON. Input data is too big!"); return version; } StaticJsonBuffer jsonBuffer; @@ -118,7 +118,7 @@ String BintrayClient::getLatestVersion() const // Check for errors in parsing if (!root.success()) { - ESP_LOGI(TAG, "Error: Could not parse JSON!"); + Serial.println("Error: Could not parse JSON!"); return version; } return root.get("name"); @@ -133,7 +133,7 @@ String BintrayClient::getBinaryPath(const String &version) const const size_t bufferSize = 1024; if (jsonResult.length() > bufferSize) { - ESP_LOGI(TAG, "Error: Could parse JSON. Input data is too big!"); + Serial.println("Error: Could parse JSON. Input data is too big!"); return path; } StaticJsonBuffer jsonBuffer; @@ -142,7 +142,7 @@ String BintrayClient::getBinaryPath(const String &version) const JsonObject &firstItem = root[0]; if (!root.success()) { //Check for errors in parsing - ESP_LOGI(TAG, "Error: Could not parse JSON!"); + Serial.println("Error: Could not parse JSON!"); return path; } return "/" + getUser() + "/" + getRepository() + "/" + firstItem.get("path"); diff --git a/platformio.ini b/platformio.ini index 90c8fdb7..0bd773fe 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,7 +29,7 @@ package = ttgov21_old api_token = 2e10f923df5d47b9c7e25752510322a1d65ee997 [common] -release_version = 6 +release_version = 7 [ota] ; build configuration based on Bintray and Wi-Fi settings @@ -42,14 +42,16 @@ build_flags = '-DBINTRAY_REPO="${bintray.repository}"' '-DBINTRAY_PACKAGE="${bintray.package}"' -DVERSION=${common.release_version} +lib_deps_ota = + https://github.com/platformio/bintray-secure-ota.git [common_env_data] platform_espressif32 = espressif32@1.3.0 ;platform_espressif32 = https://github.com/platformio/platform-espressif32.git#feature/stage -;board_build.partitions = no_ota.csv board_build.partitions = min_spiffs.csv lib_deps_all = - ArduinoJson + ArduinoJson@^5.13.1 +; ArduinoJson${ota.lib_deps_ota} lib_deps_display = U8g2@>=2.23.16 lib_deps_rgbled = diff --git a/src/OTA.cpp b/src/OTA.cpp index 7904eea3..28a58f96 100644 --- a/src/OTA.cpp +++ b/src/OTA.cpp @@ -15,10 +15,7 @@ limitations under the License. */ -#include -#include -#include -#include "ota.h" +#include "OTA.h" const BintrayClient bintray(BINTRAY_USER, BINTRAY_REPO, BINTRAY_PACKAGE); @@ -41,15 +38,20 @@ void start_ota_update() { WiFi.begin(WIFI_SSID, WIFI_PASS); - while (WiFi.status() != WL_CONNECTED) { - delay(2000); + int i = WIFI_MAX_TRY; + while (i--) { ESP_LOGI(TAG, "trying to connect to %s", WIFI_SSID); + if (WiFi.status() == WL_CONNECTED) + break; + delay(5000); } + if (i >= 0) { + ESP_LOGI(TAG, "connected to %s", WIFI_SSID); + checkFirmwareUpdates(); // gets and flashes new firmware and restarts + } else + ESP_LOGI(TAG, "could not connect to %s, rebooting.", WIFI_SSID); - ESP_LOGI(TAG, "connected to %s", WIFI_SSID); - - 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 or no wifi connect } // start_ota_update @@ -191,7 +193,8 @@ void processOTAUpdate(const String &version) { if (written == contentLength) { ESP_LOGI(TAG, "Written %d bytes successfully", written); } else { - ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", written, contentLength); + ESP_LOGI(TAG, "Written only %d of %d bytes, OTA update cancelled.", + written, contentLength); // Retry?? } diff --git a/src/OTA.h b/src/OTA.h index 52698c39..fc66973c 100644 --- a/src/OTA.h +++ b/src/OTA.h @@ -1,9 +1,10 @@ #ifndef OTA_H #define OTA_H -#include #include -#include "globals.h" +#include +#include +#include void checkFirmwareUpdates(); void processOTAUpdate(const String &version); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index ba7968bf..72b2284c 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -4,7 +4,7 @@ // Basic config #include "globals.h" #include "senddata.h" -#include "ota.h" +#include "OTA.h" // Local logging tag static const char TAG[] = "main"; diff --git a/src/main.h b/src/main.h index cb0cb8e2..4b77439a 100644 --- a/src/main.h +++ b/src/main.h @@ -9,7 +9,7 @@ #include "senddata.h" #include "cyclic.h" #include "beacon_array.h" -#include "ota.h" +#include "OTA.h" #include // needed for reading ESP32 chip attributes #include // needed for Wifi event handler diff --git a/src/paxcounter.conf b/src/paxcounter.conf index bcecc56d..68681b9b 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -63,6 +63,9 @@ #define DISPLAYREFRESH_MS 40 // OLED refresh cycle in ms [default = 40] -> 1000/40 = 25 frames per second #define HOMECYCLE 30 // house keeping cycle in seconds [default = 30 secs] +// OTA settings +#define WIFI_MAX_TRY 20 // maximum number of wifi connect attempts for OTA update [default = 20] + // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored // define hardware specifics settings in platformio.ini as build_flag for hardware environment