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 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

Connect Raspberry Pi to ThingWorx Part 3

No ratings

 

Step 8: Configure Template File (Service)

 

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.

 

Create Service Definition

  1. Open the PiTemplate.lua file.
  2. Append the service definition to the file.
  3. Create a service named GetSystemProperties that gets the system properties (cpu temperature, clock frequencies, voltages) from your Raspberry Pi and updates the respective properties on the Thingworx platform.
  4. 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.

 

Create Service code

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.

 

  1. Copy the service code below with comments explaining the logic and add append it to your template file, or download and unzip the full PiTemplate.zip attached here.
    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
    
  2. Save the PiTemplate.lua file.
     cntrl x
    

 

 

Step 9: Run LSR

 

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
lsr9.png

 

6. The output will show an error until we create the corresponding Thing in the next step.

 

 

Step 10: Bind Remote Thing Properties

 

Now we need to register a Thing so your Raspberry Pi can bind to its Properties on the Thingworx Platform.

 

  1. Create a Thing named PiThing that will bind the scripts.PiThing created in config.lua .
    • Open your Composer screen.
    • Click Things on the left-navigation and the + symbol.
    • Enter PiThing in the Name field and click RemoteThing in the Thing Template field.
       

      CreatePiThing.png

    • Click Save.
  2. Ensure that the Remote Thing Property is connected.
    • Click Properties in the left-hand navigation.
    • Verify that the isConnected Property has a value of true. This means that your Raspberry Pi is still connected and now bound to this Thing on Thingworx Platform.
       
      PiThingIsConnected.png
  3. Bind the remote Thing Properties.
    • Make sure the Properties tab is selected and click Edit at the top of the PiThing.
    • Click Manage Bindings.
    • Select the Remote tab at the top.
       

      PiThingAddRemoteProperties.png

    • Click Add All Above Properties or drag and drop the ones you need.
    • Click Done.
    • Click Save.
  4. Verify that the Properties were updated with readings from the Raspberry Pi.
    • Both the Value and Default Value for the three Properties will be set to the current reading from the Raspberry Pi.
    • Cover the Raspberry Pi and wait about a minute, then Select the Properties tab and click Refresh. You will see the cpu_temperature value change.
       
      PiThingVerifyPropertiesChanging.png 

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.

 

Troubleshooting Tips

 

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#>

 

Step 11: View Data from Devices

 

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.

 

  1. On the ThingWorx server that the EMS is connected to, start on the Home tab of Composer.
  2. Import a pre-built Mashup.
    • Download and save the pre-built Mashup XML file attached here: Mashups_PiThingMashup-v91.xml.
    • In Composer, click the Import/Export drop-down at the bottom-left.
       

      foundation_QS_uploadSimulator.png

    • Click Import.
    • Leave all default values and click Browse to select the Mashups_PiThingMashup-v91.xml file that you just downloaded.
    • Click Open, then Import, and once you see the success message, click Close.
  3. View Mashup displaying live data.
    • Select the home icon in the top left side of Composer, then click Mashups on the left-navigation panel.
    • Click Mashups_PiThingMashup-v91 and you'll see the design view of the Mashup.
       

      open-imported-mashup.png

    • Click View Mashup, and you'll see the live Mashup.
       
      view-mashup.png

 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. 

Version history
Last update:
‎Mar 07, 2023 02:40 PM
Updated by:
Labels (2)
Attachments
Contributors