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 2

No ratings

 

Step 5: Launch the EMS

 

1. Navigate to the microserver directory.

cd ..

2. Ensure that you have ownership to the executable wsems and executable privileges. To check ownership.

Ls -la
    -rwxrwxr-x 1 pi pi  769058 Jun  9 17:46 wsems

NOTE: The account that owns the wsems executable should be used to log into the Raspberry Pi.

 

3. Confirm that you have executable privileges by run the following command:

sudo chmod 775 wsems

4. Run the EMS.

sudo ./wsems

5. Validate that your EMS successfully connected.

549start.png

 

  • Depending on your logger level, your wsems execution should indicate Websocket connected in the log and the following INFO message:
[INFO ] 2016-10-11 14:22:54,770 Main: Succesfully connected.  Saving .booted config file

 

Troubleshoot Connectivity Issues

 

If the websocket does not connect successfully, check the following:

 

 Issue                                               Solution

WEBSOCKET CLOSED - Warning seen immediately after Websocket gets connected.Ensure that the host IP address, port and appKey of the ThingWorx composer instance are accurately set. If in the config.json you have selected the option to validate certification, then make sure the path to the certificate file is correctly set.
twWs_Connect - Error trying to connect.Ensure that the host IP address, port running the ThingWorx Composer is accurately set. Check if the certification parameter is set or not. By default the WS EMS validates certificates. To ensure that the validation is performed correctly without errors, ensure that the certificates configuration parameters are set accurately with the correct path to the certificate file. If you do not wish to validate the certificate, you may explicitly set the validate parameter in certificates parameter set to false.
twTlsClient_Connect - Error intializing TLS connection. Invalid certificate.Check if the ws_encryption parameter is present in your config.json file. By default, WS EMS enables TLS connection to ThingWorx platform. Ensure that the certificate file mentioned in the config.json is valid and stored in the path specified in the config.json. For debugging purposes, you can set the ssl parameter to none in ws_encryption configuration parameter.
[WARN ] ... Main - Unable to connect to server. Trying .booted config file.Ensure that the host is up and running at the IP address and port number mentioned in the config.json file. Ensure that ThingWorx is running on the host address at the correct port number. Ensure that all appropriate networking ports are open and available.

 

 

Step 6: Configure Lua Script Resource (LSR)

 

The Lua Script Resource (LSR) is used to implement Things on the Edge device. Using the Lua Script Resource, you can define for your Raspberry Pi:

  • Data Shapes
  • Properties
  • Services
  • Tasks

NOTE: The steps in this guide install the LSR on the same server (Raspberry Pi) as the EMS but it could also be installed on another server.

 

  1. Launch a new terminal session that will be used to configure and launch the LSR.
  2. Navigate to etc folder.
    cd microserver/etc
  3. Create the config.lua file.
    sudo nano config.lua
  4. Set the logger level.
    scripts.log_level = "INFO"
  5. Turn off encryption for connection to EMS. This should only be used for testing.

    scripts.script_resource_ssl = false
    scripts.script_resource_authenticate = false
  6. Create the Edge RemoteThing.

    scripts.PiThing = {
        file = "thing.lua",
        template = "PiTemplate",
        scanRate = 1000,
        taskRate = 30000
        }

 

NOTE: This configuration tells the Lua Script Resource to create a Thing called PiThing whose template definition is in PiTemplate.lua file in the path etc/customs/templates/PiTemplate.lua. You will create the template file PiTemplate.lua in the next section.

 

  • Property    Description
    scanRateControls how frequently (milliseconds) properties are evaluated and pushed to the server. In our example, the Pi will check every 1 second if the values have changed. If so, the values will be pushed to the server.
    taskRateControls how frequently the tasks specified in the Thing's template should be executed. In our example, the task will run every 30 seconds.

 

7.  Set the IP and port address of the LSR host server.

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

 

Sample config.lua File

 

Here is an example of a complete config.lua that can be used to configure the Lua Script Resource.

 

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

 

 

Step 7: Configure Template File (Properties)

 

The template file is located in the microserver/etc/custom/templates directory. The template file provides a base configuration for defining Properties, Services, tasks, etc. This section will focus on defining the template file and adding Properties.

 

  1. Navigate from the installation directory to the templates folder at microserver/etc/custom/templates .
    cd microserver/etc/custom/templates
  2. Create the file PiTemplate.lua.
    sudo nano PiTemplate.lua

NOTE: This is the same template filename used in config.lua in the previous section.

3. Define the template.

  • The module statement is used to define the template containing the configuration for the software component of the edge device.
    module ("templates.PiTemplate", thingworx.template.extend)

 

Parameter                                   Description

templates.PiTemplaterefers to the name of the template file: PiTemplate.lua
thingworx.template.extendidentifies the file as a template and provides base Thingworx template implementation

 

4. Define the Properties.

  • For this guide, we are going to use the Raspberry Pi’s system properties like CPU temperature, clock frequency and internal voltage as sensor readings for the Remote Thing. Add the properties for these in your PiTemplate.lua file.
    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}

NOTE: This code defines the properties cpu_temperature, cpu_freq and cpu_volt with a baseType of NUMBER. Additionally, it sets each default value to null as well as sets the pushType to ALWAYS which means that the property is always pushed to the Thingworx Server from the Raspberry Pi. The pushType can be set to ALWAYS, ON, OFF, NEVER or VALUE.

 

Sample PiTemplate.lua File

 

Here is an example of a complete PiTemplate.lua that can be used to configure the Lua Script Resource.

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}

 

 

Click here to view Part 3 of this guide. 

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