forked from db4linq/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_sensor.lua
More file actions
79 lines (70 loc) · 1.99 KB
/
node_sensor.lua
File metadata and controls
79 lines (70 loc) · 1.99 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
71
72
73
74
75
76
77
78
print("\r\n\r\nDevice ID: "..node.chipid())
server = "192.168.1.48"
port = 5683
node_name = "Test"
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)
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] == 'F') then
if (list[2] == 'led') then
led(list[3])
end
if (list[2] == 'toggle') then
toggle(list[3])
end
if (list[2] == 'status') then
status(list[3])
end
end
end
function close()
conn:close()
end
function led(param)
print(param)
list=split(param,",")
local pin = tonumber(list[1])
local state = tonumber(list[2])
print("Pin: " .. pin)
print("State: " .. state)
gpio.write(pin, state)
conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}')
end
function toggle(param)
print(param)
list=split(param,",")
local pin = tonumber(list[1])
print("Pin: " .. pin)
if gpio.read(pin) == 1 then
gpio.write(pin, 0)
else
gpio.write(pin, 1)
end
conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}')
end
function status(param)
local pin = tonumber(param)
conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}')
end
open()