-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweather_sensor.lua
80 lines (62 loc) · 1.73 KB
/
weather_sensor.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
local weather_sensor = {}
local BME280_installed = true
local using_BME280 = false
function readDHT(dhtPin, callback)
status, temp, humidity, temp_decimial, humi_decimial = dht.read(dhtPin)
print("dht read status=" .. status .. ", temp=" .. temp .. ", hum=" .. humidity)
if (status == dht.OK) then
callback(temp, humidity)
end
end
weather_sensor.init = function()
result = bme280.setup() -- 2 if sensor is BME280, 1 if sensor is BMP280
print(result)
if (result == nil) then
BME280_installed = false
elseif (result == 2) then
using_BME280 = true
end
end
weather_sensor.read = function(dhtPin, alt, callback)
local inTemp = "?"
local inHum = "?"
local inPress = "?"
if (not BME280_installed) then
readDHT(dhtPin, function(temp, humidity)
if (temp <= 100) then
inTemp = temp
else
inTemp = temp / 25.6
end
if (humidity <= 100) then
inHum = humidity
else
inHum = humidity / 25.6
end
callback(inTemp, inHum, inPress)
end)
else
T, P, H, QNH = bme280.read(alt)
local Tsgn = 1
if (T < 0) then
Tsgn = -1
end
T = Tsgn*T
inTemp = string.format("%s%d.%.1d", Tsgn<0 and "-" or "", T/100, T%100)
inPress = string.format("%d.%.1d", QNH/1000, QNH%1000)
if (using_BME280) then
inHum = string.format("%d.%.1d", H/1000, H%1000)
callback(inTemp, inHum, inPress)
else -- BMP280 has no humidity data
readDHT(dhtPin, function(temp, humidity)
if (humidity <= 100) then
inHum = humidity
else
inHum = humidity / 25.6
end
callback(inTemp, inHum, inPress)
end)
end
end
end
return weather_sensor