Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Services are implemented as Lua functions. In our Lua script, Services are divided into two pieces. The first is the Service definition which consists of a Service name, inputs and output. The second part of defining a Service is the service code. The Service code is run when you execute the service.
Specify your output type but not the name because the name of every output from a ThingWorx service is always result.
serviceDefinitions.GetSystemProperties( output { baseType="BOOLEAN", description="" }, description { "updates properties" } )
NOTE: This service has no input parameters and an output that results in True if the properties were successfully updated on Thingworx.
The Service code is run when you execute the Service. Functions in Lua are variables therefore to define the Service code, you will create a variable. The name of the Service has to match the name you specified in the Service definition.
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%(..%)=(%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
cntrl x
1. Navigate from installation directory to microserver directory.
cd microserver
2. Ensure that wsems is running in a separate terminal session before you start running LuaScriptResource.
3. Ensure that you have ownership to the executable luaScriptResource and executable privileges. To check ownership:
Ls -la -rwxrwxr-x 1 pi pi 769058 Jun 9 17:46 luaScriptResourc
NOTE: The owner of luaScriptResource should be the user you are logged in as on the Raspberry Pi.
4. Confirm you have executable privileges by running the following command:
sudo chmod 775 luaScriptResource
5. Run LuaScriptResource by executing the following command:
sudo ./luaScriptResource
6. The output will show an error until we create the corresponding Thing in the next step.
Now we need to register a Thing so your Raspberry Pi can bind to its Properties on the Thingworx Platform.
NOTE: The system properties from your Raspberry Pi are now being passed to the server every 30 seconds. Wait a couple of cycles to see if the values from the Raspberry Pi change. If you are impatient, manually change the value of the property using the Set button in the Composer then click Refresh to see the updated value. The value will be temporarily updated for about 30 seconds until the Raspberry Pi reports the current live value.
Tip #1 If the properties are not updating, try to stop and start both the wsems and luaScriptResource services.
quit sudo ./wsems or ./luaScriptResource
Tip #2 If a wsems and/or luaScriptResource is not shut down gracefully, sometimes the service is still running which can cause issues. You can search and kill any wsems/luaScriptResource services by using the following command. Re-run the GetSystemProperties to test if this fixed the issue.
ps -efl kill -9 <id#>
In order to demonstrate how ThingWorx can render a visualization of data from connected devices, at this point in the lesson you will import a pre-configured Mashup.
TIP: You will need to allow pop-ups in your browser for the Mashup to be displayed.
Click here to view Part 4 of this guide.