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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Simple Java Code for a Client Device to Update a Thing Property

ptc-6436697
1-Newbie

Simple Java Code for a Client Device to Update a Thing Property

Enclosed is a Java Class that only relies on the base Java SE and allows one to update a Thing's Property. I use it in a BeagleBone Black to update sensor values but it should work just as well in other applications.

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ThingWorxRESTClient{
    private String api_key;
    private String thingworx_hostname;
  
    public static void main(String[] args) {
        //Example Usage
        ThingWorxRESTClient tw_rest = new ThingWorxRESTClient("maker01.cloud.thingworx.com", "<<your api key>>");

        try{
            tw_rest.UpdateThingWorxProperty("TemperatureAndHumidity", "Temperature", "85.7");
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    public ThingWorxRESTClient(String thingworx_hostname, String api_key) {
        super();
      
        this.thingworx_hostname = thingworx_hostname;
        this.api_key = api_key;
    }
  
    public void UpdateThingWorxProperty(String thing_name, String thing_property, String value) throws IOException {

        URL thingworx_rest_url = new URL("http://"thingworx_hostname"/Thingworx/Things/"thing_name"/Properties/"thing_property"?appKey="api_key"&amp;method=put&amp;value="+value);
        HttpURLConnection http_connection = (HttpURLConnection) thingworx_rest_url.openConnection();
        http_connection.setDoOutput(true);
        http_connection.setRequestMethod("PUT");
        InputStream in = http_connection.getInputStream();//We get an InputStream to cause the HttpURLConnection to actually connect to the server
        in.close();
    }

}

0 REPLIES 0
Top Tags