-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from konnected-io/aws-iot
2.3.0: Cloud Connectivity, OTA Updates, foundational support for MQTT
- Loading branch information
Showing
25 changed files
with
634 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
local module = ... | ||
|
||
local mqtt = require('mqtt_ws') | ||
local device_id = wifi.sta.getmac():lower():gsub(':','') | ||
local c = mqtt.Client() | ||
|
||
local function aws_sign_url(settings) | ||
local aws_sig = require('aws_sig') | ||
local url = aws_sig.createSignature( | ||
settings.aws.access_key, settings.aws.secret_key, | ||
settings.aws.region, 'iotdevicegateway', 'GET', settings.endpoint, '') | ||
|
||
aws_sig = nil | ||
package.loaded.aws_sig = nil | ||
return url | ||
end | ||
|
||
local sendTimer = tmr.create() | ||
local timeout = tmr.create() | ||
|
||
timeout:register(3000, tmr.ALARM_SEMI, function() | ||
sensorPut[1].retry = (sensorPut[1].retry or 0) + 1 | ||
sensorPut[1].message_id = nil | ||
sendTimer:start() | ||
end) | ||
|
||
sendTimer:register(200, tmr.ALARM_AUTO, function(t) | ||
local sensor = sensorPut[1] | ||
if sensor then | ||
t:stop() | ||
|
||
if sensor.retry and sensor.retry > 0 then | ||
print("Heap:", node.heap(), "Retry:", sensor.retry) | ||
end | ||
|
||
if sensor.retry and sensor.retry > 10 then | ||
print("Heap:", node.heap(), "Retried 10 times and failed. Rebooting in 30 seconds.") | ||
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 topic = "konnected/" .. device_id .. "/sensor/" .. sensor.pin | ||
print("Heap:", node.heap(), "PUBLISH", "Message ID:", message_id, "Topic:", topic, "Payload:", sjson.encode(sensor)) | ||
timeout:start() | ||
c:publish(topic, sensor) | ||
sensor.message_id = message_id | ||
end | ||
end | ||
end) | ||
|
||
local function startLoop(settings) | ||
print("Heap:", node.heap(), 'Connecting to AWS IoT Endpoint:', settings.endpoint) | ||
|
||
c:on('offline', function() | ||
print("Heap:", node.heap(), "mqtt: offline") | ||
sendTimer:stop() | ||
c:connect(aws_sign_url(settings)) | ||
end) | ||
|
||
c:connect(aws_sign_url(settings)) | ||
end | ||
|
||
c:on('puback', function(_, message_id) | ||
local sensor = sensorPut[1] | ||
if sensor.message_id == message_id then | ||
print("Heap:", node.heap(), 'PUBACK', 'Message ID:', message_id) | ||
table.remove(sensorPut, 1) | ||
blinktimer:start() | ||
timeout:stop() | ||
sendTimer:start() | ||
end | ||
end) | ||
|
||
c:on('message', function(_, topic, message) | ||
print("Heap:", node.heap(), 'topic:', topic, 'msg:', message) | ||
local payload = sjson.decode(message) | ||
require("switch")(payload) | ||
end) | ||
|
||
c:on('connect', function() | ||
print("Heap:", node.heap(), "mqtt: connected") | ||
for i, actuator in pairs(actuatorGet) do | ||
local topic = "konnected/" .. device_id .. "/switch/" .. actuator.pin | ||
print("Heap:", node.heap(), "Subscribing to topic:", topic) | ||
c:subscribe(topic) | ||
end | ||
sendTimer:start() | ||
end) | ||
|
||
return function(settings) | ||
package.loaded[module] = nil | ||
module = nil | ||
return startLoop(settings) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
local module = {} | ||
local function sign(key, msg) | ||
return crypto.hmac('SHA256', msg, key) | ||
end | ||
|
||
local function getSignatureKey(key, dateStamp, regionName, serviceName) | ||
local kDate = sign('AWS4' .. key, dateStamp) | ||
local kRegion = sign(kDate, regionName) | ||
local kService = sign(kRegion, serviceName) | ||
return sign(kService, 'aws4_request') | ||
end | ||
|
||
local function url_quote(text) | ||
return string.gsub(text, "([^%w_%-~%.])", function(c) return string.format("%%%02X", string.byte(c)) end) | ||
end | ||
|
||
local function createSignature(aws_access_key, aws_secret_key, region, service, method, url, payload) | ||
local t = rtctime.epoch2cal(rtctime.get()) | ||
local amz_date = string.format("%04d%02d%02dT%02d%02d%02dZ", t['year'], t['mon'], t['day'], t['hour'], t['min'], t['sec']) | ||
local datestamp = amz_date:sub(1,8) | ||
|
||
local protocol, host, path | ||
protocol, host, path = string.match(url, "(%w+)://([^/]+)(.*)") | ||
path = path or '/' | ||
|
||
local canonical_headers = 'host:' .. host .. ':443\n' | ||
local signed_headers = 'host' | ||
|
||
local credential_scope = datestamp .. '/' .. region .. '/' .. service .. '/' .. 'aws4_request' | ||
|
||
local canonical_querystring = ( | ||
'X-Amz-Algorithm=AWS4-HMAC-SHA256' .. | ||
'&X-Amz-Credential=' .. url_quote(aws_access_key .. '/' .. credential_scope) .. | ||
'&X-Amz-Date=' .. amz_date .. | ||
'&X-Amz-Expires=86400' .. | ||
'&X-Amz-SignedHeaders=' .. signed_headers | ||
) | ||
local payload_hash = 'UNSIGNED-PAYLOAD' | ||
if payload ~= nil then | ||
payload_hash = crypto.toHex(crypto.hash('SHA256', payload)) | ||
end | ||
local canonical_request = ( | ||
method .. '\n' .. path .. '\n' .. | ||
canonical_querystring .. '\n' .. canonical_headers .. '\n' .. | ||
signed_headers .. '\n' .. payload_hash | ||
) | ||
-- print('canonical_request', canonical_request) | ||
local string_to_sign = 'AWS4-HMAC-SHA256\n' .. amz_date .. '\n' .. credential_scope .. '\n' .. crypto.toHex(crypto.hash('SHA256', canonical_request)) | ||
|
||
local signing_key = getSignatureKey(aws_secret_key, datestamp, region, service) | ||
local signature = crypto.toHex(crypto.hmac('SHA256', string_to_sign, signing_key)) | ||
|
||
return protocol .. '://' .. host .. path .. '?' .. canonical_querystring .. '&X-Amz-Signature=' .. signature | ||
end | ||
module.createSignature = createSignature | ||
return module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.