Smartphones, Tablets, and Mobile Devices Documents

cancel
Showing results for 
Search instead for 
Did you mean: 

Building Android Applications for Simple Data Monitoring

Introduction

This example shows you how to build a native Android application for simple data monitoring.

emulator SCC.png

Application Architecture

androidflow.jpg

    1. Measurement Application - Performs your measurement and publishes the latest data value to a variable.
    2. LabVIEW Web Service - Retrieves the latest data value in the variable and returns it.
    3. Android Application - Periodically calls the web service and plots the latest data value. (Note: the Android application may miss data points if the Measurement Application is sampling faster than the Android Application calls the web service.)

Download Example Code

Simple Data Monitor (LabVIEW Project).zip

Simple Data Monitor (Android Application).zip

Tutorial

Part 1: Measurement Application

LabVIEW VI's can share data between other VI's using shared variables. In this example, we perform a single point acquisition, plot the point to a chart, and publish it to the shared variable.

daq_assist.jpg

Part 2: LabVIEW Web Services

LabVIEW Web Services allow any web capable device to access data from your application through any web interface. Your measurement application continually runs in parallel with your web service VI. When the web service VI is called by a web client, it reads the value from the shared variable, generates an XML string, and writes a HTTP response with the data back to the web client.

web_service1.jpg

After you create your web service VI, you must deploy it within the LabVIEW project.

deploy.jpg

It can then be invoked via URL web request.

web_request.jpg

Additional Resources:

Web Services in LabVIEW

Web Services FAQ

Part 3: Android Application

Getting Started with Android

Download the SDK

Hello World Tutorial

Developer's Guide

This code snippet contains the core functionality. It first sets up a connection to the URL, configures the XML parser, reads and parses the XML from the input stream, adds the data value to an array of all data values received, and plots the array of values on the chart.

while (!mStop)
{

// First, open a connection to the machine where LabVIEW is running

URL url = new URL(mURLText.getText().toString());
URLConnection connection = url.openConnection();

// Set resonable timeouts
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

// Create an XML document builder with the default settings
DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

// Parse XML from the web service into a DOM tree
Document doc = docbuilder.parse(connection.getInputStream());

// Now, pull out the value attribute of the first channel element
String value = doc.getElementsByTagName("channel")
                     .item(0)
                     .getAttributes()
                     .getNamedItem("value")
                     .getNodeValue();

// Create an array of data to add to the chart. There is only one
// channel of data right now, so just add one point.
ArrayList<Double> data = new ArrayList<Double>();
data.add(Double.parseDouble(value));
// Add the data to the chart.
mChart.addDataPoint(data);
}
Regards,

Chris Delvizis
National Instruments
Comments
Member Arvont
Member

This example may not work on Windows XP.  After you deploy the Web Service from the LabVIEW project you may get a message that says it was deployed successfully, but if you look in Distributed System Manager 2009, the service won't be running.  This Knowledge Base article gives some insight.

http://digital.ni.com/public.nsf/allkb/6063AFA6E5D95EAB86257792006082D4

I still couldn't get it to work on XP with LV 2009, so I switched to Vista and LabVIEW 8.6 and finally got it to work.

On the android side, prepare to spend an hour or two getting your environment setup and run the "hello, world" tutorial to make sure you have everything setup correctly.

Member bartoon
Member

Had some problems, typing the URL is quite tricky. You have to type it like this http://XXX.XXX.XXX.XXX:8080/MobileAPI/SampleTemp. The XXX.XXX.XXX.XXX is your IP. Also you have to solve the problem with the comma to make it work in the CreateXML.vi. Use the dot.

Member Burzredion
Member

I can´t deploy it, I'm using LabVIEW 2010 32bits, Win7 64bits and USB-6009. Error message: "Web Services Deployment Failed. You must start the NI Application Web Server before deploying a web service." but NI Application Web Server is started. Some suggestions?

Chris_D
NI Employee

Hey Burzedion, check out this thread on the NI discussion forums: http://forums.ni.com/t5/LabVIEW/Web-Service-deployment-failed/m-p/1736702?requireLogin=False. It has a few suggestions you can try.

Regards,

Chris Delvizis
National Instruments
Member Burzredion
Member

Hi Chris, thanks for your help. I tried to repair LabVIEW 2010 and still having the same problem. I don't know how to subscribe NI Application Web Server. Some others suggestions? Thanks.

Chris_D
NI Employee

Did you try some of the other suggestions listed on the thread? It may be worth posting details about your issue on the thread so an NI applications engineer can give suggestions.

Regards,

Chris Delvizis
National Instruments
Active Participant D60
Active Participant

Hello!

I have only one problem. When I want to connect to WebService I got an error on my phone:

java.net.SocketTimeoutException: Connection Timed out

Do you have any idea?

---
+++ In God we believe, in Trance we Trust +++
[Hungary]
Chris_D
NI Employee

It sounds like you have a networking issue. Try typing the URL into your web browser and see if the web service responds. Are you sure the URL and IP Address is correct?

Regards,

Chris Delvizis
National Instruments
Member arithmetic
Member

Hi, I have a problem with the Webservice.

When I click to run, the labview throws this message: "Error -67301 occurred at NI_WebServices.lvlib:Error Converter (ErrCode or Status).vi"

Do somebody have any idea? The httprequestID's value is 0, its good?

Thanks for help.

Chris_D
NI Employee

Here is a list of error code: http://zone.ni.com/reference/en-XX/help/371361J-01/lverror/web_services_error_codes/. Have you deployed the web service?

Regards,

Chris Delvizis
National Instruments
Member loops
Member

How to slow down the speed that android application calls webservice?

edited*

i knew the answer after post this comment. Thank you

Member mayttr
Member

i have that error when i type URL

Access Error: 404 -- Not Found

Cannot open document for: /SmartPhone/SampleTemp
Member arya1
Member

Hi Chris

Thanks for the code. I have an application which would require me to send around 1000 points every 10 to 20 ms to display in a chart in my phone. With web services, what could be the maximum rate of data transfer?

Thanks

Arya

Member welcomeyoulyl
Member

我想知道这个程序该如何改改呢?谢谢!

JoeySpinella
NI Employee

你想改Android程序或者LabVIEW Web Service吗?如果你想学怎么改Android的程序,我推荐你上Android提供的资源:http://developer.android.com/training/basics/firstapp/starting-activity.html

Joey S.
Senior Product Manager, Software
National Instruments
Member welcomeyoulyl
Member

Thank you for your reply。我出现的问题和arithmetic一样,LabVIEW:  (Hex 0xFFFEF91B) LabVIEW Web Services: Invalid Request ID.不知道是什么问题?

Member irfan.aloha
Member

I can´t deploy it, with Error message: "Web Services Deployment Failed". How can I go ahead

Contributors