I have 2 DS18B20 1-wire temperature sensors connected to a Raspberry Pi and I have OWFS (one wire file system) running on it. I am trying to read the temperatures and have it in Vera. I managed to get it to work, but it seems to hang or crash after a while. Could you have a look at my code and see what I am doing wrong? It also seems weird that 1 sensor seems to keep working but the other crashes.
[code]<?xml version="1.0"?>
function startup(lul_device)
luup.log("Starting RaspberryPi 1-wire Bridge for DS18B20: " … tostring(lul_device))
TMP102_DEVICE = lul_device
TMP102_SLAVEADDRESS = luup.variable_get("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", TMP102_DEVICE)
if (TMP102_SLAVEADDRESS == nil) then
luup.variable_set("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", 72, TMP102_DEVICE)
TMP102_SLAVEADDRESS = "72"
end
luup.log("Scheduled: " .. luup.call_timer("refreshCache", 1, "1", "", ""))
end
function refreshCache()
luup.log("Refreshing temperature.")
-- Get temperature sensor value.
local status, content = luup.inet.wget("http://192.168.1.3:3002/text/" .. TMP102_SLAVEADDRESS .. "/temperature")
luup.log("Status:" .. status)
if (status == 0) then
recordTemperature(content)
end
luup.call_timer("refreshCache", 1, "60", "", "")
end
function recordTemperature(b)
local numbers = "%d%d.%d"
-- local fraction = string.byte(b,2)
local temp = string.sub(b, string.find(b, numbers))
-- if (whole >= 128) then
-- Negative temperature
-- temp = -256 + whole
-- else
-- temp = whole
-- end
-- temp = temp + fraction
-- Round to nearest 0.1 degree.
-- temp = (math.floor(temp * 10 + 0.5))/10
luup.log("Temperature: " .. temp)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", temp, TMP102_DEVICE)
end
startup
[/code]
The text coming out of the web page from OWFS looks like this: " 13.0625" which is why I manipulated the string.
Looking through the logs. I think this is the point where it breaks:
01 08/19/13 2:57:46.892 LuaInterface::CallFunction_Timer-5 function refreshCache failed [string “…”]:28: bad argument #2 to ‘sub’ (integer expected, got nil) <0x2df89680>
Maybe if the temperature reaches single digits, my string parsing breaks?
The web page serves up " 13.0625" including the inverted commas. I tried with a single %d but then it only came up with a single number, i.e. my temperature reading is now 1, when it should be 17.6875. If I try %d%d%d, it comes up with 312. No idea why.
Is there a simple way to pull just the numbers from the webpage response, and drop the white space and the inverted commas? I’m not doing any math on the value right now, so it might be fine to pull it as text and not integers.
Have a look at the Programming in Lua book (2nd ed.), at the section on the string library (20.3 lists the patterns and 20.4 discusses captures).
Based on an example there:
str = "\" 13.0625\""
q, quotedPart = string.match(str, "([\"'])%s*(.-)%1")
print(quotedPart)
Prints [tt]13.0625[/tt] because it will match either single or double quotes surrounding zero or more space characters, and then capture everything else up until the matching quote.
Also, try experimenting with a command-line Lua interpreter on your development computer – much less hassle than uploading a new plugin file and testing string parsing on the Vera. See downloads here: [url=http://sourceforge.net/projects/luabinaries/]http://sourceforge.net/projects/luabinaries/[/url]
Me again. I got the system working, but it is crashing with this error:
LuaInterface::CallFunction_Timer-5 function refreshCache failed [string “…”]:41: attempt to concatenate global ‘temp’ (a nil value)
I assume I need to make the temp variable a local variable, or add some error handling, but no idea how.
This is my current Implementation file:
<?xml version="1.0"?>
<implementation>
<settings>
</settings>
<functions>
function startup(lul_device)
luup.log("Starting RaspberryPi 1-wire Bridge for DS18B20: " .. tostring(lul_device))
TMP102_DEVICE = lul_device
TMP102_SLAVEADDRESS = luup.variable_get("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", TMP102_DEVICE)
if (TMP102_SLAVEADDRESS == nil) then
luup.variable_set("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", 72, TMP102_DEVICE)
TMP102_SLAVEADDRESS = "72"
end
luup.log("Scheduled: " .. luup.call_timer("refreshCache", 1, "1", "", ""))
end
function refreshCache()
luup.log("Refreshing temperature.")
-- Get temperature sensor value.
local status, content = luup.inet.wget("http://192.168.1.3:3002/text/" .. TMP102_SLAVEADDRESS .. "/fasttemp")
luup.log("Status:" .. status)
if (status == 0) then
recordTemperature(content)
end
luup.call_timer("refreshCache", 1, "60", "", "")
end
function recordTemperature(b)
q, temp = string.match(b, "([\"'])%s*(.-)%1")
luup.log("Temperature: " .. temp)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", temp, TMP102_DEVICE)
end
</functions>
<incoming>
</incoming>
<startup>startup</startup>
<actionList>
</actionList>
</implementation>
You definitely want to use local variables as much as possible.
But your pattern must be failing to match, hence no value is being assigned to [tt]temp[/tt]. If you log the value of [tt]b[/tt] in your [tt]recordTemperature[/tt] function instead of [tt]temp[/tt], and go over your pattern with the examples here: [url=http://lua-users.org/wiki/PatternsTutorial]http://lua-users.org/wiki/PatternsTutorial[/url], that ought to get you back on track.
Whether or not [tt]temp[/tt] is a local variable ought not make any difference, because you attempt to set it and print it in the same code path. It would be worthwhile to make it [tt]local[/tt] in any case.
If the [tt]string.match[/tt] is sometimes failing to parse the second capture [tt]temp[/tt] (and possibly the first capture [tt]q[/tt]), then the string you are parsing sometimes does not match the pattern. This is probably where the root cause is. Make sure you know what the b argument looks like on each call to [tt]recordTemperature[/tt], and either fix the source of [tt]b[/tt], or rework your pattern matching to accommodate it.
[quote=“rtaxerxes, post:10, topic:176556”]Thanks, I think I have fixed it with:
if (b ~= nil) then
…[/quote]
If your call to [tt]luup.inet.wget[/tt] sometimes doesn’t return content, and when it does it is always in the correct format, then your fix should work. That’s assuming it makes sense to you to sometimes not get content back.
A different approach would be to not even call [tt]recordTemperature[/tt], changing
My XML file still crashes sometimes. The error log is:
50 10/17/13 13:32:44.100 luup_log:96: Refreshing temperature. <0x2e0cb680>
50 10/17/13 13:32:44.198 luup_log:96: Status:0 <0x2e0cb680>
01 10/17/13 13:32:44.199 LuaInterface::CallFunction_Timer-5 function refreshCache failed [string “…”]:43: attempt to concatenate global ‘temp’ (a nil value) <0x2e0cb680>
I assume that means the variable is still being worked on even though no content was received. Any idea how to handle it? I have added an Else statement now, not sure if that is the solution. Current XML file is below:
<?xml version="1.0"?>
<implementation>
<settings>
</settings>
<functions>
function startup(lul_device)
luup.log("Starting RaspberryPi 1-wire Bridge for DS18B20: " .. tostring(lul_device))
TMP102_DEVICE = lul_device
TMP102_SLAVEADDRESS = luup.variable_get("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", TMP102_DEVICE)
if (TMP102_SLAVEADDRESS == nil) then
luup.variable_set("urn:rtax-com:serviceId:TMP102Temperature1", "SlaveAddress", 72, TMP102_DEVICE)
TMP102_SLAVEADDRESS = "72"
end
luup.log("Scheduled: " .. luup.call_timer("refreshCache", 1, "1", "", ""))
end
function refreshCache()
luup.log("Refreshing temperature.")
-- Get temperature sensor value.
local status, content = luup.inet.wget("http://192.168.1.3:3002/text/" .. TMP102_SLAVEADDRESS .. "/fasttemp")
luup.log("Status:" .. status)
if (status == 0 and content) then
recordTemperature(content)
else
luup.log("Bad data received.")
end
luup.call_timer("refreshCache", 1, "60", "", "")
end
function recordTemperature(b)
-- 2 local numbers = "%d+."
-- local temp = string.find(b, %d%d)
if (b ~= nil) then
q, temp = string.match(b, "([\"'])%s*(.-)%1")
end
-- 2 local temp = string.sub(b, string.find(b, numbers))
-- if (whole >= 128) then
-- Negative temperature
-- temp = -256 + whole
-- else
-- temp = whole
-- end
-- temp = temp + fraction
-- Round to nearest 0.1 degree.
-- temp = (math.floor(temp * 10 + 0.5))/10
luup.log("Temperature: " .. temp)
luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", temp, TMP102_DEVICE)
end
</functions>
<incoming>
</incoming>
<startup>startup</startup>
<actionList>
</actionList>
</implementation>
No, that’s just remnants from trying to parse the string. The XML works normally, but seems to crash once in a while when it doesn’t get info or something.
You’re trying to use temp but your string.match call is sometimes not setting the second capture. You should log the string b you are trying to parse to see why sometimes the string.match is not setting temp as a result value.
It would also be useful to always test “temp” for a nil value, no matter what. As soon as your match breaks, your program will fail because temp will be nil in this line:
luup.log("Temperature: " .. temp) -- cannot concatenate a nil value here with another string - all parts need to be strings
Something like this, where you convert from a nil to an empty string can help (if this is appropriate to the overall intent of your program):
Lua uses ‘nil’ while some other languages use ‘null’. The null referred to in the log output is bug. It should say nil. If you are writing code in different languages each day, this all gets a bit tedious and one can easily use nil or null incorrectly.
Best Home Automation shopping experience. Shop at Ezlo!