-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpir.lua
More file actions
34 lines (25 loc) · 792 Bytes
/
pir.lua
File metadata and controls
34 lines (25 loc) · 792 Bytes
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
SensorID = "1"
status = "CLEAR"
oldstatus = "CLEAR"
server = "192.168.1.37"
port = 8888
function open()
print("Open connection...")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(port, server)
conn:send("{ \"Type\":\"PIR\",\"SensorID\":\"".. SensorID .. "\"}")
end
function sendalarm(SensorID,status)
conn:send("{ \"Type\":\"PIR\",\"SensorID\":\"".. SensorID .. "\", \"Status\":\"".. status .."\"}")
end
function close()
conn:close()
end
open()
gpio.mode(4,gpio.INPUT,gpio.FLOAT)
tmr.alarm(0, 1000, 1, function() -- Set alarm to one second
if gpio.read(4)==1 then status="ALARM" else status="CLEAR" end
if status ~= oldstatus then sendalarm (SensorID,status) end
oldstatus = status
end)