From d2789e76d98cd9b1cbb09fb96d03bd07bcdd6877 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Wed, 17 Aug 2022 22:18:53 -0400 Subject: [PATCH 1/9] tiny cleanup --- src/lfs/httpd_res.lua | 1 + src/lfs/variables_build.lua | 1 - src/lfs/zone_to_pin.lua | 4 +--- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lfs/httpd_res.lua b/src/lfs/httpd_res.lua index b2c8004..a254780 100644 --- a/src/lfs/httpd_res.lua +++ b/src/lfs/httpd_res.lua @@ -66,5 +66,6 @@ local httpdResponse = { return function() package.loaded[module] = nil + module = nil return httpdResponse end \ No newline at end of file diff --git a/src/lfs/variables_build.lua b/src/lfs/variables_build.lua index 301dc99..a8ca5ad 100644 --- a/src/lfs/variables_build.lua +++ b/src/lfs/variables_build.lua @@ -1,6 +1,5 @@ local module = ... - local function build_list(objects) local out = {} for key, value in pairs(objects) do diff --git a/src/lfs/zone_to_pin.lua b/src/lfs/zone_to_pin.lua index df24181..0efb652 100644 --- a/src/lfs/zone_to_pin.lua +++ b/src/lfs/zone_to_pin.lua @@ -1,6 +1,4 @@ -local module = ... - -zoneMap = { +local zoneMap = { [1] = 1, [2] = 2, [3] = 5, From 8296ff762098767d6921028b91fd2dfebf517872 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Fri, 19 Aug 2022 15:30:52 -0400 Subject: [PATCH 2/9] build on every push --- .github/workflows/build.yml | 15 +++++++++------ scripts/build-firmware | 10 ++++------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d0e7ee6..6e1786b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,6 @@ name: Build -on: pull_request +on: push jobs: build-upload: @@ -20,16 +20,19 @@ jobs: sudo apt-get -y install libusb-1.0-0 cmake srecord pip install -U pyserial - - name: Build + - id: build + name: Build env: - PR_NUMBER: ${{ github.event.number }} - run: ./scripts/build-firmware + SHA: ${{ github.event.head }} + run: | + fname=$(./scripts/build-firmware | tail -1) + echo "::set-output name=fname::$fname" - uses: keithweaver/aws-s3-github-action@v1.0.0 - name: Copy PR bin to AWS + name: Copy build to AWS with: command: cp - source: build/konnected-esp8266-PR-${{ github.event.number }}.bin + source: build/${{ steps.build.outputs.fname }} destination: s3://konnected-io/builds/ aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} diff --git a/scripts/build-firmware b/scripts/build-firmware index 11707e8..4de2fd9 100755 --- a/scripts/build-firmware +++ b/scripts/build-firmware @@ -15,11 +15,7 @@ function usage() { set -e if ! git describe --exact-match --tags &> /dev/null; then - if [[ -z "${PR_NUMBER}" ]]; then - BRANCH="$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')" - else - BRANCH="PR-"${PR_NUMBER} - fi + BRANCH="$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')" else BRANCH="$(git describe --exact-match --tags $(git log -n1 --pretty='%h'))" fi @@ -108,9 +104,11 @@ echo # Build full image srec_cat -output "${OUTPUT_BASE}/konnected-esp8266-${BRANCH}.bin" -binary "${OUTPUT_BASE}/${IMAGE_NAME}.bin" -binary -fill 0xff 0x0000 0x100000 "${OUTPUT_BASE}/konnected-filesystem-0x100000-${BRANCH}.img" -binary -offset 0x100000 +echo "Wrote ${OUTPUT_BASE}/konnected-esp8266-${BRANCH}.bin" # Copy to common name for ease of flashing cp "${OUTPUT_BASE}/konnected-esp8266-${BRANCH}.bin" "${OUTPUT_BASE}/konnected-esp8266-latest.bin" -echo "Build Complete: Flash this build with './scripts/flash ${BRANCH} '" +echo "Build Complete:Flash this build with './scripts/flash ${BRANCH} '" +echo "konnected-esp8266-${BRANCH}.bin" From 672c3b4b02f2555621e7ded7af861339a7a21965 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Fri, 19 Aug 2022 16:14:23 -0400 Subject: [PATCH 3/9] return a function and unload --- src/lfs/aws_iot.lua | 2 +- src/lfs/mqtt_ws.lua | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lfs/aws_iot.lua b/src/lfs/aws_iot.lua index 8a4a3c4..22d6172 100644 --- a/src/lfs/aws_iot.lua +++ b/src/lfs/aws_iot.lua @@ -4,7 +4,7 @@ local mqtt = require('mqtt_ws') local settings = require('settings') local zoneToPin = require("zone_to_pin") local device_id = wifi.sta.getmac():lower():gsub(':','') -local c = mqtt.Client(settings.aws) +local c = mqtt(settings.aws) local topics = settings.aws.topics local sendTimer = tmr.create() diff --git a/src/lfs/mqtt_ws.lua b/src/lfs/mqtt_ws.lua index 4fbcb0a..21794c9 100644 --- a/src/lfs/mqtt_ws.lua +++ b/src/lfs/mqtt_ws.lua @@ -1,3 +1,4 @@ +local module = ... local mqtt_packet = require('mqtt_packet') local function emit(self, event, ...) @@ -89,6 +90,8 @@ local function Client(aws_settings) return client end -return { - Client = Client -} +return function(aws_settings) + package.loaded[module] = nil + module = nil + return Client(aws_settings) +end From 2fe4b5c67a1b9eea179eacc6c68f55960329cc48 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Fri, 19 Aug 2022 17:13:43 -0400 Subject: [PATCH 4/9] remove dead code --- src/lfs/mqtt_packet.lua | 7 +------ src/lfs/mqtt_ws.lua | 6 ------ 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/lfs/mqtt_packet.lua b/src/lfs/mqtt_packet.lua index ac02c1a..b8beb56 100644 --- a/src/lfs/mqtt_packet.lua +++ b/src/lfs/mqtt_packet.lua @@ -1,9 +1,5 @@ local bit32 = bit32 or bit -local function toHex(str) - return str:gsub("(.)", function(s) return string.format("%02x ", string.byte(s)) end) -end - local function lengthStr(length) local buf = "" local digit = 0 @@ -66,6 +62,5 @@ end return { subscribe = subscribe, publish = publish, - parse = parse, - toHex = toHex + parse = parse } diff --git a/src/lfs/mqtt_ws.lua b/src/lfs/mqtt_ws.lua index 21794c9..0c6fd59 100644 --- a/src/lfs/mqtt_ws.lua +++ b/src/lfs/mqtt_ws.lua @@ -59,12 +59,7 @@ local function Client(aws_settings) } ws:on('receive', function(_, msg, opcode, x) --- print("received", msg:len(), "msg:", msg, "bytes:", mqtt_packet.toHex(msg)) local parsed = mqtt_packet.parse(msg) --- for k, v in pairs(parsed) do --- print('>', k, v) --- end - if parsed.cmd == 4 then client:emit('puback', parsed.message_id) elseif parsed.cmd == 3 then @@ -74,7 +69,6 @@ local function Client(aws_settings) else print(parsed) end - end) ws:on('close', function(_, status) From aa8fc9ebb24bc02eaa2eb4faafe38a01b1b81285 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Fri, 19 Aug 2022 17:34:39 -0400 Subject: [PATCH 5/9] remove unnecessary env --- .github/workflows/build.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6e1786b..6bad931 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,8 +22,6 @@ jobs: - id: build name: Build - env: - SHA: ${{ github.event.head }} run: | fname=$(./scripts/build-firmware | tail -1) echo "::set-output name=fname::$fname" From 42325f1fa67a10f5fc6415dc16308d1d58026bd9 Mon Sep 17 00:00:00 2001 From: h2zero Date: Fri, 19 Aug 2022 20:45:01 -0600 Subject: [PATCH 6/9] Change zoneToPin to a global function that is executable from flash. This allows the Lua complier to execute the zoneToPin function directly from flash with no RAM used for the previous table. --- src/lfs/application.lua | 12 +++++++++++- src/lfs/aws_iot.lua | 1 - src/lfs/rest_endpoint.lua | 2 -- src/lfs/zone_to_pin.lua | 19 ------------------- 4 files changed, 11 insertions(+), 23 deletions(-) delete mode 100644 src/lfs/zone_to_pin.lua diff --git a/src/lfs/application.lua b/src/lfs/application.lua index 51d5634..c812901 100644 --- a/src/lfs/application.lua +++ b/src/lfs/application.lua @@ -6,11 +6,21 @@ local actuators = require("actuators") local settings = require("settings") local ds18b20 = require("ds18b20") local sensorTimer = tmr.create() -local zoneToPin = require("zone_to_pin") -- globals sensorPut = {} actuatorGet = {} +function zoneToPin(zone) + -- handle strings or numbers + --return zoneMap[zone] or zoneMap[tonumber(zone)] + if zone == 1 or zone == '1' then return 1 end + if zone == 2 or zone == '2' then return 2 end + if zone == 3 or zone == '3' then return 5 end + if zone == 4 or zone == '4' then return 6 end + if zone == 5 or zone == '5' then return 7 end + if zone == 6 or zone == '6' then return 9 end + if zone == "out" then return 8 end +end local function getDevicePin(device) if device.zone ~= nil then diff --git a/src/lfs/aws_iot.lua b/src/lfs/aws_iot.lua index 22d6172..ff75652 100644 --- a/src/lfs/aws_iot.lua +++ b/src/lfs/aws_iot.lua @@ -2,7 +2,6 @@ local module = ... local mqtt = require('mqtt_ws') local settings = require('settings') -local zoneToPin = require("zone_to_pin") local device_id = wifi.sta.getmac():lower():gsub(':','') local c = mqtt(settings.aws) local topics = settings.aws.topics diff --git a/src/lfs/rest_endpoint.lua b/src/lfs/rest_endpoint.lua index 5d418df..65ae955 100644 --- a/src/lfs/rest_endpoint.lua +++ b/src/lfs/rest_endpoint.lua @@ -1,7 +1,5 @@ local module = ... -local zoneToPin = require("zone_to_pin") - -- print HTTP status line local function printHttpResponse(code, data) local a = { "Heap:", node.heap(), "HTTP Call:", code } diff --git a/src/lfs/zone_to_pin.lua b/src/lfs/zone_to_pin.lua deleted file mode 100644 index 0efb652..0000000 --- a/src/lfs/zone_to_pin.lua +++ /dev/null @@ -1,19 +0,0 @@ -local zoneMap = { - [1] = 1, - [2] = 2, - [3] = 5, - [4] = 6, - [5] = 7, - [6] = 9, - ["out"] = 8, -} - -local function zoneToPin(zone) - -- handle strings or numbers - return zoneMap[zone] or zoneMap[tonumber(zone)] -end - -return function(zone) - -- we won't unload this since it's small and frequently called... - return zoneToPin(zone) -end From c3620e4578e3b637022b86c87c2640ccdcf76e3f Mon Sep 17 00:00:00 2001 From: h2zero Date: Fri, 19 Aug 2022 21:05:13 -0600 Subject: [PATCH 7/9] Use a global for mqtt client to allow method access. * Allows for modules to access mqtt methods for situations where it may be required. * When settings are pushed the mqtt client will now be disconnected before writing the new settings, which is now handled in the "offline" method of the mqtt client. This allows for larger payloads by freeing memory before processing, preventing "out of RAM" errors. --- src/lfs/aws_iot.lua | 22 ++++++++++++---------- src/lfs/server_receiver.lua | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/lfs/aws_iot.lua b/src/lfs/aws_iot.lua index ff75652..21d7ca2 100644 --- a/src/lfs/aws_iot.lua +++ b/src/lfs/aws_iot.lua @@ -3,7 +3,6 @@ local module = ... local mqtt = require('mqtt_ws') local settings = require('settings') local device_id = wifi.sta.getmac():lower():gsub(':','') -local c = mqtt(settings.aws) local topics = settings.aws.topics local sendTimer = tmr.create() @@ -11,6 +10,9 @@ local timeout = tmr.create() local mqttTimeout = tmr.create() local heartbeat = tmr.create() +-- Global client for method access +mqttC = mqtt(settings.aws) + timeout:register(3000, tmr.ALARM_SEMI, function() sensorPut[1].retry = (sensorPut[1].retry or 0) + 1 sensorPut[1].message_id = nil @@ -31,12 +33,12 @@ sendTimer:register(200, tmr.ALARM_AUTO, function(t) for k, v in pairs(sensorPut) do sensorPut[k] = nil end -- remove all pending sensor updates tmr.create():alarm(30000, tmr.ALARM_SINGLE, function() node.restart() end) -- reboot in 30 sec else - local message_id = c.msg_id + local message_id = mqttC.msg_id local topic = sensor.topic or topics.sensor sensor.device_id = device_id print("Heap:", node.heap(), "PUBLISH", "Message ID:", message_id, "Topic:", topic, "Payload:", sjson.encode(sensor)) timeout:start() - c:publish(topic, sensor) + mqttC:publish(topic, sensor) sensor.message_id = message_id end end @@ -59,7 +61,7 @@ local function startLoop() print("Heap:", node.heap(), 'Connecting to AWS IoT Endpoint:', settings.endpoint) local mqttFails = 0 - c:on('offline', function() + mqttC:on('offline', function() mqttFails = mqttFails + 1 print("Heap:", node.heap(), "mqtt: offline", "failures:", mqttFails) sendTimer:stop() @@ -67,16 +69,16 @@ local function startLoop() if mqttFails >= 10 then tmr.create():alarm(3000, tmr.ALARM_SINGLE, function() node.restart() end) -- reboot in 3 sec else - c:connect(settings.endpoint) + mqttC:connect(settings.endpoint) end end) mqttTimeout:start() -- stripping -ats uses the endpoint with a smaller cert chain - c:connect(string.gsub(settings.endpoint, "-ats", "")) + mqttC:connect(string.gsub(settings.endpoint, "-ats", "")) end -c:on('puback', function(_, message_id) +mqttC:on('puback', function(_, message_id) print("Heap:", node.heap(), 'PUBACK', 'Message ID:', message_id) local sensor = sensorPut[1] if sensor and sensor.message_id == message_id then @@ -87,7 +89,7 @@ c:on('puback', function(_, message_id) end end) -c:on('message', function(_, topic, message) +mqttC:on('message', function(_, topic, message) print("Heap:", node.heap(), 'topic:', topic, 'msg:', message) local payload = sjson.decode(message) local endState = require("switch")(payload) @@ -111,11 +113,11 @@ c:on('message', function(_, topic, message) end end) -c:on('connect', function() +mqttC:on('connect', function() mqttTimeout:stop() print("Heap:", node.heap(), "mqtt: connected") print("Heap:", node.heap(), "Subscribing to topic:", topics.switch) - c:subscribe(topics.switch) + mqttC:subscribe(topics.switch) -- update current state of actuators upon boot for i, actuator in pairs(actuatorGet) do diff --git a/src/lfs/server_receiver.lua b/src/lfs/server_receiver.lua index 70c1bf4..bd7fcf3 100644 --- a/src/lfs/server_receiver.lua +++ b/src/lfs/server_receiver.lua @@ -17,8 +17,8 @@ local function httpReceiver(sck, payload) end collectgarbage() - local request = require("httpd_req")(payload) - local response = require("httpd_res")() + request = require("httpd_req")(payload) + response = require("httpd_res")() if request.method == 'OPTIONS' then print("Heap: ", node.heap(), "HTTP: ", "Options") @@ -26,10 +26,8 @@ local function httpReceiver(sck, payload) "Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS\r\n", "Access-Control-Allow-Headers: Content-Type\r\n" })) - return - end - if request.path == "/" then + elseif request.path == "/" then print("Heap: ", node.heap(), "HTTP: ", "Index") response.file(sck, "http_index.html") @@ -41,8 +39,17 @@ local function httpReceiver(sck, payload) print("Heap: ", node.heap(), "HTTP: ", "Discovery") elseif request.path == "/settings" then - print("Heap: ", node.heap(), "HTTP: ", "Settings") - response.text(sck, require("server_settings")(request)) + 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 elseif request.path == "/device" then print("Heap: ", node.heap(), "HTTP: ", "Device") From 889f889e21a57f00841449b6262c845f48387638 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Sat, 20 Aug 2022 16:24:43 -0400 Subject: [PATCH 8/9] bump v3.1.1 --- 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 f1e1cdf..99c218a 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.0", + swVersion = "3.1.1", http_port = math.floor(node.chipid()/1000) + 8000, urn = "urn:schemas-konnected-io:device:Security:1" } From c06f98548748635ceeea123fa13dfc88dc324203 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Sun, 21 Aug 2022 16:13:02 -0400 Subject: [PATCH 9/9] release images to S3 automatically --- .github/workflows/release.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8936876..7ad27a4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,3 +41,23 @@ jobs: release-url: ${{ steps.get_release.outputs.upload_url }} allow-overwrite: true github-token: ${{ secrets.GITHUB_TOKEN }} + + - uses: keithweaver/aws-s3-github-action@v1.0.0 + name: Copy image to S3 + with: + command: cp + source: assets/konnected-esp8266-${{ steps.get_release.outputs.tag_name }}.bin + destination: s3://konnected-io/esp8266/images/ + aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws_region: us-east-2 + + - uses: keithweaver/aws-s3-github-action@v1.0.0 + name: Copy LFS to S3 + with: + command: cp + source: assets/lfs-${{ steps.get_release.outputs.tag_name }}.img + destination: s3://konnected-io/esp8266/lfs/ + aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws_region: us-east-2