ota-test first push

This commit is contained in:
Klaus K Wilting 2018-09-15 14:47:13 +02:00
parent 7a58c8dece
commit 38dc2667ab
3 changed files with 97 additions and 17 deletions

View File

@ -9,6 +9,7 @@
; http://docs.platformio.org/page/projectconf.html ; http://docs.platformio.org/page/projectconf.html
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
;env_default = generic ;env_default = generic
@ -29,9 +30,9 @@ description = Paxcounter is a proof-of-concept ESP32 device for metering passeng
[bintray] [bintray]
user = cyberman54 user = cyberman54
repository = paxcounter repository = paxcounter-firmware
package = esp32-paxcounter package = ttgov21_old
api_token = 9f02e2a2374c278fd79d5bcf4b4442fca9752012 api_token = ***
[wifi] [wifi]
ssid = *** ssid = ***
@ -42,7 +43,8 @@ platform = https://github.com/platformio/platform-espressif32.git
; firmware version, please modify it between releases ; firmware version, please modify it between releases
; positive integer value ; positive integer value
release_version = 1 ;release_version = 1.4.30
release_version = 4
; build configuration based on Bintray and Wi-Fi settings ; build configuration based on Bintray and Wi-Fi settings
build_flags = build_flags =
@ -51,7 +53,7 @@ build_flags =
'-DBINTRAY_USER="${bintray.user}"' '-DBINTRAY_USER="${bintray.user}"'
'-DBINTRAY_REPO="${bintray.repository}"' '-DBINTRAY_REPO="${bintray.repository}"'
'-DBINTRAY_PACKAGE="${bintray.package}"' '-DBINTRAY_PACKAGE="${bintray.package}"'
'-DVERSION=0' -DVERSION=${common.release_version}
; ;
; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <---
; otherwise device may leak RAM ; otherwise device may leak RAM
@ -70,7 +72,7 @@ build_flags =
; -DCORE_DEBUG_LEVEL=5 ; -DCORE_DEBUG_LEVEL=5
[common_env_data] [common_env_data]
platform_espressif32 = espressif32@1.2.0 platform_espressif32 = espressif32@1.3.0
;platform_espressif32 = https://github.com/platformio/platform-espressif32.git#feature/stage ;platform_espressif32 = https://github.com/platformio/platform-espressif32.git#feature/stage
;board_build.partitions = no_ota.csv ;board_build.partitions = no_ota.csv
board_build.partitions = min_spiffs.csv board_build.partitions = min_spiffs.csv
@ -142,6 +144,7 @@ build_flags =
${common_env_data.build_flags} ${common_env_data.build_flags}
[env:ttgov21] [env:ttgov21]
bintraypackage = ttgov21_old
platform = ${common_env_data.platform_espressif32} platform = ${common_env_data.platform_espressif32}
framework = arduino framework = arduino
board = esp32dev board = esp32dev
@ -154,6 +157,8 @@ lib_deps =
build_flags = build_flags =
${common.build_flags} ${common.build_flags}
${common_env_data.build_flags} ${common_env_data.build_flags}
upload_protocol = custom
extra_scripts = pre:publish_firmware.py
[env:ttgobeam] [env:ttgobeam]
platform = ${common_env_data.platform_espressif32} platform = ${common_env_data.platform_espressif32}

71
publish_firmware.py Normal file
View File

@ -0,0 +1,71 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests
from os.path import basename
from platformio import util
Import('env')
project_config = util.load_project_config()
bintray_config = {k: v for k, v in project_config.items("bintray")}
version = project_config.get("common", "release_version")
#
# Push new firmware to the Bintray storage using API
#
def publish_firmware(source, target, env):
firmware_path = str(source[0])
firmware_name = basename(firmware_path)
print("Uploading {0} to Bintray. Version: {1}".format(
firmware_name, version))
print(firmware_path, firmware_name)
url = "/".join([
"https://api.bintray.com", "content",
bintray_config.get("user"),
bintray_config.get("repository"),
bintray_config.get("package"), version, firmware_name
])
print(url)
headers = {
"Content-type": "application/octet-stream",
"X-Bintray-Publish": "1",
"X-Bintray-Override": "1"
}
r = requests.put(
url,
data=open(firmware_path, "rb"),
headers=headers,
auth=(bintray_config.get("user"), bintray_config['api_token']))
if r.status_code != 201:
print("Failed to submit package: {0}\n{1}".format(
r.status_code, r.text))
else:
print("The firmware has been successfuly published at Bintray.com!")
# Custom upload command and program name
env.Replace(
PROGNAME="firmware_v_%s" % version,
UPLOADCMD=publish_firmware
)

View File

@ -31,9 +31,13 @@ const uint32_t RESPONSE_TIMEOUT_MS = 5000;
volatile int contentLength = 0; volatile int contentLength = 0;
volatile bool isValidContentType = false; volatile bool isValidContentType = false;
// Local logging tag
static const char TAG[] = "main";
void checkFirmwareUpdates() void checkFirmwareUpdates()
{ {
// Fetch the latest firmware version // Fetch the latest firmware version
ESP_LOGI(TAG, "Checking latest firmware version...");
const String latest = bintray.getLatestVersion(); const String latest = bintray.getLatestVersion();
if (latest.length() == 0) if (latest.length() == 0)
{ {
@ -42,11 +46,11 @@ void checkFirmwareUpdates()
} }
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, "The current firmware is up to date. Continue ...");
return; return;
} }
ESP_LOGI(TAG, "There is a new version of firmware available: v.%s", latest); ESP_LOGI(TAG, "There is a new version of firmware available: v.%s", latest.c_str());
processOTAUpdate(latest); processOTAUpdate(latest);
} }
@ -76,7 +80,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); ESP_LOGI(TAG, "Cannot connect to %s", currentHost.c_str());
return; return;
} }
@ -89,7 +93,7 @@ void processOTAUpdate(const String &version)
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, "Redirect detected! Cannot connect to %s for some reason!", currentHost); ESP_LOGI(TAG, "Redirect detected! Cannot connect to %s for some reason!", currentHost.c_str());
return; return;
} }
} }
@ -148,12 +152,12 @@ void processOTAUpdate(const String &version)
if (line.startsWith("Location: ")) if (line.startsWith("Location: "))
{ {
String newUrl = getHeaderValue(line, "Location: "); String newUrl = getHeaderValue(line, "Location: ");
ESP_LOGI(TAG, "Got new url: %s", newUrl); ESP_LOGI(TAG, "Got new url: %s", newUrl.c_str());
newUrl.remove(0, newUrl.indexOf("//") + 2); newUrl.remove(0, newUrl.indexOf("//") + 2);
currentHost = newUrl.substring(0, newUrl.indexOf('/')); currentHost = newUrl.substring(0, newUrl.indexOf('/'));
newUrl.remove(newUrl.indexOf(currentHost), currentHost.length()); newUrl.remove(newUrl.indexOf(currentHost), currentHost.length());
firmwarePath = newUrl; firmwarePath = newUrl;
ESP_LOGI(TAG, "firmwarePath: %s", firmwarePath); ESP_LOGI(TAG, "firmwarePath: %s", firmwarePath.c_str());
continue; continue;
} }
@ -161,13 +165,13 @@ void processOTAUpdate(const String &version)
if (line.startsWith("Content-Length: ")) if (line.startsWith("Content-Length: "))
{ {
contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str()); contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
ESP_LOGI(TAG, "Got %s bytes from server", String(contentLength)); ESP_LOGI(TAG, "Got %d bytes from server", contentLength);
} }
if (line.startsWith("Content-Type: ")) if (line.startsWith("Content-Type: "))
{ {
String contentType = getHeaderValue(line, "Content-Type: "); String contentType = getHeaderValue(line, "Content-Type: ");
ESP_LOGI(TAG, "Got %s payload", contentType); ESP_LOGI(TAG, "Got %s payload", contentType.c_str());
if (contentType == "application/octet-stream") if (contentType == "application/octet-stream")
{ {
isValidContentType = true; isValidContentType = true;
@ -186,11 +190,11 @@ void processOTAUpdate(const String &version)
if (written == contentLength) if (written == contentLength)
{ {
ESP_LOGI(TAG, "Written %s successfully", String(written)); ESP_LOGI(TAG, "Written %d successfully", written);
} }
else else
{ {
ESP_LOGI(TAG, "Written only %s / %s Retry?", String(written), String(contentLength)); ESP_LOGI(TAG, "Written only %d / %d Retry?", written, contentLength);
// Retry?? // Retry??
} }
@ -208,7 +212,7 @@ void processOTAUpdate(const String &version)
} }
else else
{ {
ESP_LOGI(TAG, "An error occurred. Error #: %s", String(Update.getError())); ESP_LOGI(TAG, "An error occurred. Error #: %d", Update.getError());
} }
} }
else else