-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathnode_soil.lua
More file actions
87 lines (84 loc) · 2.66 KB
/
node_soil.lua
File metadata and controls
87 lines (84 loc) · 2.66 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
79
80
81
82
83
84
85
86
server = "103.22.180.136"
port = 5683
node_name = "Soil-01"
conn=nil
gpio.mode(5,gpio.INPUT,gpio.FLOAT)
gpio.mode(6,gpio.INPUT,gpio.FLOAT)
soil = "HIGH"
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=net.createConnection(net.TCP, 0)
conn:on("receive", receive)
conn:on("disconnection", function()
print("Disconnection..")
tmr.stop(0)
conn= nil
if (wifi.sta.status() == 5) then
tmr.alarm(0, 5000, 1, function()
open()
tmr.stop(0)
end)
end
end);
conn:on("reconnection", function() print("Disconnection..") end);
conn:on("connection", function()
print("Connected..")
start()
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
if (list[2] == 'soil1') then
soil = "HIGH"
if gpio.read(5)==1 then
soil = "LOW"
end
conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 1, "value": "'..soil..'"}}')
end
if (list[2] == 'soil2') then
soil = "HIGH"
if gpio.read(6)==1 then
soil = "LOW"
end
conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 2, "value": "'..soil..'"}}')
end
end
end
status_soil2 = "HIGH"
oldstatus_soil2 = "HIGH"
status_soil3 = "HIGH"
oldstatus_soil3 = "HIGH"
function sendalarm(id, status)
conn:send('{ "type": 4, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}')
end
function start()
tmr.alarm(0, 3000, 1, function() -- Set alarm to one second
if gpio.read(5)==1 then status_soil2="LOW" else status_soil2="HIGH" end
if status_soil2 ~= oldstatus_soil2 then sendalarm (1, status_soil2) end
oldstatus_soil2 = status_soil2
if gpio.read(6)==1 then status_soil3="LOW" else status_soil3="HIGH" end
if status_soil3 ~= oldstatus_soil3 then sendalarm (2, status_soil3) end
oldstatus_soil3 = status_soil3
end)
end
function close()
conn:close()
end
tmr.alarm(0, 2000, 1, function()
open()
tmr.stop(0)
end)
open()