PI Web API and Python

The OsiSoft PI has an html based application system called PIWebAPI to extract data. There are a couple of steps to follow to set up the system, which I will cover in my posts later, but for now, let's see how we can access the data.

We will search for all tags in our database called "DTBASE" and show the results.

Assumptions:
IP address: 192.168.1.44
Username to piwebapi: username
Password to piwebapi: password

import base64
import requests
from django.shortcuts import render, redirect

First we need to create the query url. We can use api url creator programs such as Postman. It is much easier to check if API is actually working.

url = "https://192.168.1.44/piwebapi/search/query?q=*

Then we will create the header string for html before browsing it using GET.

usrPass = username + ":" + password
b64Val = base64.b64encode(usrPass.encode()).decode("ascii")
payload = ""
headers = {
        "Authorization": "Basic %s" % b64Val,
        'cache-control': "no-cache",
    }

And finally we browse the link:

response = requests.request("GET", url, verify=False, data=payload, headers=headers, params=queryString)

The "response" variable is the response which can be extracted as Json using Json package from Python. In later posts we will show how to use Django, a python based server to extract data from PiWebAPI and show it in html format.

At IntelliPetri we help clients to write phone and desktop applications to show KPIs, alarms, etc from OsiSoftPI program.

Comments

Popular posts from this blog

Python Basics

Visit website using proxy server, and with different IP addresses