Skip to content

Using REST API in Nodejs

Dear community members,

How can i use the REST API of plcnext in NodeJs to GET and PUT data's. can anyone show me an example of a get and put template. 

Thank you in advance,

with kind regards.

Comments

  • Hey,

    an application example can be found at the plcnext GitHub channel.

    We hope to release additional Examples and documentation in the next months.


    It should be roughly like this.
    Read:

    GET https://plc/_pxc_api/api/variables?pathPrefix=Arp.Plc.Eclr/&paths=SineWave3,SineWave

     

    Example Response with status code 200:

    {

      "apiVersion": "1.1.0.3",

      "projectCRC": 2373751656,

      "userAuthenticationRequired": true,

      "variables": [

        {

          "path": "Arp.Plc.Eclr/SineWave3",

          "value": -0.910105

        },

        {

          "path": "Arp.Plc.Eclr/SineWave",

          "value": -0.61786

        }

      ]

    }

    or write variables:

     

    PUT https://plc/_pxc_api/api/variables

     

    Content-Type: application/json

     

    With the accompanying request body:

    {

      "sessionID": "s1798554755",

      "pathPrefix": "Arp.Plc.Eclr/",

      "variables": [

        {

          "path": "BoolVar3",

          "value": "true",

          "valueType": "Constant"

        }

      ]

    }

     

    You will also have to open and maintain a session.
    A workaround might be creating a ehmi Page with the requested variables...

     

  • Dear Oliver,

    Thank you for your reaction, i have tried to implement what you send me in my nodejs server but it is not clear for me how to do that.please can you guide me how i can use the get and put methods in my Nodejs server. My intention is to get the status ..according to your example lets say 'SineWave3' i declared it as bool and variable usage as external..if status changes to true  i want to write the variable 'BoolVar3' to true and send it to PLC using put method. please look below my NodeJs server code and help me implement the put and get methods in it. 

    Thank you in advance.

    i have made my nodejs server as follows:

    var http = require('http');

    var http = require('http');

    var fs = require('fs');

    var server = http.createServer(function(req,res){ 

    console.log('request was made:' +req.url ); 

    res.writeHead(200,{''});   

    res.write(req.url);  res.end('')

    });
    server.listen(3000,'192.168.0.5');

    console.log('now you are listening to port 3000');

     

  • Hello beme,

    you can use the RestAPI however you prefere. 
    There exists a multitude of JS or nodeJS frameworks to choose from to access REST APIs.

    I am not going to suggest which to use or support these frameworks in this forum as our REST API is not bound to any programming language.

    You better look in a NodeJS Forum to find answers on these general topics, as this is not PLCnext Specific and you most likely can find sophisticated examples and answers to all your questions on the Web.

    kind regards,
    Oliver Warneke

  • Dear Oliver 

    Thank you for your answer, i was actually asking a very specific question about PLC-Next and its REST-API usage, because in our project we want to use plc next as a platform to run a javascrip code without starting a web browser, the possibility to do that is using NodeJs, but there is no any example on plcnext community regarding that point. My intention was to demonstrate the communication between Nodejs and plcnext REST-API without starting a web browser for the project group so that we can implement it in our project. if there is a simple example witch shows this kind of application, we would appreciate it very much.

    Thank you again for your cooperation.

    With kind regards.  

  • Hello,

    Here is an example of a node script that does not include a http server, and that makes a call to a public sample REST server:

    https://attacomsian.com/blog/http-requests-in-nodejs#2-request

    This is unrelated to PLCnext Technology, but requires knowledge and experience of Node programming, and how to access REST APIs from a Node program.

    Once you have this running on your PC, it is relatively simple to replace the calls to the third-party REST API, with calls to the PLCnext REST API that Oliver provided above.

    Martin.

  • Dear Martin,

    Thank you for you suggestion, i flowed the link explanation and made a server with a get request module, and i get this error message:

    root@axcf2152:/opt/plcnext/# node server
    Error: unable to verify the first certificate 

    Is there a guide how to  create a  certificate chain and where to store it in the plc?

    Thank you in advance

    with kind regards.

     

  • Hello beme,

    look at "https://github.com/request/request" to find hints how to add certificate paths.


    You can add your own certificates to the PLC via the Web Based Management.
    Have a look at the PLCnext Technology Handbook for further information.

    Or a simple aproach for starters:
    just add "rejectUnauthorized: false" to the request options

    kind regards,
    Oliver

  • Dear Oliver and Martin,

    Thanks to your help i can now write and read using Nodejs and the Rest-Api of Plcnext without starting the web browser, your guide was very helpful Thanks again for your cooperation. 

  • Hi beme , Could you help us to know how we can  control the axio I/O with CPU AXC F 2152

    using just nodejs and leave running the aplication on this PLC

    if is possible send us and example of code , we will apreciate beme , thanks

     

Sign In or Register to comment.