Skip to main content
1-Visitor
September 14, 2020
Solved

[RaspberryPi Tutorial] PiTemplate.lua error

  • September 14, 2020
  • 1 reply
  • 2481 views

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 

Best answer by vcavic

Hi @VladimirRosu_116627 ,

 

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

1 reply

19-Tanzanite
September 14, 2020
Maybe because there's a ";" just before that line?
vcavic1-VisitorAuthor
1-Visitor
September 14, 2020

Hi @VladimirRosu_116627 ,

thank you for your response!

 

Still same error.

 

BR

Vladimir

19-Tanzanite
September 14, 2020

I'm no LUA expert, beware.

If you remove that line, do you still see that error?