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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Connect Raspberry Pi to ThingWorx Part 1

No ratings

 

Connect a Raspberry Pi to ThingWorx using the Edge Micro Server (EMS).

 

Guide Concept

 

This project will introduce you to the Edge MicroServer (EMS) and how to connect your ThingWorx server to a Raspberry Pi device.

 

Following the steps in this guide, you will be able to connect to the ThingWorx platform with your Raspberry Pi. The coding will be simple and the steps will be very straight forward.

 

We will teach you how to utilize the EMS for your Edge device needs. The EMS comes with the Lua Script Resource, which serves as an optional process manager, enabling you to create Properties, Services, Events, and Subscriptions for a remote device on the ThingWorx platform.

 

You'll learn how to

 

  • Set up Raspberry Pi
  • Install, configure and launch the EMS
  • Connect a remote device to ThingWorx

 

NOTE: This guide's content aligns with ThingWorx 9.3. The estimated time to complete ALL parts of this guide is 30 minutes. 

 

Step 1: Setup Raspberry Pi

 

  1. Follow the setup instructions to get your Raspberry Pi up and running with the Raspberry Pi OS operating system.
  2. Ensure that your Pi has a valid Ethernet or Wifi connection.
  3. If your Pi is connected to a monitor/keyboard, run ifconfig from the Command Line Interface (CLI) to determine the IP address.
  4. If you are connecting remotely, probe your local network to find your Pi using one of these methods to determine the IP address.
  5. Log into your Raspberry Pi using the userid/password combination pi/raspberry.

 

Step 2: Install the EMS

  1. Download the MED-61060-CD-054_SP10_Microserver-Linux-arm-hwfpu-openssl-5-4-10-1509.zip attached here directly to the Raspberry Pi, or transfer it using a SFTP application such as WinSCP.
  2. After downloading the EMS zip file, unzip the archive in a suitable location on the Pi using the command below. Use the Tab key to automatically complete file names.
    unzip /MED-61060-CD-054_SP9_Microserver-Linux-arm-hwfpu-openssl-5-4-10-1509.zip
  3. After unzipping the distribution, a sub-directory named /microserver will be created inside the parent directory.

  4. Verify that microserver directory was created with the command ls -l

    unzip.png

     

  5. Switch into the microserver directory with the command cd microserver The microserver directory includes the following files. 

    unzippeddir.png   

 

File Name                    Description

wsemsAn executable file that runs the Edge MicroServer
luaScriptResourceThe Lua utility that is used to run Lua scripts, configure remote things, and integrate with the host system

 

 

Step 3: Create Application Key

 

In this step, you will be using the ThingWorx Composer to generate an Application Key. The Application Key will be used to identify the Edge Agent. The Application Key is tied to a user and has the same entitlements on the server.

 

Using the Application Key for the default User (Administrator) is not recommended. If administrative access is absolutely necessary, create a User and place the user as a member of the SecurityAdministrators and Administrators User Groups.

 

Create the User the Application Key will be assigned to.

 

  1. On the Home screen of Composer click + New.


    click_new.png

     

  2. In the dropdown list, click Applications Key.


    new_application_key.png

     

  3. Give your Application Key a name (ie, MyAppKey).
  4. Set the User Name Reference to a User you created.


    update_key.png

     

  5. Update the Expiration Date field, otherwise it will default to 1 day.
  6. Click Save.

    save_key.png

     

Step 4: Configure the EMS

 

The EMS consists of two distinct components that do slightly different things and communicate with each other. The first is the EMS which creates an AlwaysOn™ connection to the ThingWorx server. It binds things to the platform and automatically provides features like file transfer and tunneling.

 

The second is the Lua Script Resource (LSR). It is used as a scripting language so that you can add properties, services, and events to the things that you create in the EMS. The LSR communicates with your sensors or devices. The LSR can be installed on the same device as the EMS or on a separate device. For example, one LSR can be a gateway and send data from several different things to a single EMS.

 

architecture-0-0-0-0-1494600892.png

 

  1. Open a terminal emulator for the Raspberry Pi.
  2. Change directory to microserver/etc.
    cd microserver/etc
  3. Create a config.json file.
    • EMS comes with two sample config files that can be used as a reference for creating your config.json file. The config.json.minimal file provides minimum and basic options for getting started. The config.json.complete provides all of the configuration options.

      directory-0-0-0-0-1494600967.png

       

    • Create the config.json file in the etc folder.
      sudo nano config.json
  4. Edit the config.json file

    • ws_servers - host and port address of the server hosting the ThingWorx Platform. If you are using a Developer Portal hosted server, your server hostname is listed on the dashboard.

      hosted_server_controls.png
      {"host":"<TwX Server IP>", "port":443}
    • http_server - host and port address of the machine running the LSR. In this case it will be your localhost running on the raspberry pi.
      {"host":"127.0.0.1","port":8080,
      "use_default_certificate":  true,"ssl": false, "authenticate": false}
    • appKey - the application key generated from the ThingWorx server. Use the keyId generated in the previous step "Create Application Key".
      "appKey":"<insert keyId>"
    • logger - sets the logging level for debugging purposes. Set to log at a DEBUG level.
      ("level":"INFO"}
    • certificates - for establishing a secure websocket connection between the ThingWorx server and the EMS. A valid certificate should be used in a production environment but for debugging purposes you can turn off validation and allow self signed certificates.
      {"validate":false, "disable_hostname_validation":  true}

NOTE: To ensure a secure connection, use valid certificates, encryption and HTTPS (port : 443) protocol for establishing a websocket connection between the EMS and the ThingWorx Platform.

5. Exit and Save.

ctrl x

 

Sample config.json File

 

Replace host and appKey with values from your hosted server.

 

{
        "ws_servers":   [{
                        "host": "pp-2007011431nt.devportal.ptc.io",
                        "port": 443
                }],
        "appkey":       "2d4e9440-3e51-452f-a057-b55d45289264",
        "http_server":  {
                "host": "127.0.0.1",
                "port": 8080,
                "use_default_certificate":  true,
                "ssl":  false,
                "authenticate": false
        },
        "logger":       {
                "level": "INFO"
        },
        "certificates": {
                "validate": false,
                "disable_hostname_validation":  true
        }
}

 

 

Click here to view Part 2 of this guide. 

Comments

It is possible to connect the raspberry with a Thingworx in the cloud. In my application I cannot discover the Thingworx IP because it is in the cloud provided by PTC. 

 

Translated by the Community Moderation using Google Translate 

 

É possivel fazer a conexão do raspberry com um thing worx em cloud. Em minha aplicação não consigo descobrir o IP do ThingWorx pois é em cloud fornecido pela PTC

 

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