ThingWorx Edge .NET SDK > Installing and Navigating the Directories of the ThingWorx Edge .NET SDK > Steam Sensor Example for the .NET SDK
Steam Sensor Example for the .NET SDK
The .NET SDK installation includes a 64-bit binary file for the Steam Sensor example that you can run "out-of-the-box". This example provides properties that are updated by the VirtualThing as well as properties that are updated from the ThingWorx platform. There are services and an event example as well.
Initial Setup
Before you can run the example, you need to create the entities for the Steam Sensor example in your ThingWorx platform by importing the Entities_SteamSensor.xml file stored in the C:\Program Files (x86)\tw-dotnet-sdk-v.v.v\SteamSensor\import\ subdirectory of the .NET SDK installation. To import the entities file, follow these steps:
1. Log in to ThingWorx Composer. The rest of the steps are derived from the new Composer, v.8.4.0.
2. From the Import/Export menu of Composer, select Import, as shown here:
3. From the Import Option menu, select From File:
4. From the Import Type menu, select Entity.
5. Skip the two check boxes, and under Import Source, select Single File.
6. Click Browse, and navigateto the import subdirectory in C:\Program Files (x86)\tw-dotnet-sdk\SteamSensor\, and select the XML file.
7. After selecting the file, click Open, and when you return to the Import dialog, click Import.
8. When the import completes, click Close, and select Browse > Things. The new things for this example appear at the top of the list.
Running the SteamSensor Example
At the top of the .NET SDK installation directory are the SteamSensorConsole executable files. To run this example:
1. Open a Command Prompt.
2. Navigate to C:\Program Files (x86)\tw-dotnet-sdk\.
3. At the prompt, enter
SteamSensorConsole <url_to_ThingWorx_platform> <application key> <scan_rate_ms>
<url_to_ThingWorx_platform> is the address (IP or host name and port number) for connecting to the ThingWorx platform. The following example will connect to a ThingWorx platform that is running on the local machine, using port 8443 (using SSL/TLS):
<application_key is the Application Key for authentication and authorization with the ThingWorx platform. An Application Key needs to be created, using ThingWorx Composer. The format of an Application Key follows:
<scan_rate_ms> is the interval (in milliseconds) that the example waits between scans for properties and events. To scan every second, the value is 1000.
Managing Bindings in ThingWorx Composer
After starting the Steam Sensor, you can manage the bindings for the properties, services, and events using ThingWorx Composer, as follows:
1. Log in to ThingWorx Composer.
2. Open the SteamSensor1 thing.
3. Select the Properties tab.
4. Refresh the properties to verify that the isConnected property is true.
5. If the page is not in Edit mode, click the Edit button to enter Edit mode.
6. Click the Manage Bindings button to browse for the remote properties.
7. In the Manage Property Bindings page, click the Remote tab to view the remote properties.
8. Click the Add All Above Properties button to add all of the properties.
9. Click Done.
10. Click Save.
11. Refresh the properties and verify that they are changing.
12. Select the Services tab.
13. Click the Browse Remote Services button.
14. Click the Add All Above Services button to add the services.
15. Click Done.
16. Click Save.
17. Click the Test button next to the AddNumbers service and verify it is working.
18. Select the Events tab.
19. Click Browse Remote Events.
20. Click Add All Above Events.
21. Click the Create button next to the SteamSensor.Fault Data Shape if it has not already been created.
22. Click Save.
23. Select the Subscriptions tab.
24. Click Add My Subscriptions.
25. From the Select an Event drop down menu, select SteamSensorFault.
26. Select the Enabled check box.
27. Enter the following for the Script text that will write an event when there is a steam sensor fault:
logger.error("Steam Sensor 1 Fault: " + eventData.message);
28. Click Done.
29. Click Save.
Source Files
The source files for this binary are in the SteamSensor subdirectory of the installation:
SteamSensorConsole.csproj—The project file for the Steam Sensor Console example.
SteamSensorConsole.sln—The solution file for the project.
SteamThing.cs—This file sets up all the property, service, and event definitions for this example.
SteamSensorClient.cs—This file contains the user interface for the console. It configures the client based on user input (using the ClientConfigurator class), instantiates the client (ConnectedThingClient class), and starts it. As long as the connection is successful, it collects values from the steam sensors.
SteamSensorConsole.exe.config—This XML document can be used to set the logging level. If you encounter an error while running the console, changing the log level to DEBUG or VERBOSE provides more information to help you resolve the error.
Modifying the SSL/TLS Configuration in the Source File
Depending on your use of SSL/TLS with this example, you need to make some changes in the SteamSensorClient.cs file. The steps below explain how:
1. Open the file, SteamSensorClient.cs, file.
2. Locate the following lines:
/***** WARNING: For Development purposes only. *****/
/***** Do not use these settings in a production environment. *****/
//config.AllowSelfSignedCertificates = true;
//config.DisableCertValidation = true;
/***** WARNING *****/
3. If you are connecting over HTTPS and using a self-signed certificate, the client configuration (ClientConfigurator) must be set up to allow self-signed certificates. Remove the comment characters (//) from the two lines that start with //config.. After you remove the comment characters, the lines should look like this:
/***** WARNING: For Development purposes only. *****/
/***** Do not use these settings in a production environment. *****/
config.AllowSelfSignedCertificates = true;
config.DisableCertValidation = true;
/***** WARNING *****/
These settings mean that you can use your self-signed certificate and that validation of certificates is turned off.
4. If you must use an insecure (non-SSL/TLS) connection, you need to call the disableEncryption() method on the ConnectedThingClient after instantiating the client but before calling connect on it. Locate the following lines:
/***** WARNING: For Development purposes only. *****/
/***** Do not use these settings in a production environment. *****/
// To connect to an insecure (non-SSL) server
/***** WARNING *****/
5. Remove the comment characters from the beginning of the line that contains the disableEncryption() method. It should now look like this:
/***** WARNING: For Development purposes only. *****/
/***** Do not use these settings in a production environment. *****/
// To connect to an insecure (non-SSL) server
/***** WARNING *****/
6. Save the SteamSensorClient.cs file, and run the project with the correct command line arguments as described above.
It is strongly recommended that you use SSL/TLS with a valid server certificate for connections between your devices and the ThingWorx platform. Use self-signed certificates, the AllowSelfSignedCertificates and DisableCertValidation settings of the ClientConfigurator, or the DisableEncryption method of the ConnectedThingClient for development purposes only. Do NOT use them in a production environment.
The only property in the Steam Sensor example that does not update is the TemperatureLimit property. This property defines the limit for the fault of the steam sensor. The fault occurs when the value of this property is greater than zero and the temperature exceeds the limit defined by this property. If the value of this property is 0 or less, the fault does not occur.
Ranges from 400-440.
Temperature of the steam sensor.
Ranges from 18-23.
Pressure of the steam sensor.
Starts at 0 and increments by 0-1 every scan rate.
Total flow through the steam sensor.
Usually true, set to false on 15– second intervals.
Inlet valve of the steam sensor.
If this value is greater than zero and the value of Temperature rises above this value, a fault is generated in the steam sensor.
The temperature value that, when reached, causes a fault in the steam sensor.
True when a fault occurs (see what causes a fault above), false otherwise.
Indicates that a fault occurred in the steam sensor.
AddNumbers (NUMBER a, NUMBER b)
Returns the sum of two double precision numbers.
Returns a large string value of all zeros.
Returns an InfoTable that contains several readings from the steam sensor.
Occurs when the value of TemperatureLimit is greater than zero and the value of Temperature is greater than TemperatureLimit.
The event data is defined by the SteamSensor. Fault data shape and contains only a single STRING field that is named message. When a fault occurs, the content of message is sent in the event data.