What steps do I need to take to write an Ignition tag from external (python) code?
By external, you mean outside of ignition ?
Use webdev and setup an API.
Thank you
I have not used web dev so I will need to do some investigating
Could you (or anyone) point me to an example
Thanks in advance
Using webdev ?
Expose a POST endpoint that will receive the tag path and its value through the request's data, then use these with system.tag.readBlocking
.
I have an edge project that's just for that, but a bit more complicated so I can't give you the code as an example. It also takes lists to update multiple tags at once.
Basically, it would look something like this:
def doPost(request, session):
path = request['data']['path']
value = request['data']['value']
ret = system.tag.writeBlocking([path], [value])
return {'json': {'result': ret}}
Thank you Pascal
What I don’t get is what is on the Python (non-Ignition) side
Regards
Ross Dye
0400669880
it will be a url with parameters.
/projectName/NameOfDoPostPage?path='path/to/Tag'.,value=ValueToWrite
If the endpoint's method is POST and he's pulling data from request.data
, I think he'll have to pass the payload in the request's body.
something like
import requests
url = 'your endpoint's address'
payload = {
'path': "tag path",
'value': "some value"
}
requests.post(
url=url,
json=payload
)
@jrd It doesn't have to be python. That's the point of a web API: the only thing you need is an url and the proper format for the payload. You can test it with postman or any other tool you like. You can send your requests with a python script, or a java application, or any other language.
You can even send your requests directly from a terminal with something like
curl -X POST -d '{"path": "tag path", "value": "some value"}' your_endpoint_url
Hi, jrd. Please see Keep it tidy on the forum FAQ. It has some regulations regarding signatures in forum posts.
Sorry @transistor
I simply replied to an email in my inbox
Shan't be doing that again!
Sorry guys, I remain confused. It is a state with which I am familiar !
I have not previously used WebDev. I have read the doco and the posts above, and I have not changed state! I remain confused.
I will try and state my needs a bit more clearly
I have an existing application written python. It is running on same server as Ignition Gateway. From time to time I would like this app to send notifications to Ignition. I would like these notifications to be text (likely with embedded integers). These notifications would then be displayed in certain pages in Perspective sessions. I do not require a response returned from Ig to the app (but that could be useful later)
I figure that the best way to do this communication would be via Ig tags, ie send a message (post?) to Ig, read the message in WebDev, write data to Ignition tag(s), then display on a Perspective page in appropriate component, label, table or whatever. I can manage that bit !!
So in my python app I do as suggested above, ie specify url, payload etc.
but this line fails:
import requests
where do I import 'requests' from?
Secondly, I don't understand why I would need
def doPost(....
On the Ignition side, wouldn't I be waiting for a "Post", from external and so use doGet?
Thanks and confused
jrd
ALL endpoints are waiting for incoming requests. The different methods are used for different things.
To make things simple, let's see the 4 main methods:
- get is used to ask the server for data.
- post is used to ask the server to add data
- delete is used to delete data
- put is used to update data
The method used by both the client and the server need to match. You can't catch a post with a get.
I suggest you dive a bit deeper and read some docs on APIs to understand why we use different methods, but the simple answer is: this allows to use the same url for different operations, and to put different access rights on each of them.
So, you want to change a tag value in ignition, from a python script.
In this case, ignition is the server, and the external script is the client.
The client will ask the server to do something, through a request on your configured endpoint (url + method).
Since you're asking ignition to change things on its side, you'll need either a post or a put method.
Which one you use doesn't really matter in the end, so I suggested post. This matches the intent of "sending notifications".
if you can't import requests
in your script, then you're not using a compatible version.
You can try urllib3
instead. Or you can tell us exactly what version of python you're using, and maybe with which interpreter - not all interpreters accept the same libraries. For example, ignition's python is actually Jython, built on Java, and as such cannot use libraries built for C python.
How long is this existing application in python? Less than a few hundred lines of code? If so, I would translate it to Ignition Jython.
it is 15+ modules each 150-20 lines
But size is not the issue.
The application uses a couple of essential libraries that do not run in Jython
Thanks
THANK YOU !! for the clarifications
I will try this in the next few days and advise
Thanks again !
Thanks sir
Can you advise what 'NameOfDoPostPage?' is in your reply?
Is this the name of the Web Dev resource?
And when I create this resource, I assume I create a Python resource?
Yes you'll need a python resource for this.
To get the url of your endpoint, right click on it in the project browser, and select "copy mounted path"
It will give you something that looks like this:
/system/webdev/{project_name}/{ressource_name}
Append this to your gateway's address to get the full url:
example with a local dev gateway, a webdev resource named 'foo' on a project named sandbox:
http://localhost:8088/system/webdev/sandbox/foo
Great, thank you. Progress... now I don't get a timeout in my Python application
But the Ig tag does not get written.
My Python (external app) code is:
import requests
url = 'http://localhost:8088/system/webdev/F3_Scheduler/writeTag'
payload = {
'path': "[Sample_Tags]F3/TextTag1",
'value': "my new value"
}
requests.post(
url=url,
json=payload
)ype or paste code here
and my Web Dev code:
type or paste code herdef doPost(request, session):
path = request['data']['path']
value = request['data']['value']
ret = system.tag.writeBlocking([path], [value])
return {'json': {'result': ret}}
What is the missing piece?
Add some logging to your WebDev code so you can see what you are getting for path
and value
.
Thank you to all who advised
It is now ok