Sending API Value to a tag

Hi Guys , I would like your help , i have this API which i isolated a specific value as below the value 234.56

I want to send this value to a specific Tag and use it as a setpoint. How can i achieve this? Have anyone of you done something Similar ?
Thank you in advance


Here is an example shows that how you can get the spesific data from api

Hi Omer , i have got the specific data from the API. I want to use that specific value to write it in a Tag using it as a setpoint. Still thank you for your fast response to this thread.

You would use the system.tag.writeBlocking | Ignition User Manual function.

tagPaths = ['theTagToWrite']
values = [valueFromTheAPI]
result = system.tag.writeBlocking(tagPaths,values)

Thank you Bschroeder, im not sure in which area to write this code. can you give me some more details please?

Typically, you would make the call out a HTTP end point in a script, process the data and then write it to the tags.

You can do that in a button, timer script etc...

So let's start from the beginning.

How/where are you calling the API?

I took A Text Display i added the binding Type HTTP and then i isolated the required value i want to use as a setpoint


required value
image

So... if that page isn't open then what do you want to happen? If you are only reading this on that page via a binding, then when that page isn't open then nothing will be happening.

Lets say this value 234.56 i want to write it on this tag which is a Register on PLC. the communication is a Modbus TCP
image

is it possible ?

Of course it is possible.

However, if you are reading this value just into a label on a view, and that view isn't open then the API is being called to get the new value.

So you need to move your logic to access the API data to a gateway timer script and use the system.net.httpClient | Ignition User Manual scripting function to get the data, process that value and then write it to the tag you want via the system.tag.writeBlocking function.

I understand but my knowledge on this is very limited , i would be kindly ask you to assist me on this please if you can and you have the time for it.

Sure.

Put this into it's own project scripting library. Something like project.api

httpAddress = "apiAddressHere"
tagPath = "fullTagPathHere"
# Create the JythonHttpClient.
client = system.net.httpClient()

def getSetpoint():
	# Sent a GET request.
	response = client.get(httpAddress)
	if response.good:
	    # Do something with the response
	    setpoint = response.json['nested1']['value1']
	    print setpoint #Comment out this line when it is working
	    #system.tag.writeBlocking(tagPaths=[tagPath], values=[setpoint]) #Un commend this line when it is working

Edit the httpAddress and tagPath details.

Then in your scripting console call the project script.

project.api.getSetpoint()

You should get the setpoint from the API printed in the console.
If you don't post the error here.

And once it is all working, comment the

print setpoint

and uncomment the

system.tag.writeBlocking

and run it again. Then your tag should change to that setpoint.

Once you have that going, then move the call to a Gateway timer script that runs at the pace you want it to run to update it on the frequency you want.

1 Like

thank you but how do i check if this is working see below

you could call this function in the script console

or just put it in a timer immeadiatly

1 Like

I was babystepping it... :smiley:

1 Like

Save the project, open up the scripting console and run:

Project_API.getSetpoint()

Make sure you are in read/write mode in designer.

1 Like

I'd put @system.util.runInGateway above that library function, so your call from the designer script console will produce a valid gateway-scope test. :grin:

(You could also return the result for printing/use by the caller.)

3 Likes

Hi Mate ,sorry for the late reply, Just to inform you that It did worked. See screenshot below. Much appreciated.

Now i have to somehow be able to control this when to execute these values , and how to stop .

1 Like