cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Error in my PiTemplate with implementated lua packages while running luaScriptResource

Moritz_P
3-Visitor

Error in my PiTemplate with implementated lua packages while running luaScriptResource

I used the guides "Connect Raspberry Pi to ThingWorx" and "Setup a Raspberry Pi as an IoT Device".

After setting up the EMS and connecting to TW i wanted to collect data with the Raspi UART pins / or UDP(same problem) so i could use the data for my property values.

For this i used the lua package manager luarocks to install the modules 'periphery' and 'socket':

 

sudo apt-get update

sudo apt-get install luarocks --package manager for Lua modules

sudo luarocks install luasocket  (Web-Socket for UDP/TCPIP)

sudo luarocks install lua-periphery (Raspi GPIO access)

 

The data transfer script i created works fine on its own but when i want to implement it in my PiTemplate i get an error.

 

 

At first the script of the PiThing:

The serial connection starts at --Sensor Values; UART    and ends with serial:close

 

 

require "shapes.swupdate"
module ("templates.200903_Paeslack_SimKI_TT", 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}
 
properties.Data1={baseType="NUMBER", pushType="ALWAYS", value=0}
properties.Data2={baseType="NUMBER", pushType="ALWAYS", value=0}
 
 
serviceDefinitions.GetSystemProperties(
    output { baseType="BOOLEAN", description="" },
    description { "updates properties" }
)
services.GetSystemProperties = function(me, headers, query, data)
log.trace("[200903_Paeslack_SimKI_TT]","########### 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")
 
 
--Sensor Values; UART    
 

local periphery = require('periphery')
local Serial = periphery.Serial

 

-- Open /dev/ttyUSB0 with baudrate 115200, and defaults of 8N1, no flow control
local serial = Serial("/dev/serial0", 115200)

 

while true do
local buf = serial:read(26, 500)
--print(string.format("read %d bytes: _%s_", #buf, buf))

local Speedgoat_Input_String = tostring(buf)

print(buf)


end
serial:close()

 
   
    --  set property temperature
         local s = tempCmd:read("*a")
         s = string.match(s,"temp=(%d+\.%d+)");
         log.debug("[200903_Paeslack_SimKI_TT]",string.format("temp %.1f",s))
         properties.cpu_temperature.value = s
         
    --  set property frequency
         s = freqCmd:read("*a")
         log.debug("[200903_Paeslack_SimKI_TT]",string.format("raw freq %s",s))
         s = string.match(s,"frequency%(45%)=(%d+)");
         s = s/1000000
         log.debug("[200903_Paeslack_SimKI_TT]",string.format("scaled freq %d",s))
         properties.cpu_freq.value = s
         
    --  set property volts
         s = voltCmd:read("*a")
         log.debug("[200903_Paeslack_SimKI_TT]",string.format("raw volts %s", s))
         s = string.match(s,"volt=(%d+\.%d+)");
         log.debug("[200903_Paeslack_SimKI_TT]",string.format("scaled volts %.1f", s))
         properties.cpu_volt.value = s
 
end
tasks.refreshProperties = function(me)
    log.trace("[200903_Paeslack_SimKI_TT]","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In tasks.refreshProperties~~~~~~~~~~~~~ ")
queryHardware()
end
 
I skipped the data preprocessing because it changes nothing on the problem.
The task is quite simple. I want to read 26 bytes with 500 ms break, like seen in the script.
As i said it works well when i execute it on its own but when i want to use the UART-connection script in the Template this Error appears and i don't get any values printed:
 
[ERROR] 2020-11-26 13:53:58,331 luaScriptProxy::execute: [1979708496] Error executing script PiThing, Error = Error: error loading module 'periphery' from file '/usr/local/lib/lua/5.1/periphery.so':
/usr/local/lib/lua/5.1/periphery.so: undefined symbol: lua_gettop
[ERROR] 2020-11-26 13:53:58,331 luaScriptProxy::app_unlock: [1979708496] Attempt to unlock app_mutex while not holding it.L= 0x2f59b8
 
I assume that the library and syntax was not updated correctly.
Is it a problem for the PiTemplate / luaScriptResource that i installed the lua packages globally and not locally? 
Does anybody has experience with such problems?
 
I appreciate any help and if anybody knows how to patch the lua path for the packages it would be great.
 
THX
1 REPLY 1
slangley
23-Emerald II
(To:Moritz_P)

Hi @Moritz_P.

 

Have you tried using the library locally?

 

Regards.

 

--Sharon

Top Tags