From dc345dc46985989c7ea0a62b3d762bf31a1b0933 Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 25 Aug 2022 21:15:15 -0600 Subject: [PATCH 1/2] Fix OTA updates when connected to AWS. This resolves an out of memory issue when updating via Over-The-Air while connected to a secure MQTT broker/AWS. The process creates a file and writes the update info into it. The device is then rebooted and the file, if detected after connection to WiFi, is read from and the update performed using the data in the file. --- src/lfs/http_ota.lua | 2 +- src/lfs/server_receiver.lua | 15 ++++++++++++--- src/lfs/start.lua | 2 +- src/lfs/wifi.lua | 17 ++++++++++++----- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/lfs/http_ota.lua b/src/lfs/http_ota.lua index e5aad4c..96bef32 100644 --- a/src/lfs/http_ota.lua +++ b/src/lfs/http_ota.lua @@ -68,7 +68,7 @@ finalise = function(sck) wifi.setmode(wifi.NULLMODE, false) collectgarbage();collectgarbage() -- run as separate task to maximise RAM available - node.task.post(function() node.flashreload(image) end) + node.task.post(function() node.LFS.reload(image) end) else print"Invalid save of image file" end diff --git a/src/lfs/server_receiver.lua b/src/lfs/server_receiver.lua index bd7fcf3..a4ff0ed 100644 --- a/src/lfs/server_receiver.lua +++ b/src/lfs/server_receiver.lua @@ -39,15 +39,14 @@ local function httpReceiver(sck, payload) print("Heap: ", node.heap(), "HTTP: ", "Discovery") elseif request.path == "/settings" then + print("Heap: ", node.heap(), "HTTP: ", "Settings") if mqttC ~= nil and request.method ~= "GET" then mqttC:on("offline", function(client) - print("Heap: ", node.heap(), "HTTP: ", "Settings") response.text(sck, require("server_settings")(request)) end) mqttC:close() return else - print("Heap: ", node.heap(), "HTTP: ", "Settings") response.text(sck, require("server_settings")(request)) end @@ -65,7 +64,17 @@ local function httpReceiver(sck, payload) elseif request.path == "/ota" then print("Heap: ", node.heap(), "HTTP: ", "OTA Update") - response.text(sck, require("ota")(request)) + if mqttC ~= nil and request.method ~= "GET" then + local uri = request.body.uri + local host, path, filename = string.match(uri, "%w+://([^/]+)(/[%w%p]+/)(.*)") + local f = file.open("ota_update.lua", "w") + f.writeline("return function() return " .. "'"..host.."'," .. "'"..path.."'," .. "'"..filename.."'" .. " end") + f.close() + response.text(sck, '{ "status":"ok", "host":"'.. host ..'", "path":"'.. path ..'", "filename":"'.. filename ..'" }') + tmr.create():alarm(1000, tmr.ALARM_SINGLE, function() print("Restarting for update") node.restart() end) + else + response.text(sck, require("ota")(request)) + end end sck, request, response = nil diff --git a/src/lfs/start.lua b/src/lfs/start.lua index 38ed24b..d26dca1 100644 --- a/src/lfs/start.lua +++ b/src/lfs/start.lua @@ -1,6 +1,6 @@ for fn in pairs(file.list()) do local fm = string.match(fn,".*%.lua-$") - if (fm) and fm ~= "init.lua" then + if (fm) and fm ~= "init.lua" and fm ~= "ota_update.lua" then print("Heap: ", node.heap(), "Compiling: ", fn) node.compile(fm) file.remove(fm) diff --git a/src/lfs/wifi.lua b/src/lfs/wifi.lua index 3eedaad..0440980 100644 --- a/src/lfs/wifi.lua +++ b/src/lfs/wifi.lua @@ -29,11 +29,18 @@ if wifi.sta.getconfig() == "" then end local bootApp = function() - print("Heap: ", node.heap(), "Booting Konnected application") - require("server") - print("Heap: ", node.heap(), "Loaded: ", "server") - require("application") - print("Heap: ", node.heap(), "Loaded: ", "application") + if file.exists("ota_update.lua") then + print("Performing OTA update...") + local host, path, filename = require("ota_update")() + file.remove("ota_update.lua") + LFS.http_ota(host, path, filename) + else + print("Heap: ", node.heap(), "Booting Konnected application") + require("server") + print("Heap: ", node.heap(), "Loaded: ", "server") + require("application") + print("Heap: ", node.heap(), "Loaded: ", "application") + end end local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t) From 2568770848c7084c5e3c15d53ee7cb5b36b4df68 Mon Sep 17 00:00:00 2001 From: h2zero Date: Fri, 26 Aug 2022 08:51:40 -0600 Subject: [PATCH 2/2] Bump version to 3.1.2 --- src/lfs/device.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lfs/device.lua b/src/lfs/device.lua index 99c218a..838c57e 100644 --- a/src/lfs/device.lua +++ b/src/lfs/device.lua @@ -2,7 +2,7 @@ local me = { id = "uuid:8f655392-a778-4fee-97b9-4825918" .. string.format("%x", node.chipid()), name = "Konnected", hwVersion = "3.0.0", - swVersion = "3.1.1", + swVersion = "3.1.2", http_port = math.floor(node.chipid()/1000) + 8000, urn = "urn:schemas-konnected-io:device:Security:1" }