7 Replies Latest reply on May 4, 2018 12:02 PM by don.guan

    PI webAPI and R integration

    don.guan

      I tried to use the sample code of the PI web API client lib for R (https://github.com/osimloeff/PI-Web-API-Client-R). Please clarify the syntax of the two highlighted lines - the correct way to define the PI AF server and a AF tag.  The error message is "Error: object 'piwebapi' not found". I installed the PI-R webapi package program correctly so I can get the data from your UC Davis data site to R.    

       

      piWebApiservice <- piwebapi$new("https://InternalPIserver.xxx.com/piwebapi", TRUE, NULL, NULL, FALSE, FALSE)

      BLR.GAS.DP <- piWebApiService$data$getRecordedValues(path =

                                         "af:\\\\Database1\\Totalizers|BLR.GAS.DP",

                                          startTime = "1-mar-2018", endTime = "*",

                                          selectedFields = "items.value;items.timestamp",

                                          maxCount = 50000)

      2.    How to set up the Authentication to get data from my PI system? I added the code below at the beginning of my R code, but I still got the same error "Error: object 'piwebapi' not found".

      useKerberos <- FALSE

      username <- "myusername"

      password <- "mypassword"

      validateSSL <- TRUE

      debug <- TRUE

      piWebApiService <- piwebapi$new("https://InternalPIserver.xxx.com/piwebapi", useKerberos, username, password, validateSSL, debug)

       

      Thank you.

       

      Don

        • Re: PI webAPI and R integration
          Lal_Babu_Shaik

          Hi Don Guan

           

          Could you please change the code as below

           

          useKerberos <- TRUE

          username <- "myusername"

          password <- "mypassword"

          validateSSL <- TRUE (If certificate is external)/FALSE(If it is selfsigned)

          debug <- TRUE

          • Re: PI webAPI and R integration
            gregor

            Hello Don,

             

            The URI (https://InternalPIserver.xxx.com/piwebapi) should be the base URI to your PI Web API installation. E.g. targeting our public endpoint, you would use https://devdata.osisoft.com/piwebapi

             

            I recommend you to try your PI Web API instance using a browser first. If you like to use Kerberos authentication, please use Internet Explorer 11. With other authentication methods (Basic or Anonymous), you could use any browser of your choice. I am expecting you may be experiencing difficulties already e.g. the self-signed certificate not being imported to the trusted root certificate store. Please feel encouraged to share your experience and you can as well try with our public endpoint. Its certificate is issued by a trusted authority and because of this you shouldn't run into any certificate issues with https://devdata.osisoft.com/piwebapi

              • Re: PI webAPI and R integration
                vkaufmann

                Some more background on internet browsers and how they behave with Kerberos. I also echo that things tend to more seamless when using IE 11 with kerberos delegation without any additional configuration needed.

                 

                --Vince

                  • Re: PI webAPI and R integration
                    don.guan

                    Thank you all.

                    It seems that I made some progress in first step after I changed "useKerberos <- TRUE"

                    piWebApiservice <- piwebapi$new("https://InternalPIserver.xxx.com/piwebapi", TRUE, NULL, NULL, FALSE, FALSE)

                    as I can see some parameters from the R window. I used both my real user name and password and just the general "myusername" and "myPassword". If I send the file to others, I don't want to have my real user name and password. How to avoid this in R?.

                    I still get the error "HTTP/1.1 401 Unauthorized" at the 2nd step. Should I add https://InternalPIserver.xxx.com/piwebapi

                    as a trusted site from IE? My IE already "Enable Integrated Windows Authentication". From R side, should I define this URL as a trusted site?

                     

                    Regards,

                     

                    Don

                      • Re: PI webAPI and R integration
                        don.guan

                        BTW, I am using RStudio. Does it support PI WebAPI? Should I use R.NET NuGet Gallery | R.NET 1.7.0 ?

                          • Re: PI webAPI and R integration
                            Lal_Babu_Shaik

                            Hi Don

                             

                            RStudio works with PI Web API and have tested the same. Request you to check PI Web API configuration in system explorer especially PI Web API --> System Configuration --> AuthenticationMenthods,CorsHeaders,CorsMethods,CorsOrigins,CorsSupportsCredentials and EnableCSRFDefense. Based on the attribute values you may face some issues while connecting from external source.

                              • Re: PI webAPI and R integration
                                don.guan

                                Thank you, all.

                                 

                                I made a few changes and it finally work!  Below is my full code. You can change the specific http link and the path for your PI system.

                                 

                                Should I keep the useKerberos - debug portion in the code? What is the best way to write the result from R to PI? Use StreamSet/UpdateValuesAdHoc function?

                                Do you have a tested full sample R code?

                                 

                                Regards,

                                 

                                Don

                                 

                                library (piwebapi)

                                library(lubridate)

                                library(ggplot2)

                                 

                                useKerberos <- TRUE

                                username <- "myusername"

                                password <- "myPassword"

                                validateSSL <- TRUE

                                debug <- TRUE

                                piWebApiService <- piwebapi$new("https://InternalPIserver.xxx.com/piwebapi", useKerberos, username, password, validateSSL, debug)

                                 

                                 

                                BLR.GAS.DP <- piWebApiService$data$getRecordedValues(path =

                                                                                       "af:\\\\PI_AFSERVER\\Database_1\\MAIN|BLR.GAS.DP",

                                                                                     startTime = "1-may-2018", endTime = "*",

                                                                                     selectedFields = "items.value;items.timestamp",

                                                                                     maxCount = 50000)

                                 

                                 

                                BLR.GAS.DP$timestamp <- as.POSIXct (BLR.GAS.DP$timestamp, "%Y-%m-%dT%H:%M:%S", tz = "")

                                 

                                ggplot(data =BLR.GAS.DP, aes(x = BLR.GAS.DP$timestamp, y = BLR.GAS.DP$value )) + geom_point()