-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbme280.lua
More file actions
84 lines (71 loc) · 2.38 KB
/
bme280.lua
File metadata and controls
84 lines (71 loc) · 2.38 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
sda = 3 -- wireing BME-280
scl = 4
Broker = "mqtt.eclipse.org"
port = 1883
local wireisgood
wireisgood = bme280.init(sda, scl) -- return 2 at BME, 1 - BMP, else nil
m = mqtt.Client( "bme280280", 120, "bme280", "superpass")
pu = false -- is wifi and broker connected
m:lwt("/myhome/lwt", "bme280", 0, 0)
connectNow = function()
local tm = tmr.create()
tm:alarm(5000, 1, function() get() end)
function get()
if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then
tmr.stop(tm)
tmr.unregister(tm)
print("Got WiFi!")
m:connect(Broker, port, 0, function(conn)
print("Mqtt Connected to: " .. Broker)
pu = true
end)
else
print("No WiFi!")
end
end
get()
end
m:on("offline", function(con)
pu = false
print ("Mqtt Reconnecting.")
connectNow()
end)
function publish_data()
print("==========\t\t\tHeap at Start "..node.heap())
local H = string.format("%.1f", (bme280.humi()/1000))
local P, T = bme280.baro()
P = string.format("%.1f", (P/1000*0.75))
T = string.format("%.2f", T/100)
print("Humidity = "..H.." %, \nPressure = "
..P.." mm.Hg"
.."\nTemperatre = "..T.." C")
if pu == true then
m:publish("/myhome/bme280/temperature/status",T,0,0, function(conn)
print("Temp "..T.." published!")
tmr.create():alarm(1000, 0, function(conn)
m:publish("/myhome/bme280/humi/status",H,0,0, function(conn)
print("Humi "..H.." published!")
tmr.create():alarm(1000, 0, function(conn)
m:publish("/myhome/bme280/press/status",H,0,0, function(conn)
print("Pressure "..P.." published!")
print("==========\t\t\tHeap at Finish "..node.heap())
end)
end)
end)
end)
end)
collectgarbage()
end
end
function run_main_prog()
print("Main program")
publish_data()
tmr.create():alarm(30000, 1, function() publish_data() end)
end
if wireisgood == 2 then
connectNow()
run_main_prog()
else
print("\n\n===========\t\t Error at Wireing or not BME, Balbes! ===========")
end
--end