Skip to content

Commit

Permalink
Merge pull request #169 from konnected-io/mem-cleanup
Browse files Browse the repository at this point in the history
memory cleanup
  • Loading branch information
heythisisnate committed Aug 21, 2022
2 parents d6e43a5 + c06f985 commit e4b92c3
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 72 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build

on: pull_request
on: push

jobs:
build-upload:
Expand All @@ -20,16 +20,17 @@ jobs:
sudo apt-get -y install libusb-1.0-0 cmake srecord
pip install -U pyserial
- name: Build
env:
PR_NUMBER: ${{ github.event.number }}
run: ./scripts/build-firmware
- id: build
name: Build
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 }}
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 4 additions & 6 deletions scripts/build-firmware
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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} <port>'"
echo "Build Complete:Flash this build with './scripts/flash ${BRANCH} <port>'"
echo "konnected-esp8266-${BRANCH}.bin"

12 changes: 11 additions & 1 deletion src/lfs/application.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions src/lfs/aws_iot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ 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.Client(settings.aws)
local topics = settings.aws.topics

local sendTimer = tmr.create()
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
Expand All @@ -32,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
Expand All @@ -60,24 +61,24 @@ 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()

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
Expand All @@ -88,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)
Expand All @@ -112,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
Expand Down
2 changes: 1 addition & 1 deletion src/lfs/device.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
1 change: 1 addition & 0 deletions src/lfs/httpd_res.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ local httpdResponse = {

return function()
package.loaded[module] = nil
module = nil
return httpdResponse
end
7 changes: 1 addition & 6 deletions src/lfs/mqtt_packet.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -66,6 +62,5 @@ end
return {
subscribe = subscribe,
publish = publish,
parse = parse,
toHex = toHex
parse = parse
}
15 changes: 6 additions & 9 deletions src/lfs/mqtt_ws.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local module = ...
local mqtt_packet = require('mqtt_packet')

local function emit(self, event, ...)
Expand Down Expand Up @@ -58,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
Expand All @@ -73,7 +69,6 @@ local function Client(aws_settings)
else
print(parsed)
end

end)

ws:on('close', function(_, status)
Expand All @@ -89,6 +84,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
2 changes: 0 additions & 2 deletions src/lfs/rest_endpoint.lua
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
21 changes: 14 additions & 7 deletions src/lfs/server_receiver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ 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")
response.text(sck, "", nil, nil, table.concat({
"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")

Expand All @@ -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")
Expand Down
1 change: 0 additions & 1 deletion src/lfs/variables_build.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local module = ...


local function build_list(objects)
local out = {}
for key, value in pairs(objects) do
Expand Down
21 changes: 0 additions & 21 deletions src/lfs/zone_to_pin.lua

This file was deleted.

0 comments on commit e4b92c3

Please sign in to comment.