teaser for labview http request

How to make REST call of DataLayer through LabVIEW

kuldeepM
Long-established Member


Introduction

LabVIEW is a graphical programming language, widely used in scientific and engineering communities. It is often used to develop control systems, data acquisition and analysis applications, and other complex software systems. One common task that LabVIEW developers may need is making HTTP requests or REST calls to web services or APIs.

This is a basic overview of how to make HTTP requests or REST calls in LabVIEW using the built-in HTTP client library. Of course, there are many more advanced features and options available, depending on your specific application and requirements. But this should give you a good starting point for building your own HTTP client applications in LabVIEW.

Prerequisites 
  • LabVIEW software
  • ctrlX CORE web interface


If you are used to the LabVIEW environment skip the description which starts with '-->'.

Configuration

To create such an interface in LabVIEW go through the step-by-step description.

Step 1: Add HTTP pallet in the LabVIEW environment 

To make an HTTP request or REST call in LabVIEW, you can either use the built-in HTTP client libraries that are included with the LabVIEW software or you can download the 'Blue Easy HTTP Client pallet' from the VI package manager. These libraries allow you to easily send and receive HTTP requests, parse the response data, and handle any errors or exceptions that may occur.

To download the 'Blue Easy HTTP Client Pallet', first start for the VI package manager and a window that can be seen in the picture below will open.

VI package managerVI package manager

Step 2: Open a new VI in LabView 

To open a new VI in LabVIEW go to File --> New VI and a new window will open. To get the Front panel and Block Diagram window side-by-side click on windows --> Tile left and right. Now interface should look like the picture below.

Blue Easy HTTP Client palletBlue Easy HTTP Client pallet

To Bulit HTTP Request VI, insert HTTP objects from Blue Easy HTTP client as can be found in the picture above.

In this pallet, there are different VIs for HTTP requests, generate HTTP request headers and OPEN and CLOSE Client.

Create a flow to request a token

To get a token for authorization add VIs as described below from the blue easy HTTP client pallet

  1. Open Handle
  2. Add Headers
  3. Post

Connect and configure these VIs from nodes client, handle and error IO as shown in the picture below.

Connect VIsConnect VIs
To configure input and output of these VIs, click on respective VI and go to the node you want to configure, right click and there you will find three options for it.

  • Create Constant
  • Create Control
  • Create Indicator

Configuration of nodes on VIsConfiguration of nodes on VIs

--> For 'open handle':  Go to verify server node and right-click. Now choose 'create constant' and set it to a false value by clicking on the generated button. We have to set it to false because by default while making the rest call to the core server it will find it inauthentic and to clear this error, we have set verify server to a false value.

--> For 'add headers': Go to the HTTP header node. You can find the node on the left side of 'add headers VI'. Additionally the “Content-Type” and “application/json” has to be added to the request header to tell the server how data should be interpreted. 

--> For 'POST': We need to configure three points 'url, data, error out and body'. Url is from where we want to post the data. To configure url go to the left side of POST VI and choose create constant for that node. Now add 'https://192.168.1.1/identity-manager/api/v1/auth/token' to this url dialogue box. Also on the left side, you can find data. Create a constant for this node. The data has to be a permitted user and a password written as a JSON (JavaScript Object Notation) object. The standard ones are “boschrexroth” ({"name":"boschrexroth","password":"boschrexroth"}). On right side of this VI, you can find error-out and body node. For these nodes create indicator.

Now click on the run button or press ctrl+r to run the code and the front panel will look like this. we can move and resize these boxes as we like to get better readability of data.

Front panel for getting tokenFront panel for getting token

Now copy this bearer token for later use.

Create flow to read single data layer node 

To make HTTP read requests add VIs as described below from the blue easy HTTP client pallet

  1. Open Handle
  2. Add Headers (3 times)
  3. GET

Connect these VIs from the node's client handle and error IO as shown in the picture below.

Read a one node basic lowRead a one node basic low

--> Configure add header: Go to the HTTP headers node and create a constant, now add "Content-Type" and "application/json" to the boxes generated.

--> Configure second add header VI: Go to HTTP headers node create constantly. Now in the generated box, you can define the "authorization" header and set the value to "Bearer+space+generated_token_from_above".

--> Configure GET: For GET VI we are going to generate control for 'url and body'.

After configuration, your block diagram should look like the image below in the block diagram window. If you want to continuously read value then please add a while loop as shown image.

Block diagram for reading a single node from data layerBlock diagram for reading a single node from data layer

In the front panel, there should be a dialogue box for URLs in this box use can type in the API address of the variable node that they want to read. In my case, I have defined the PLC variable in PLC Engineering App which I want to read. For that purpose, I use the link: https://192.168.1.1/automation/api/v1/plc/app/Application/sym/PLC_PRG/iI'.

Now run this code with ctrl+r and your front panel window should look like this.

Front panel viewFront panel view

After pressing the stop button you can stop the while loop.

Create flow to write single data layer node 

To make HTTP write requests configuration of VIs and connection between VI is the same as the read method but the only change, in this case, is that we will use PUT instead of GET.

--> Configure PUT: For PUT VI we are going to generate control for 'url, data and body'. Here data section is used as a payload for making the rest call.

After configuration, your Block Diagram window should look like this.

Block diagram for write a node in data layerBlock diagram for write a node in data layer

Now go to front panel and define url and data to write. In my case:

url:https://192.168.1.1/automation/api/v1/plc/app/Application/sym/PLC_PRG/iI

data:{"type":"int16","value":-50}

and after running program with ctrl+r, your front panel should look like below.

Front panel for put methodFront panel for put method

You can also check your data layer node in the ctrlX CORE web interface to verify that it has made a change.

Create a flow to read bulk or multiple data layer nodes 

To create such program we need to insert '1 OpenHandle, 3 Add Headers, 1 PUT'. Basically the same as the write method but we are going to include one more header in this case.

These three headers are respectively configured for 

  1. 'Content-Type':'application/json'
  2. Create control and go to the front panel and type define the "Authorization" header and set the value to "Bearer+space+generated_token_from_above"
  3. 'Accept': 'application/json'

Now your block diagram window should look like this:

Block diagram for bulk readingBlock diagram for bulk reading

Now go to the front panel and in url box type exact link below for bulk reading:

https://192.168.1.1/automation/api/v2/bulk?type=read

In data box please type in json format below:

[
{
"address": "plc/app/Application/sym/PLC_PRG/iI",
"type": "int16"
},
{
"address": "plc/app/Application/sym/PLC_PRG/iB",
"type": "bool8"
},
{
"address": "plc/app/Application/sym/PLC_PRG/iR",
"type": "float"
},
{
"address": "plc/app/Application/sym/PLC_PRG/iS",
"type": "string"
}
]

You can get these addresses and types from the data layer section of the ctrlX core web interface as described in the picture below. So instead of values that I defined, you can type in the address and type that you want to read.

Data layer web interfaceData layer web interface

Now press ctrl+r to run the LabVIEW program and your front panel should look like something in the below image.

Front panel window of bulk read programFront panel window of bulk read program

Click the stop button to terminate the while loop.

Create a flow to write bulk or multiple data layer nodes 

For this purpose, we going to use the same program that we have created for a bulk reading method. Only things which changes in this case are 'url and data'.

In url section of front panel copy link below:

https://192.168.1.1/automation/api/v2/bulk?type=write

In data section write json below:

[
{
"address": "plc/app/Application/sym/PLC_PRG/iI",
"type": "int16",
"value":100
},
{
"address": "plc/app/Application/sym/PLC_PRG/iB",
"type": "bool8",
"value":false
},
{
"address": "plc/app/Application/sym/PLC_PRG/iR",
"type": "float",
"value":5.55555
},
{
"address": "plc/app/Application/sym/PLC_PRG/iS",
"type": "string",
"value":"HELLO WORLD SECOND TIME"
}
]

As I mentioned above in bulk reading method you can change address and type as per your requirement. Now press run button and changes should have made to following addresses. To verify go to data layer web interface check for your nodes.

Changed values in datalayerChanged values in datalayer

Thank you for taking the time to read this article. I hope you found it informative and enjoyable. If you have any questions or comments, please feel free to leave them in the comments section below. I would love to hear from you and continue the conversation. Your feedback is always appreciated!

1 Comment
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist