-
Notifications
You must be signed in to change notification settings - Fork 6
/
mqttpir.lua
63 lines (56 loc) · 1.47 KB
/
mqttpir.lua
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
pir = 1
dht22 = 2
mqttHost = '192.168.100.122'
chipId = node.chipid()
gpio.mode(pir,gpio.INT)
mqttClient = mqtt.Client(chipId, 5)
mqttClient:lwt("events/esp8266/".. chipId .."/status",
cjson.encode({
type = "status",
sensorId = chipId,
status = 0
}), 0, 0)
mqttClient:on("offline", function()
if DEBUG then
print ("Connecting to the broker ...")
end
connect()
end)
local function connect()
mqttClient:connect(mqttHost, 1883, 0, function(conn)
mqttClient:publish("events/esp8266/".. chipId .."/status",
cjson.encode({
type = "status",
sensorId = chipId,
status = 1,
heap = node.heap()
}), 0, 0)
if DEBUG then
print("connected")
end
end)
end
local function sendSensors()
local status, temperature, humidity, temperatureDecimial, humidityDecimial = dht.read(dht22)
if status == 0 then
if DEBUG then
print("temperature: "..temperature.." deg C")
print("humidity: "..humidity.." %")
print("Sendind temperature and humidity")
end
mqttClient:publish("events/esp8266/".. chipId .."/sensors",
cjson.encode({
sensorId = chipId,
type = "report",
temperature = temperature,
temperatureDecimal = temperatureDecimial,
humidity = humidity ,
humidityDecimal = humidityDecimial,
alarm = gpio.read(pir),
heap = node.heap()
}) ,0,0)
end
end
connect()
gpio.trig(pir,"both", sendSensors)
tmr.alarm(2, 10000, 1, sendSensors)