Data logging app for Android smartphones using PI Web API and Xamarin

Document created by fabiano.batista on Apr 1, 2016Last modified by fabiano.batista on Apr 3, 2016
Version 3Show Document
  • View in full screen mode

1. Overview

I took the first PI Web API class that was offered last year. All students who take the PI Web API training class (from PI Square) are required to develop a software application to apply the knowledge acquired during course. Since I was interested in start learning mobile programming, I found this a great opportunity to learn 2 different subjects in the same time: PI Web API and Android programming. But to avoid complicating too much my life (I have been busy with other activities at work), I decided to stick to C#, which is one of the languages I know the most. For this reason, I started playing with Xamarin, which came with Visual Studio 2015. It allowed me to develop a native Android application without having to switch to Java.


The purpose of this article is to share with you with my class final work, so that you can leverage it in your future projects.


2. Project Scope

This application allows users to log the cell phone signal strength (in dBm), radio-base station cell ID, network type (EDGE, UMTS, etc.), location area and geolocation (latitude, longitude and provider), every 5 seconds. The data being logged is sent to the PI System through PI Web API, and the data collection is represented by an event frame, which contains the attributes associated with the variables mentioned above. The user interface is very basic, not fancy at all, but enough to allow me to explore more the PI Web API product.


When I started this project (before Xamarin being acquired by Microsoft), Xamarin was not free for everyone. In fact there was a free version (the one that comes bundled with VS 2015), which was very limited. However, since I am a post-grad student, I was able to obtain a full license without any cost. Now, according to their website, "Xamarin will be in every edition of Visual Studio, including the widely-available Visual Studio Community Edition, which is free for individual developers, open source projects, academic research, education, and small professional teams".


3. High-Level Requirements

In order to be able to compile this application, you will need:

  • Visual Studio 2015 with Xamarin
  • Newtonsoft.Json
  • An Android telephone (version 5.0 or greater)
  • A PI System environment (PI Data Archive + PI AF) exposed through PI Web API. In order to be able to perform some external data collection, you will need a PI Web


I am using the device LG G3 (model LG-D852) with Android 5.0.1 (Lollipop). In order to be able to use it, I have to enable the “Developer Options” menu. Check here to learn how to do it.


4. Code

All the code that interacts with the PI System is placed in the class called PIWebAPIClient. In a multi-platform environment like Xamarin, this class can be shared among multiple platforms. In my case, I am just using it for the Android platform.

That class is responsible for writing the collected values to specific PI AF attributes, and to and create an event frame when the data collection starts. When the data collection is stopped, the event frame is closed.

The attributes names are specified as static variables in the PIWebAPIClient class, as follows:


const int REQUEST_TIMEOUT = 5000;
        const string attrOperator = "Operator Name";
        const string attrCID = "Cell ID";
        const string attrLAC = "Local Area Code";
        const string attrNetType = "Network Type";
        const string attrSignalStrength = "Signal Strength";
        const string attrLatitude = "Latitude";
        const string attrLongitude = "Longitude";
        const string attrProvider = "Location Provider";


You need to create an AF element with the attribute names specified above, and a EF template with a similar set of attributes.

The AF connection info, EF template name and other configuration parameters are specified in the MainActivity.cs file:



//Since I was not able to create tags on the internet PI Data Archive, I ended up using my local VM for testing
        //However, the GPS related tests are limited, because the wifi range is limited to indoors only.
        const string _PIWebAPIBaseUrl = "";

        //PIAF/Data Archive connection information
        //PI Web API is set to Basic Authentication
        const string _PIAFDBName = "CellSignalStrength";        //AF Database name
        const string _EFTemplateName = "MobileDriveTest";       //AF Template name
        //const string _PIAFServer = "JUPITER001";
        const string _PIAFServer = "FBAT-LOCALSRV";             //AF server name
        const string _AssetRelativePath = "LG-852";             //Asset path relative to AF Database o the element that will 
                                                                //be added as primary reference of the event frame
        const string _UserName = @"fbat-localsrv\PIUser";       //Windows user name with proper permissions to create the event frames
        const string _Password = "User1234";                    //Passsword ** Caution: PI Web API must be configured to accept basic authentication

        const int _ScanRate = 5000;                             //Data collection rate (in miliseconds)


I invite you to browse the code to view my comments and to understand the general program behavior, specially to see how the interaction between the mobile app and the PI System takes place via the PI Web API layer. The entire project is attached. Enjoy!

The code is available as is, and was built for training purposes only. You are free to use it in your projects, after careful review.

5. Final Consideration


If you have any question about this project, feel free to contact me. You may consider taking the Programming with PI Web API class available on PI Square, if you are just beginning with this technology.

By the way, special thanks to Daphne Ng for putting together the PI Web API training.