Bring Your Ignition Data To The Web With the WebDev Module

Inductive Automation offers the WebDev Module (download), which “allows users to directly program against the web server inside the Ignition Gateway.” Today, I will demonstrate how the WebDev Module can be easily utilized to bring your Ignition Data to the web.

After installing the WebDev Module in the usual way, you will see the following in your Project Browser:

The Web Dev module allows you to serve up files (such as images), text (such as HTML), and dynamically created content via Python scripts. To create any of these three types of resources, simply right click on Web Dev in the Project Browser and select the resource you want to create. To create our API, we will use the Python resource type:

“Python resources are dynamic web resources. Each time a user browses to the URL associated with a python resource, the script will run and generate a response.”

Name your new resource “api”. In the api file, a boilerplate API to retrieve a certain row from a dataset tag would look like (in the doGet function):

myDataset = system.tag.read('myDataset').value
myRecord = {}
if not ('name' in request['params']): return {'html': '<html><body>This API requires a name parameter</body></html>'}
for row in system.dataset.toPyDataSet(myDataset):
  if row['name'] == request['params']['name']:
    for column in system.dataset.getColumnHeaders(myDataset):
      myRecord[column] = row['name']
    break
return {'json': system.util.jsonEncode(myRecord)}

Create the dataset tag ‘myDataset’ with a ‘name’ column and any columns you want to store for each person. Now, when you access the URL of the form: http://HOSTNAME:PORTNUMBER/main/system/webdev/PROJECTNAME/api?name=Stu you will be returned JSON with the record for the person you requested. Clearly, this API can be easily changed to retrieve different kinds of tags as well.

Now let’s secure our API by requiring authentication with one click:

Wow! Nine lines of code and we have a simple API to access our tags. What are some other simple things we could do with the Web Dev module? Suppose the CEO wants to be able to generate the latest production report PDF on demand and retrieve it with her web browser. You’ve already got the report made in Ignition, so let’s make a new Python resource named “report” with the following contents in the doGet function:

filePath = system.file.getTempFile('pdf')
split = filePath.split('\\')
file = split[-1]
folder = '\\\\'.join(split[:len(split) - 1])
system.report.executeAndDistribute(path = 'Report', 
project = 'webDevProject', 
action = 'save', 
actionSettings = {'path': folder, 
'fileName': file, 
'format': 'pdf'})
return {'file': filePath}

As you can see, we’re using functions that are built-in to Ignition and just returning their results. Following the URL format, this report URL would be http://HOSTNAME:PORTNUMBER/main/system/webdev/PROJECTNAME/report

So far we have shown examples that respond to HTTP GET. Using the HTTP Method dropdown, you can create a Python resource that will respond to GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE HTTP methods. You can imagine the kind of functionality you could build with these methods, like allowing a major customer to place an order through your API that communicates directly and instantly with Ignition. Of course, when connecting your facility with the outside world, you will want to take security precautions such as requiring HTTPS (a simple checkbox in the Web Dev module), using VPNs to restrict access, and sanitizing any user input.

We have shown how easy it is to create Python resources in the Web Dev Module to open up your Ignition data to the web. The possibilities on what you will do next are endless!

Perfect Abstractions offers a website and API service using the Web Dev Module. We also sell the Web Dev Module. Contact us today to get the Web Dev Module and to get help in using it!

Leave a Reply

Your email address will not be published. Required fields are marked *