Skip to content

Commit 6d186e0

Browse files
committed
properly handle momentary switch in AWS IoT; failsafe reconnect
1 parent d52b649 commit 6d186e0

6 files changed

Lines changed: 24 additions & 5 deletions

File tree

1.23 MB
Binary file not shown.
232 KB
Binary file not shown.
732 KB
Binary file not shown.

scripts/build-firmware

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ rm -rf "${BUILD_PATH}/*"
5555
cp "${FIRMWARE_PATH}/bin/nodemcu_integer_${IMAGE_NAME}.bin" "${BUILD_PATH}/${IMAGE_NAME}.bin"
5656
cp "${FIRMWARE_PATH}/bin/konnected-filesystem-0x100000.img" "${BUILD_PATH}/konnected-filesystem-0x100000-${VERSION}.img"
5757

58+
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
59+
60+
5861
echo "Build Complete: Flash this build with './scripts/flash ${VERSION} <port>'"

src/lfs/aws_iot.lua

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ end)
5252
local function startLoop()
5353
print("Heap:", node.heap(), 'Connecting to AWS IoT Endpoint:', settings.endpoint)
5454

55+
local mqttFails = 0
5556
c:on('offline', function()
56-
print("Heap:", node.heap(), "mqtt: offline")
57+
mqttFails = mqttFails + 1
58+
print("Heap:", node.heap(), "mqtt: offline", "failures:", mqttFails)
5759
sendTimer:stop()
58-
c:connect(settings.endpoint)
60+
61+
if mqttFails >= 10 then
62+
tmr.create():alarm(3000, tmr.ALARM_SINGLE, function() node.restart() end) -- reboot in 3 sec
63+
else
64+
c:connect(settings.endpoint)
65+
end
5966
end)
6067

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

8087
-- publish the new state after actuating switch
81-
table.insert(sensorPut, { pin = payload.pin, state = gpio.read(payload.pin) })
88+
table.insert(sensorPut, endState)
89+
90+
-- set state back to initial after momentary is complete
91+
if payload.momentary and payload.times > 0 then
92+
revertIn = (payload.momentary + payload.pause) * payload.times - payload.pause
93+
tmr.create():alarm(revertIn, tmr.ALARM_SINGLE, function()
94+
local revertState = { pin = endState.pin, state = endState.state == 0 and 1 or 0}
95+
table.insert(sensorPut, revertState)
96+
end)
97+
end
8298
end)
8399

84100
c:on('connect', function()

src/lfs/device.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local me = {
22
id = "uuid:8f655392-a778-4fee-97b9-4825918" .. string.format("%x", node.chipid()),
33
name = "Konnected",
44
hwVersion = "2.3.0",
5-
swVersion = "2.3.3",
5+
swVersion = "2.3.4",
66
http_port = math.floor(node.chipid()/1000) + 8000,
77
urn = "urn:schemas-konnected-io:device:Security:1"
88
}

0 commit comments

Comments
 (0)