-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.lua
More file actions
59 lines (51 loc) · 1.57 KB
/
Copy pathapplication.lua
File metadata and controls
59 lines (51 loc) · 1.57 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
-- file : application.lua
local module = {}
m = nil
-- Sends a simple ping to the broker
local function send_dht22(temp, humi)
m:publish(config.ENDPOINT .. "temp", temp,0,0)
m:publish(config.ENDPOINT .. "humidity", humi,0,0)
--print(config.ENDPOINT .. "temp" .. temp, "id=" .. config.ID,0,0)
print("Sending temp")
end
local function get_dht22()
pin = 5
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
if status == dht.OK then
send_dht22(temp, humi)
-- Float firmware using this example
--print("DHT Temperature:"..temp..";".."Humidity:"..humi)
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
end
-- Sends my id to the broker for registration
local function register_myself()
m:subscribe(config.ENDPOINT .. config.ID,0,function(conn)
print("Successfully subscribed to data endpoint")
end)
end
local function mqtt_start()
m = mqtt.Client(config.ID, 120)
-- register message callback beforehand
m:on("message", function(conn, topic, data)
if data ~= nil then
print(topic .. ": " .. data)
-- do something, we have received a message
end
end)
-- Connect to broker
m:connect(config.HOST, config.PORT, 0, 1, function(con)
register_myself()
-- And then pings each 1000 milliseconds
tmr.stop(6)
tmr.alarm(6, 60000, 1, get_dht22)
end)
end
function module.start()
mqtt_start()
print(temp)
end
return module