forked from db4linq/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_ralay.lua
More file actions
71 lines (60 loc) · 1.73 KB
/
node_ralay.lua
File metadata and controls
71 lines (60 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
gpio.mode(5,gpio.OUTPUT)
gpio.mode(6,gpio.OUTPUT)
gpio.mode(7,gpio.OUTPUT)
gpio.write(5, 0)
gpio.write(6, 0)
gpio.write(7, 0)
server = "192.168.43.94"
port = 5683
node_name = "Lighting-01"
conn=net.createConnection(net.TCP, 0)
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function open()
print("Open connection...")
conn:on("receive", receive)
conn:on("disconnection", function() print("Disconnection..") end)
conn:on("reconnection", function() print("Reconnection..") end)
conn:on("connection", function() print("Connected..") end)
conn:connect(port, server)
conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}')
end
function receive(conn, payload)
--print(payload)
list=split(payload,"|")
if (list[1] == 'V') then
if (list[2] == 'heap') then
conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}')
end
end
if (list[1] == 'P') then
if (list[2] == 'heap') then
conn:send('{ "type": 6, "data": "ok"}')
end
end
if (list[1] == 'F') then
if (list[2] == 'toggle') then
toggle(list[3])
end
end
end
function close()
conn:close()
end
function toggle(param)
--print(param)
local pin = tonumber(param)
print("Pin: " .. pin)
if gpio.read(pin) == 1 then
gpio.write(pin, 0)
else
gpio.write(pin, 1)
end
conn:send('{"type": 3, "data": { "status": '..gpio.read(pin)..', "coreID": "'.. node.chipid() ..'", "id": '..pin..' }}')
end
open()