Skip to content

Commit

Permalink
properly handle momentary switch in AWS IoT; failsafe reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisisnate committed Mar 25, 2020
1 parent d52b649 commit 6d186e0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions scripts/build-firmware
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ rm -rf "${BUILD_PATH}/*"
cp "${FIRMWARE_PATH}/bin/nodemcu_integer_${IMAGE_NAME}.bin" "${BUILD_PATH}/${IMAGE_NAME}.bin"
cp "${FIRMWARE_PATH}/bin/konnected-filesystem-0x100000.img" "${BUILD_PATH}/konnected-filesystem-0x100000-${VERSION}.img"

srec_cat -output "${BUILD_PATH}/konnected-esp8266-${VERSION}.bin" -binary "${BUILD_PATH}/${IMAGE_NAME}.bin" -binary -fill 0xff 0x0000 0x100000 "${BUILD_PATH}/konnected-filesystem-0x100000-${VERSION}.img" -binary -offset 0x100000


echo "Build Complete: Flash this build with './scripts/flash ${VERSION} <port>'"
24 changes: 20 additions & 4 deletions src/lfs/aws_iot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,17 @@ end)
local function startLoop()
print("Heap:", node.heap(), 'Connecting to AWS IoT Endpoint:', settings.endpoint)

local mqttFails = 0
c:on('offline', function()
print("Heap:", node.heap(), "mqtt: offline")
mqttFails = mqttFails + 1
print("Heap:", node.heap(), "mqtt: offline", "failures:", mqttFails)
sendTimer:stop()
c:connect(settings.endpoint)

if mqttFails >= 10 then
tmr.create():alarm(3000, tmr.ALARM_SINGLE, function() node.restart() end) -- reboot in 3 sec
else
c:connect(settings.endpoint)
end
end)

c:connect(settings.endpoint)
Expand All @@ -75,10 +82,19 @@ end)
c:on('message', function(_, topic, message)
print("Heap:", node.heap(), 'topic:', topic, 'msg:', message)
local payload = sjson.decode(message)
require("switch")(payload)
local endState = require("switch")(payload)

-- publish the new state after actuating switch
table.insert(sensorPut, { pin = payload.pin, state = gpio.read(payload.pin) })
table.insert(sensorPut, endState)

-- set state back to initial after momentary is complete
if payload.momentary and payload.times > 0 then
revertIn = (payload.momentary + payload.pause) * payload.times - payload.pause
tmr.create():alarm(revertIn, tmr.ALARM_SINGLE, function()
local revertState = { pin = endState.pin, state = endState.state == 0 and 1 or 0}
table.insert(sensorPut, revertState)
end)
end
end)

c:on('connect', function()
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 = "2.3.0",
swVersion = "2.3.3",
swVersion = "2.3.4",
http_port = math.floor(node.chipid()/1000) + 8000,
urn = "urn:schemas-konnected-io:device:Security:1"
}
Expand Down

0 comments on commit 6d186e0

Please sign in to comment.