aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.editorconfig11
-rw-r--r--diller/inter.lua4
-rw-r--r--diller/main.lua87
-rw-r--r--diller/manual_init.lua9
-rw-r--r--diller/mq.lua52
5 files changed, 78 insertions, 85 deletions
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..774f1b7
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+root = true
+
+# Unix-style newlines with a newline ending every file
+[*]
+end_of_line = lf
+insert_final_newline = true
+charset = utf-8
+indent_style = space
+
+[*.lua]
+indent_size = 2
diff --git a/diller/inter.lua b/diller/inter.lua
index e10e1df..8820727 100644
--- a/diller/inter.lua
+++ b/diller/inter.lua
@@ -11,7 +11,7 @@ function P.parse(line)
local i = string.find(line, " ")
if not i then
-- print("inter_parse: invalid line: no command, line="..line)
- return string.len(line) > 0 and line or nil, args
+ return string.len(line) > 0 and line or nil, args
end
local cmd = string.sub(line, 1, i - 1)
-- print("inter_parse: cmd="..cmd)
@@ -37,7 +37,7 @@ function P.parse(line)
else
key = string.sub(arg, 1, j-1)
value = string.sub(arg, j+1)
- args[key] = value
+ args[key] = value
-- print("inter_parse: key="..key..", value="..value)
end
end
diff --git a/diller/main.lua b/diller/main.lua
index eca88a9..f982f8c 100644
--- a/diller/main.lua
+++ b/diller/main.lua
@@ -11,7 +11,7 @@ local mq = require('mq')
local function on_cmd(cmd, args)
if not cmd then
- return
+ return
end
print("on_cmd: '"..cmd.."', #args="..tostring(table.getn(args)))
@@ -20,57 +20,57 @@ local function on_cmd(cmd, args)
end
if cmd == "reset" then
- print("ok")
+ print("ok")
panic("Reset requested")
elseif cmd == "wlan" then
- local ssid = args.ssid password = args.password
+ local ssid = args.ssid password = args.password
- if args.ssid then
- wifi.sta.config(args.ssid, args.password, 1)
- end
- ssid, password, bssid_set, bssid = wifi.sta.getconfig()
- print("ok ssid="..(ssid or ''))
+ if args.ssid then
+ wifi.sta.config(args.ssid, args.password, 1)
+ end
+ ssid, password, bssid_set, bssid = wifi.sta.getconfig()
+ print("ok ssid="..(ssid or ''))
elseif cmd == "network" then
- local ip = args.ip nm = args.netmask gw = args.gateway
+ local ip = args.ip nm = args.netmask gw = args.gateway
- if args.ip then
- wifi.sta.setip({ip=ip, netmask=nm, gateway=gw})
- end
+ if args.ip then
+ wifi.sta.setip({ip=ip, netmask=nm, gateway=gw})
+ end
- ip, nm, gw = wifi.sta.getip()
- print("ok ip="..(ip or '').." netmask="..(nm or '').." gateway="..(gw or ''))
+ ip, nm, gw = wifi.sta.getip()
+ print("ok ip="..(ip or '').." netmask="..(nm or '').." gateway="..(gw or ''))
elseif cmd == "set-property" then
- local id = args.id
- if not id then
- print("fail status=missing-id")
- else
- local name_path = id.."/name"
- mq.subscribe(name_path, function() print('message on '..path) end)
-
- local description_path = id.."/description"
- mq.subscribe(description_path, function() print('message on '..path) end)
-
- if args.value then
- local path = id.."/value"
- mq.publish(path, args.value)
- end
- if args.name then
- mq.publish(name_path, args.name)
- end
- if args.description then
- mq.publish(description_path, args.description)
- end
- end
+ local id = args.id
+ if not id then
+ print("fail status=missing-id")
+ else
+ local name_path = id.."/name"
+ mq.subscribe(name_path, function() print('message on '..path) end)
+
+ local description_path = id.."/description"
+ mq.subscribe(description_path, function() print('message on '..path) end)
+
+ if args.value then
+ local path = id.."/value"
+ mq.publish(path, args.value)
+ end
+ if args.name then
+ mq.publish(name_path, args.name)
+ end
+ if args.description then
+ mq.publish(description_path, args.description)
+ end
+ end
elseif cmd == "publish" then
-- print("Publishing, topic="..tostring(cmd.topic)..", payload="..tostring(cmd.payload))
- ok, msg = mq.publish(cmd.topic, cmd.payload)
+ ok, msg = mq.publish(cmd.topic, cmd.payload)
if ok then
- print("ok status="..msg)
- else
- print("failed status="..msg)
- end
+ print("ok status="..msg)
+ else
+ print("failed status="..msg)
+ end
else
- print("failed status=unknown-command")
+ print("failed status=unknown-command")
end
end
@@ -114,8 +114,9 @@ function P.main()
wifi.setmode(wifi.STATION)
wifi.sta.config(wlan_ssid, wlan_password)
- local client_id = "esp8266-"..wifi.sta.getmac()
- mq.init(timers.mqtt, client_id)
+ local mac = wifi.sta.getmac()
+ local client_id = "esp8266-"..mac
+ mq.init(timers.mqtt, mac, client_id)
inter.init(on_cmd)
diff --git a/diller/manual_init.lua b/diller/manual_init.lua
new file mode 100644
index 0000000..f9c77a0
--- /dev/null
+++ b/diller/manual_init.lua
@@ -0,0 +1,9 @@
+node.compile('inter.lua');
+node.compile('mq.lua');
+node.compile('main.lua');
+
+print("network");
+print("wlan");
+print("set-property id=temp-0 value=12.3 type=temperature name=Water");
+require('main').main();
+
diff --git a/diller/mq.lua b/diller/mq.lua
index 74f3ef8..212d964 100644
--- a/diller/mq.lua
+++ b/diller/mq.lua
@@ -1,48 +1,19 @@
local P = {}
local m, topic, cid
-local active_subscriptions = {}
-local pending_subscriptions = {}
-local pending_subcsription
+local subscriptions = {}
local function p(msg)
print("MQTT: "..msg)
end
--- Subscribe to all registered subscriptions
--- TODO: subscriptions that are not successfull will remain in state = 1. Might not be a real problem if it happens, the server probably disallowed the subscription.
-local function create_subscriptions()
- print("create_subscriptions, pending count "..table.getn(pending_subscriptions)..", pending: "..tostring(pending_subscription))
-
- if (pending_subscription) then
- return
- end
-
- pending_subcsription = table.remove(pending_subscriptions)
-
- -- The API claims that the callback has this signature: function(client, topic, message)
- -- but that doesn't seems right from the source code. The code doesn't
- -- remember the topics that is being subscribed to.
- m:subscribe(pending_subcsription, 0, function(client)
- print("success: "..pending_subcsription)
- table.insert(active_subscriptions, pending_subscription)
- pending_subscription = nil
- create_subscriptions()
- end)
-end
-
local function mq_client_connected(con)
p("connected")
-
- create_subscriptions()
end
local function disconnect()
p("Lost wifi connection, disconnecting")
m:close()
m = nil
-
- -- append active_subscriptions to pending_subscriptions
- active_subscriptions = {}
end
function mq_on_timer()
@@ -66,21 +37,21 @@ function mq_on_timer()
end
else
if m then
- disconnect()
+ disconnect()
end
end
end
-function P.init(timer_id, client_id)
+function P.init(timer_id, device_id, client_id)
cid = client_id
- topic = "/esp8266/"..client_id
+ topic = "/diller/"..device_id
- tmr.alarm(timer_id, 3 * 1000, 1, mq_on_timer)
-end
+ tmr.alarm(timer_id, 4 * 1000, 1, mq_on_timer)
+end
function P.subscribe(path, cb)
- subscription = subscriptions[path]
+ local subscription = subscriptions[path]
if subscription then
print("subscription on "..path.." already registered, state="..tostring(subscription.state))
return
@@ -88,15 +59,16 @@ function P.subscribe(path, cb)
print("Registering subscription on "..path)
- pending_subscriptions[path] = true
+ subscriptions[path] = true
+ -- pending_subscriptions[path] = true
- create_subscriptions()
+ -- create_subscriptions()
end
function P.publish(path, payload)
if not m then
- print("Not connected, dropping message to "..path)
- return false, 'not connected'
+ print("Not connected, dropping message to "..path)
+ return false, 'not connected'
end
path = topic.."/"..path