Recently a customer from the ThingWorx Academic Program sent in a sample program they were having problems with. They were trying to post data from a Raspberry PI using Python to their ThingWorx server. It turns out that their program did work just fine and was also a great example of posting data from a PI using REST.

 

Here is how to set up this example.

 

1. Import the attached "Things_TempAndHumidityThing.xml" entity file.

2. from the PI run 'sudo pip install requests'

3. from the PI run 'sudo pip install logging'

4. from the PI run 'sudo pip install http_client'

5. Create a python file call test.py that contains this example code:

#!/usr/bin/python

 

import requests

import json

import logging

import sys

 

 

# These two lines enable debugging at httplib level (requests->urllib3->http.client)

# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.

# The only thing missing will be the response.body which is not logged.

try:

    import http.client as http_client

except ImportError:

    # Python 2

    import httplib as http_client

http_client.HTTPConnection.debuglevel = 1

 

 

# You must initialize logging, otherwise you'll not see debug output.

logging.basicConfig()

logging.getLogger().setLevel(logging.DEBUG)

requests_log = logging.getLogger("requests.packages.urllib3")

requests_log.setLevel(logging.DEBUG)

requests_log.propagate = True

 

#NYP Webserver URL in Thingworx

 

 

NYP_Webhost = sys.argv[1]

 

 

App_Key = sys.argv[2]

 

ThingName = 'TempAndHumidityThing'

 

headers = { 'Content-Type': 'application/json', 'appKey': App_Key }

 

payload = { 'Prop_Temperature': 45, 'Prop_Humidity': 33 }

 

response = requests.put(NYP_Webhost + '/Thingworx/Things/' + ThingName + '/Properties/*', headers=headers, json=payload, verify=False)

6. From the command line run, './test.py http://twhome:8080 e9274d87-58aa-4d60-b27f-e67962f3e5c4' except substitute your server and your app key.

7. A successful response should look like:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): twhome

send: 'PUT /Thingworx/Things/TempAndHumidityThing/Properties/* HTTP/1.1\r\nHost: twhome:8080\r\nappKey: e9274d87-58aa-4d60-b27f-e67962f3e5c4\r\nContent-Length: 45\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.8.1\r\nConnection: keep-alive\r\nContent-Type: application/json\r\n\r\n{"Prop_Temperature": 45, "Prop_Humidity": 33}'

reply: 'HTTP/1.1 200 OK\r\n'

header: Server: Apache-Coyote/1.1

header: Set-Cookie: JSESSIONID=E7436D2E6AE81C84EC197D406E7E365A; Path=/Thingworx/; HttpOnly

header: Expires: 0

header: Cache-Control: no-store, no-cache

header: Cache-Control: post-check=0, pre-check=0

header: Pragma: no-cache

header: Content-Type: text/html;charset=UTF-8

header: Transfer-Encoding: chunked

header: Date: Mon, 09 Nov 2015 12:39:24 GMT

DEBUG:requests.packages.urllib3.connectionpool:"PUT /Thingworx/Things/TempAndHumidityThing/Properties/* HTTP/1.1" 200 None

My thanks to the customer who sent in the simple example.