OTA (experimental)

This commit is contained in:
cyberman54 2018-09-16 12:18:11 +02:00
parent 0667ee5744
commit fb7c32e5c8
7 changed files with 31 additions and 22 deletions

View File

@ -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<bufferSize> 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<String>("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<bufferSize> 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<String>("path");

View File

@ -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 =

View File

@ -15,10 +15,7 @@
limitations under the License.
*/
#include <WiFiClientSecure.h>
#include <Update.h>
#include <BintrayClient.h>
#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??
}

View File

@ -1,9 +1,10 @@
#ifndef OTA_H
#define OTA_H
#include <Arduino.h>
#include <WiFi.h>
#include "globals.h"
#include <WiFiClientSecure.h>
#include <Update.h>
#include <BintrayClient.h>
void checkFirmwareUpdates();
void processOTAUpdate(const String &version);

View File

@ -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";

View File

@ -9,7 +9,7 @@
#include "senddata.h"
#include "cyclic.h"
#include "beacon_array.h"
#include "ota.h"
#include "OTA.h"
#include <esp_spi_flash.h> // needed for reading ESP32 chip attributes
#include <esp_event_loop.h> // needed for Wifi event handler

View File

@ -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