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

GET request in Python

Level 9

GET request in Python

Hi,

I am successfully transferring my sensor data which is collected from Raspberry pi to Thingworx using REST API in Python

i.e..

import RPi.GPIO as GPIO

import time

import requests

import json

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

headers = { 'Content-Type': 'application/json', 'appKey': '8c6083ab-8d01-4116-8775-c672cf79147b' }

TRIG=11

ECHO=12

GPIO.setup(TRIG,GPIO.OUT)

GPIO.output(TRIG,0)

GPIO.setup(ECHO,GPIO.IN)

time.sleep(0.1)

while True:

     GPIO.output(TRIG,1)

     time.sleep(0.00001)

     GPIO.output(TRIG,0)

     while GPIO.input(ECHO)==0:

     pass

     start=time.time()

     while GPIO.input(ECHO)==1:

     pass

     stop=time.time()

     c=(stop-start)*17000

     print c

     payload = { 'Distance' : c}

     response = requests.put('http://192.168.100.5:80/Thingworx/Things/ultrasonicsensor/Properties/Distance', headers=headers, json=payload, verify=False)

     time.sleep(0.5)

But Now i want to control my LED which is connected to Raspberry pi from Thingworx,,so i used GET resuest.

but i am getting the result in the form of HTML. can u please suggest me , how do i parse the HTML format to Required Property value!!

i.e.

import RPi.GPIO as GPIO

import time

from time import sleep

import requests

import json

GPIO.setwarnings(False)

headers = { 'Content-Type': 'application/json', 'appKey': '8c6083ab-8d01-4116-8775-c672cf79147b' }

GPIO.setmode(GPIO.BOARD)

LED=15

GPIO.setup(LED,GPIO.OUT)

payload = { 'cont' : 'cont'}

getreq = requests.get('http://192.168.100.5:80/Thingworx/Things/GET1/Properties/cont',headers=headers,verify=False)

s=getreq.text

print s

 

if s==1:

    GPIO.output(LED,1)

    time.sleep(5)

else:

    GPIO.output(LED,0)

    #time.sleep(0.5)

So, if i run this code i am getting output as:

<HTML><HEAD><TITLE>Property Value For GET1 &#x3a; cont</TITLE><LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK><META http-equiv='Content-Type' content='text/html'></META><META http-equiv='cache-control' content='no-cache, no-store'></META><META http-equiv='expires' content='-1'></META><META http-equiv='pragma' content='no-cache, no-store'></META></HEAD><BODY><IMG SRC="/Thingworx/images/ThingworxLogo.png"/><BR/><H1>Property Value For GET1 &#x3a; cont</H1><TABLE><TR><TH>cont</TH></TR><TR><TD>1.0</TD></TR></TABLE></BODY></HTML>

Here "<TD>1.0</TD>" showing the data. i need to Parse the "1.0" and use that value to Trigger my raspberry pi to do further actions.


i attached a image of my Thing created in Thingworx.

please suggest me. how to use that data coming from Thingworx to Trigger my raspberrypi.

Thank you

3 REPLIES 3

Re: GET request in Python

Add header 'Accept': 'application/json'. Then you'll get the return output in JSON, which should be easier to handle than HTML.


See also https://community.thingworx.com/docs/DOC-3315 Get Single Property Value

Re: GET request in Python

Here's how to parse the value from the output:

import json

import requests

url = 'http://192.168.100.5:80/Thingworx/Things/GET1/Properties/cont'

headers = {'appKey': '8c6083ab-8d01-4116-8775-c672cf79147b',

          'Accept': 'application/json'}

getreq = requests.get(url, headers=headers).json()

s = getreq["rows"][0]["cont"]

print(s)

Re: GET request in Python

Its worked.Thank you so much