-
Notifications
You must be signed in to change notification settings - Fork 0
/
triple.lua
164 lines (153 loc) · 5.12 KB
/
triple.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
--------------------------------------------------------------------------------
-- Sensor measurement
--
-- setup(casovac,prefix,pinA,pinB)
-- - nastavi casovac, ktery bude knihovna pouziva a spusti mereni
-- - prefix pro identifikaci zarizeni v pripade ze vice zarizeni posila v jednom node
-- - nastavi piny pro mereni barometru, je to I2C a ja nevim ktery je ktery, resim metodou pokus omyl
-- status() - vrati zda je mereni dokonceno nebo se jeste meri
-- getvalues() - vrati pole hodnot s namerenym hodnotami
--
--------------------------------------------------------------------------------
-- Set module name as parameter of require
local modname = ...
local M = {}
_G[modname] = M
--------------------------------------------------------------------------------
-- Local used variables
--------------------------------------------------------------------------------
local SeaLevel = 512 -- Tady se musi nastavit nadmorska vyska aby tlak prepocteny na hladinu more byl spravne
local Casovac
local Data
local Finished = 0
local p,t,h,ps
local Pocet_chyb
local i2cA,i2cB
-------------------------------------------------------------------------------
-- Local used modules
--------------------------------------------------------------------------------
--local tmr = tmr
--local math = math
--local bmp085 = bmp085
--local print = print
--local Debug = Debug
-- Limited to local environment
--setfenv(1,M)
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------
local function finishTRIPLE()
t,p,h,ps = bme280.read(SeaLevel)
if (nil ~= t) and (nil ~= h) then -- kontroluji pokud by to nahodou nevratilo teplotu tak mereni neprobehlo
d = bme280.dewpoint(h, t)
-- uprava na jednotky C,hPa,%,hPa
t = t / 100 or -100
p = p / 1000
h = h / 1000
ps = ps / 1000
d = d / 100
if Debug == 1 then
print ("TR>P="..p)
print ("TR>T="..t)
print ("TR>H="..h)
end
Data["3tlak"] = p
Data["3tlak0"] = ps
Data["3teplo"] = t
Data["3vlhko"] = h
Data["3rosa"] = d
Data["3ok"] = 1 -- indikace ze senzor je ok
p,t,h,ps,d = nil,nil,nil,nil,nil
local time = (tmr.now() - ((TimeStartLast or 0) * 1000))
if time <= 0 then time = 1 end
if Debug == 1 then
print ("TR>end1")
end
Finished = time -- ukonci mereni a da echo odesilaci a tim konci tento proces
time = nil
else
-- mereni nevratilo teplotu nebo vlhkost
if Pocet_chyb < 5 then
if Debug == 1 then
print ("TR>opakuji vycteni #"..Pocet_chyb)
end
i2c.setup(0, i2cA, i2cB, i2c.SLOW)
local result = bme280.setup() -- inicializace senzoru, standardni parametry
if result == nil or result ~= 2 then -- je to spravny senzor a je pripojen
if Debug == 1 then
print ("TR>init fail")
if result ~= nil then
print("TR>r="..result)
end
end
end
result = nil
Pocet_chyb = Pocet_chyb + 1
tmr.alarm(Casovac, 170+10*Pocet_chyb, 0, function() finishTRIPLE() end) -- znova volam vycteni
else
if Debug == 1 then
print ("TR>chyba vycteni")
end
Data["3ok"] = 0 -- indikace ze senzor nefunguje
local time = (tmr.now() - ((TimeStartLast or 0) * 1000))
if time <= 0 then time = 1 end
if Debug == 1 then
print ("TR>end2")
end
Finished = time -- ukonci mereni a da echo odesilaci a tim konci tento proces
time = nil
end
end
end
local function setup(_casovac,_baroA,_baroB)
Casovac = _casovac or 5
i2cA = _baroA
i2cB = _baroB
Data = {}
Finished = 0
-- startuji mereni
if _baroA ~= nil and _baroB ~= nil then
if Debug == 1 then
print ("TR>start")
end
-- stara verze initu, funguje a krici warovani
--local result = bme280.init(_baroA,_baroB) -- inicializace senzoru, standardni parametry
-- nova verze initue
i2c.setup(0, i2cA, i2cB, i2c.SLOW)
local result = bme280.setup() -- inicializace senzoru, standardni parametry
-- init konec
if result ~= nil and result == 2 then -- je to spravny senzor a je pripojen
Pocet_chyb = 0
tmr.alarm(Casovac, 170, 0, function() finishTRIPLE() end) -- volam to pres casovac tak aby se vycteni hodnoty zpozdilo od initu
else
if Debug == 1 then
print ("TR>fail")
if result ~= nil then
print("TR>r="..result)
end
end
Data["3ok"] = 0 -- indikace ze senzor nefunguje
if Debug == 1 then
print ("TR>end3")
end
Finished = 1 -- cas jedna ale dokonceno, pri chybe to tahle nastavim
end
result = nil
end
return Casovac
end
M.setup = setup
local function status()
return Finished
end
M.status = status
local function getvalues()
if Finished > 0 then
return Data
else
return {}
end
end
M.getvalues = getvalues
-- Return module table
return M