Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Hi,
I have an error in the Template.lue script.
Error msg
[INFO ] 2020-09-14 08:32:57,138 PiThing: Attempting to GetPropertySubscriptions from server failed. code: 504, result:
[ERROR] 2020-09-14 08:32:57,167 luaScriptProxy::execute: [-1242565552] Error executing script PiThing, Error = Error: ...oads/microserver/etc/custom/templates/PiTemplate.lua:32: attempt to perform arithmetic on global 'sf' (a nil value)
[ERROR] 2020-09-14 08:32:57,168 luaScriptProxy::app_unlock: [-1242565552] Attempt to unlock app_mutex while not holding it.L= 0x16f3c38
PiTemplate.lua error line BOLD
module ("templates.PiTemplate", thingworx.template.extend)
properties.cpu_temperature={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.cpu_freq={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.cpu_volt={baseType="NUMBER", pushType="ALWAYS", value=0}
serviceDefinitions.GetSystemProperties(
output { baseType="BOOLEAN", description="" },
description { "updates properties" }
)
services.GetSystemProperties = function(me, headers, query, data)
log.trace("[PiTemplate]","########### in GetSystemProperties#############")
queryHardware()
-- if properties are successfully updated, return HTTP 200 code with a true service return value
return 200, true
end
function queryHardware()
-- use the vcgencmd shell command to get raspberry pi system values and assign to variables
-- measure_temp returns value in Celsius
-- measure_clock arm returns value in Hertz
-- measure_volts returns balue in Volts
local tempCmd = io.popen("vcgencmd measure_temp")
local freqCmd = io.popen("vcgencmd measure_clock arm")
local voltCmd = io.popen("vcgencmd measure_volts core")
-- set property temperature
local s = tempCmd:read("*a")
s = string.match(s,"temp=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("temp %.1f",s))
properties.cpu_temperature.value = s
-- set property frequency
s = freqCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw freq %s",s))
s = string.match(s,"frequency%(45%)=(%d+)");
s = s/1000000
log.debug("[PiTemplate]",string.format("scaled freq %d",s))
properties.cpu_freq.value = s
-- set property volts
s = voltCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw volts %s", s))
s = string.match(s,"volt=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("scaled volts %.1f", s))
properties.cpu_volt.value = s
end
tasks.refreshProperties = function(me)
log.trace("[PiTemplate]","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In tasks.refreshProperties~~~~~~~~~~~~~ ")
queryHardware()
end
Any suggestion?
Thank you!
BR
Vladimir
Solved! Go to Solution.
Hi @VladimirRosu ,
the error is small but fatal
i found solution (your colleague)
Changing this line:
s = string.match(s,"frequency%(45%)=(%d+)");
to:
s = string.match(s,"frequency%(..%)=(%d+)")
BR
Vladimir
I'm no LUA expert, beware.
If you remove that line, do you still see that error?
Hi @VladimirRosu ,
If I delete the entire part of the frequency properties, don't have any error, but that is not the solution
Maybe the tutorial has not been updated for the new version raspberry pi 4.
Anyway Thank you!
BR
Vladimir
Of course, I didn't suggest deleting the entire part, just the line that causes the issue.
I should explain better: this is my typical debugging practice: I try to isolate the problem first to a service, then to a line of code (that's the part I was suggesting above).
Then in that line I try to write to console the type of variable - given by type(s) in your case, and add the value of that variable just before the specific line. That's what I would do in your case.
Note: there is a small link called Need Help ? under the Community (that points to TWX-DevZoneHelp@ptc.com ) at the bottom of that page. Could you maybe also send this issue to that email address?
Hi @VladimirRosu ,
the error is small but fatal
i found solution (your colleague)
Changing this line:
s = string.match(s,"frequency%(45%)=(%d+)");
to:
s = string.match(s,"frequency%(..%)=(%d+)")
BR
Vladimir