Skip to main content
7-Bedrock
February 25, 2019
Solved

Connection Raspberry Pi to Thingworx (LUA stuck at creating new Handler)

Hello,

I'm fairly new to Thingworx and trying to get some first steps done by connecting a Pi to Thingworx, following the

Raspberry Quickstart guide. (https://developer.thingworx.com/en/resources/guides/thingworx-raspberry-pi-quickstart/)

EMS is 5.4

I'm using a own thingworx installation.

First my config.json:

{
 "ws_servers": [{
 "host": "twxdemo.inneo.cloud",
 "port": 443
 }],
 "http_server": {
 "host": "127.0.0.1",
 "port": 8080,
	"ssl":false,
	"authenticate":false
	
	
 
 },
 "appKey": "xxx",
 "logger": {
 "level":	"INFO"
 },
 
 "auto_bind": [{
 "name": "PiThing",
 "gateway": false,
		 "host": "127.0.0.1",
 "port": 8001
 }],
 

 "certificates": {
 "validate": false,
 "allow_self_signed":true
 },
 "ws_connection": {
 "encryption": "ssl",
 "verbose": true,
 "msg_timeout": 1000
 }
}

It does connect:

pi@raspberrypi:~/microserver $ sudo ./wsems
[FORCE] 2019-02-25 12:25:10,253 ./wsems: Initializing EMS ....
[FORCE] 2019-02-25 12:25:10,253 main: Using config file /home/pi/microserver/etc/config.json
[INFO ] 2019-02-25 12:25:10,254 ./wsems: Creating the WsEms proxy.
[INFO ] 2019-02-25 12:25:10,254 ./wsems: Starting HTTP Server.
[INFO ] 2019-02-25 12:25:10,254 Main: Using custom certificate and private key for HTTP Server
[WARN ] 2019-02-25 12:25:10,254 Main: Encryption is disabled on HTTP Server.
[WARN ] 2019-02-25 12:25:10,254 Main: Authentication is disabled on the HTTP Server.
[INFO ] 2019-02-25 12:25:10,255 ./wsems: Initializing the ThingWorx REST interface.
[FORCE] 2019-02-25 12:25:10,255 httpServer: starting http server port=8080
[INFO ] 2019-02-25 12:25:10,255 wsEmsProxy::initialize: FIPS is disabled.
[INFO ] 2019-02-25 12:25:10,255 wsEmsProxy::initialize: Encryption is enabled on Web Socket connection.
[WARN ] 2019-02-25 12:25:10,257 SDK: SDK Version: 2.0.4
[WARN ] 2019-02-25 12:25:10,257 SDK: TLS Library: OpenSSL
[WARN ] 2019-02-25 12:25:10,257 SDK: TLS Library Version: 1.0.2l
[WARN ] 2019-02-25 12:25:10,257 SDK: FIPS Capable
[INFO ] 2019-02-25 12:25:10,277 WsProxy::initialize: EMS Version 5.4.0.114
[INFO ] 2019-02-25 12:25:10,277 WsProxy::initialize: twApi singleton initialized
[WARN ] 2019-02-25 12:25:10,278 WsProxy::initialize: Certificate validation is disabled.
[WARN ] 2019-02-25 12:25:10,278 WsProxy::initialize: Self signed certificates are enabled.
[ERROR] 2019-02-25 12:25:10,279 SDK: twMap_Add: parse function returned null.
[INFO ] 2019-02-25 12:25:10,279 wsEmsProxy::initialize: Initialization complete!
[INFO ] 2019-02-25 12:25:10,281 ./wsems: Starting the connection.

twxdemo.inneo.cloud:443-->[INFO ] 2019-02-25 12:25:10,356 SDK: twWs_Connect: Websocket connected!
[WARN ] 2019-02-25 12:25:10,478 SDK: twBindBody_Delete: NULL body or stream pointer
[INFO ] 2019-02-25 12:25:10,482 Main: Succesfully connected. Saving .booted config file

However runing the LUA script does result into it beeing stuck at crating a new handler(EMS does run):

pi@raspberrypi:~/microserver $ sudo ./luaScriptResource
[INFO ] 2019-02-25 12:24:34,835 ./luaScriptResource: Using config file: /home/pi/microserver/etc/config.lua
[FORCE] 2019-02-25 12:24:34,835 ./luaScriptResource: Starting up ....

[INFO ] 2019-02-25 12:24:34,840 luaScriptResource:main: Encryption is disabled on the RAP connection.
[INFO ] 2019-02-25 12:24:34,840 luaScriptResource:main: FIPS is disabled.
[INFO ] 2019-02-25 12:24:34,843 ./luaScriptResource: New path is /home/pi/microserver/etc/thingworx/clibs:/home/pi/microserver/etc/custom/clibs:/home/pi/microserver/etc/community/clibs:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[WARN ] 2019-02-25 12:24:34,846 ScriptResource::initialize: Encryption is disabled on HTTP Server.
[WARN ] 2019-02-25 12:24:34,846 ScriptResource::initialize: Authentication is disabled on the HTTP Server.
[FORCE] 2019-02-25 12:24:34,876 httpServer: starting http server port=8001
[INFO ] 2019-02-25 12:24:34,893 thingworx.shape: Creating a new shape.
[INFO ] 2019-02-25 12:24:34,895 PiThing: -- Configuration -------------------------------
[INFO ] 2019-02-25 12:24:34,895 PiThing: scanRate: 1000
[INFO ] 2019-02-25 12:24:34,895 PiThing: scanRateResolution: 500
[INFO ] 2019-02-25 12:24:34,896 PiThing: taskRate: 30000
[INFO ] 2019-02-25 12:24:34,896 PiThing: keepAliveRate: 60000
[INFO ] 2019-02-25 12:24:34,896 PiThing: requestTimeout: 15000
[INFO ] 2019-02-25 12:24:34,896 PiThing: registerRate: 43200000
[INFO ] 2019-02-25 12:24:34,896 PiThing: register: true
[INFO ] 2019-02-25 12:24:34,896 PiThing: getPropertySubscriptionOnReconnect: false
[INFO ] 2019-02-25 12:24:34,897 PiThing: maxConcurrentPropertyUpdates: 100
[INFO ] 2019-02-25 12:24:34,897 PiThing: defaultPushType: VALUE
[INFO ] 2019-02-25 12:24:34,897 PiThing: useShapes: true
[INFO ] 2019-02-25 12:24:34,897 PiThing: identifier: Not Specififed
[INFO ] 2019-02-25 12:24:34,897 PiThing: ------------------------------------------------
[INFO ] 2019-02-25 12:24:34,898 thingworx.template: Adding shape 'shapes.metadata' to template 'thingworx.template'
[INFO ] 2019-02-25 12:24:34,899 thingworx.template: Adding shape 'shapes.propsubscribe' to template 'thingworx.template'
[INFO ] 2019-02-25 12:24:34,899 thingworx.template: Creating new templates.PiTemplate named 'PiThing'
[INFO ] 2019-02-25 12:24:34,899 PiThing: -- Initializing properties ---------------------
[INFO ] 2019-02-25 12:24:34,899 PiThing: Initialized property cpu_temperature [baseType: number, pushType: ALWAYS, handler: nil, value: 0]
[INFO ] 2019-02-25 12:24:34,899 PiThing: Initialized property temp [baseType: NUMBER, pushType: ALWAYS, handler: nil, value: 0]
[INFO ] 2019-02-25 12:24:34,900 PiThing: Initialized property humidity [baseType: NUMBER, pushType: ALWAYS, handler: nil, value: 0]
[INFO ] 2019-02-25 12:24:34,900 PiThing: Initialized property cpu_volt [baseType: NUMBER, pushType: ALWAYS, handler: nil, value: 0]
[INFO ] 2019-02-25 12:24:34,900 PiThing: Initialized property upToDate [baseType: BOOLEAN, pushType: NEVER, handler: nil, value: true]
[INFO ] 2019-02-25 12:24:34,900 PiThing: Initialized property cpu_freq [baseType: NUMBER, pushType: ALWAYS, handler: nil, value: 0]
[INFO ] 2019-02-25 12:24:34,900 PiThing: ------------------------------------------------
[INFO ] 2019-02-25 12:24:36,870 PiThing: -- Starting script --------------------------
[INFO ] 2019-02-25 12:24:36,870 PiThing: Registering core callback handler
[INFO ] 2019-02-25 12:24:36,870 PiThing: Starting main loop
[INFO ] 2019-02-25 12:24:36,870 PiThing: Calling lifecycle start listeners.
[INFO ] 2019-02-25 12:24:36,871 shapes.propsubscribe: Initialized
[INFO ] 2019-02-25 12:24:36,872 thingworx.handler: Creating a new handler.

In Thingworx the PiThing is connected but without the Properties like cpu_temperature e.g.

here config.lua file

 scripts.log_level = "INFO"
 scripts.script_resource_ssl = false
 scripts.script_resource_authenticate = false
 scripts.PiThing = {
 file = "thing.lua",
 template = "PiTemplate",
 scanRate = 1000,
 taskRate = 30000
 }

 scripts.rap_host = "127.0.0.1"
 scripts.rap_port = 8080
 scripts.rap_ssl = false

scripts.script_resource_host = "127.0.0.1"

and the edited PiTemplate.lua

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 }
properties.temp = { baseType="NUMBER", pushType="ALWAYS", value=0 }
properties.humidity = { baseType="NUMBER", pushType="ALWAYS", value=0 }

serviceDefinitions.GetSystemProperties(
 output { baseType="BOOLEAN", description="" },
 description { "updates properties" }
)
-- service input parameters
-- me: table that refers to the Thing
-- headers: table of HTTP headers
-- query: query parameters from the HTTP request
-- data: lua table containing the parameters to the service call.
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")
 local sensorCmd = io.popen("./Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4")
-- 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
-- set property temp and humidity
 local sensor = sensorCmd:read("*a")
 log.debug("[PiTemplate]",string.format("raw sensor %s", sensor))
 s = string.match(sensor,"Temp=(%d+\.%d+)");
 log.debug("[PiTemplate]",string.format("scaled temp %.1f", s))
 properties.temp.value = s
 s = string.match(sensor,"Humidity=(%d+\.%d+)");
 log.debug("[PiTemplate]",string.format("scaled humidity %.1f", s))
 properties.humidity.value = s
end
tasks.refreshProperties = function(me)
 log.trace("[PiTemplate]","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In tasks.refreshProperties~~~~~~~~~~~~~ ")
queryHardware()
end

did I miss sth. at the PiTemplate.lua?

 

Any help appreciated!

 

MHaas1Answer
7-Bedrock
March 1, 2019

Solution:

Step1:

Update the EMS to 5.4.1 or higher.

link in Guide refers to 5.4.0 which causes the Problem.

just Download the latest Version from PTC.

 

Step 2:

in the PiTemplate.Lua

is a Typo:

module ("templates.PiTemplate", thingworx.template.extend)

properties.cpu_temperature = {baseType="number", pushType="ALWAYS", value=0 }
properties.cpu_freq = {baseType="NUMBER", pushType="ALWAYS",value=0 }

it needs to be changed into NUMBER.

 

3 replies

Support
February 25, 2019

Hi @MHaas1.

 

Did you set up the bindings in ThingWorx (step 10 from the instructions)?  Until you have bound the entity/properties with the corresponding ones from your remote thing, you won't see anything in ThingWorx.

 

Regards.

 

--Sharon

 

Translate
MHaas17-BedrockAuthor
7-Bedrock
February 26, 2019

Hi Sharon,

Thats where i get stuck. Below a Screenshot from "Manage Bindings" Button.

after some time the "Service GetRemoteMetadata has timed out" Message appears.

 

Regards

Moritz

*Edit1: Screenshot

 

Manage Bindings.PNG

 

Translate
MHaas17-BedrockAuthorAnswer
7-Bedrock
March 1, 2019

Solution:

Step1:

Update the EMS to 5.4.1 or higher.

link in Guide refers to 5.4.0 which causes the Problem.

just Download the latest Version from PTC.

 

Step 2:

in the PiTemplate.Lua

is a Typo:

module ("templates.PiTemplate", thingworx.template.extend)

properties.cpu_temperature = {baseType="number", pushType="ALWAYS", value=0 }
properties.cpu_freq = {baseType="NUMBER", pushType="ALWAYS",value=0 }

it needs to be changed into NUMBER.

 

Translate