WeatherStation: slight update

- Replaced the use of string.find by string.match, as it was not
   working as expected with the new lua interpretor.

 - Slight simplification of extract_value as we now can use a full-blown
   regular expression.

Change-Id: I1d7df66a272120cd10aa40f8e9326057b9709e3d
This commit is contained in:
Lionel Sambuc 2014-05-28 17:14:21 +02:00
parent ecad34966a
commit efa169276f

17
share/beaglebone/weather/weatherstation.lua Normal file → Executable file
View file

@ -80,13 +80,14 @@ function read_file(filename)
end end
-- Extract the value of the LABEL:VALUE pairs read from the device files. -- Extract the value of the LABEL:VALUE pairs read from the device files.
function extract_value(label, data, pat) function extract_value(data, pat)
local x, y, z = string.find(data, label .. "%s+: (" .. pat .. ")") local x = string.match(data, pat)
if x == nil or y == nil or z == nil then
if x == nil then
return "0" return "0"
end end
return z return x
end end
-- Read the sensor values and generate json output -- Read the sensor values and generate json output
@ -97,20 +98,20 @@ function generate_json()
if tsl2550 == nil then if tsl2550 == nil then
return nil return nil
end end
local illuminance = extract_value("ILLUMINANCE", tsl2550, "%d+") local illuminance = extract_value(tsl2550, "ILLUMINANCE: (%d+)")
local sht21 = read_file("/dev/sht21b3s40") local sht21 = read_file("/dev/sht21b3s40")
if sht21 == nil then if sht21 == nil then
return nil return nil
end end
local temperature = extract_value("TEMPERATURE", sht21, "%d+.%d+") local temperature = extract_value(sht21, "TEMPERATURE: (%d+.%d+)")
local humidity = extract_value("HUMIDITY", sht21, "%d+.%d+") local humidity = extract_value(sht21, "HUMIDITY: (%d+.%d+)")
local bmp085 = read_file("/dev/bmp085b3s77") local bmp085 = read_file("/dev/bmp085b3s77")
if bmp085 == nil then if bmp085 == nil then
return nil return nil
end end
local pressure = extract_value("PRESSURE", bmp085, "%d+") local pressure = extract_value(bmp085, "PRESSURE: (%d+)")
json = json .. "{\n" json = json .. "{\n"
json = json .. "\"temperature\": " .. temperature .. ",\n" json = json .. "\"temperature\": " .. temperature .. ",\n"