Skip to content

Commit

Permalink
Fix OTA updates when connected to AWS.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
h2zero committed Aug 26, 2022
1 parent d0344d0 commit dc345dc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lfs/http_ota.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions src/lfs/server_receiver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lfs/start.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
17 changes: 12 additions & 5 deletions src/lfs/wifi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dc345dc

Please sign in to comment.