In this tutorial we will provide you with a step by step guide to enable pushing and plotting data from Wearable Sensor System (Shimmer)


Before starting

Required:

Assumptions:


Step by step Guide

Step 1: Configure your device and related device and date models on Labeeb IoT platform

 


First you have to define a Data Type for your sensor: navigate to "Data Type" sub-menu under "Platform Management" and click on + to add all the needed Data types as shown below.


First, you have to create a Data Model for your sensor: navigate to "Data Models" sub-menu under "Platform Management" and click on + to add all the needed Data Models as shown below.

Second, attach the data types  to your Data Model, then set the accuracy profile. For example, if you set the accuracy level to 90, the time period to 1 second and the action Mark Data Model as Malfunctioning then the received data belonging to this Data Model should pass all the filters for 90% of the packets over a period of 1 second. Otherwise, the Data Model will be marked as Malfunctioning.

Finally, click on "Save" to make sure your new data model is created.



Once the Data Model is created, you have to define a Device Template: navigate to "Device Template" sub-menu under "Platform Management" and click on + to add the template as shown below:
Second, set the Device Heartbeat profile by defining the number of expected packets over the defined Time Period.
Third, you will have to define the password you will use to authorize a device to send the data to your account. This password will be used with the generated login
Fourth, add the data model that you created in previous step.

Finally, click on "Save" to make sure your new device template is created.


Now, once the new device template has been created with data model, you just need to create a new device: navigate to "Device" menu and click on + to add the new device as shown below:

Define the name of your new device and Under "Device Template" you have toe select the previously created device template:


Finally, click on "Save" to make sure your new device is created. Now your new device is now successfully created and listed in my list of devices.

You will have to use the generated MQTT topic and the login password you have set when creating the Device Template in order to push data from this device over MQTT.


You can check the device status and the data statistics under "My Account" menu as shown below:

 


Step 2: Create and configure a new Android Application


Prerequisites:

  • ShimmerDriver project (to be added as a library)
  • The Labeeb IoT Java SDK (JAR file) should be copied to a local folder

Development Steps:

  • Under Eclipse, create a new Android application Project and click on Next, as shown below:

 

 

  • Define a project name, named "LabeebIoTShimmer", and click on Finish, as shown below:

 

 

  • Once the project is created, right click --> Properties –> Java Build --> Libraries, click on "Add External Jars", locate the Labeeb IoT SDK JAR file (labeeb-iot-sdk-jar-with-dependencies.jar), and click on Open.

 

 

  • Select project : right click --> Properties –> Android --> add Libraries, click on "Add", locate the ShimmerAndroidInstrumentDriver and click on OK.


  • You must define a shimmer object and explicitly define sensor to be used like : SENSOR_ACCEL for Accelerometer and SENSOR_GYRO for Gyrometer 

shimmerDevice = new Shimmer(this, mHandler,"RightArm", 51.2, 0, 0, Shimmer.SENSOR_ACCEL|Shimmer.SENSOR_GYRO|Shimmer.SENSOR_MAG|Shimmer.SENSOR_GSR|Shimmer.SENSOR_BATT|Shimmer.SENSOR_BMP180, false); 
shimmerDevice.enableOnTheFlyGyroCal(true, 102, 1.2);
shimmerDevice.enable3DOrientation(true);// reduce the number of range labels

 

  • You must also define a handler object to to enable sensor data management

 

	 private final Handler mHandler = new Handler() {
	        public void handleMessage(Message msg) {
	 	            switch (msg.what) { // handlers have a what identifier which is used to identify the type of msg
	 	            case Shimmer.MESSAGE_READ:
	 	            	
	 	            	if ((msg.obj instanceof ObjectCluster)){	// within each msg an object can be include, objectclusters are used to represent the data structure of the shimmer device
	 	            		storeValue = new HashMap<String, Double>();
	 	            	    ObjectCluster objectCluster =  (ObjectCluster) msg.obj; 
	 	            	    if (objectCluster.mMyName=="RightArm"){
	 	            	    	
		                	    Collection<FormatCluster> accelXFormats = objectCluster.mPropertyCluster.get("Axis Angle A");  // first retrieve all the possible formats for the current sensor device
					 	    	float angle = 0,x = 0,y=0,z=0;
		                	    if (accelXFormats != null){
					 	    		FormatCluster formatCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(accelXFormats,"CAL")); // retrieve the calibrated data
					 	    		angle = (float) formatCluster.mData;
					 	    	}
					 	    	Collection<FormatCluster> accelYFormats = objectCluster.mPropertyCluster.get("Axis Angle X");  // first retrieve all the possible formats for the current sensor device
					 	    	if (accelYFormats != null){
					 	    		FormatCluster formatCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(accelYFormats,"CAL")); // retrieve the calibrated data
					 	    		x=(float) formatCluster.mData;
					 	    	}
					 	    	Collection<FormatCluster> accelZFormats = objectCluster.mPropertyCluster.get("Axis Angle Y");  // first retrieve all the possible formats for the current sensor device
					 	    	if (accelZFormats != null){
					 	    		FormatCluster formatCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(accelZFormats,"CAL")); // retrieve the calibrated data
					 	    		y=(float) formatCluster.mData;
					 	    	}
					 	    	Collection<FormatCluster> aaFormats = objectCluster.mPropertyCluster.get("Axis Angle Z");  // first retrieve all the possible formats for the current sensor device
					 	    	if (aaFormats != null){
					 	    		FormatCluster formatCluster = ((FormatCluster)ObjectCluster.returnFormatCluster(aaFormats,"CAL")); // retrieve the calibrated data
					 	    		z=(float) formatCluster.mData;
					 	    		final AxisAngle4d aa=new AxisAngle4d(x,y,z,angle);
					 	    		final Quat4d qt = new Quat4d();
					 	    		qt.set(aa);
					 	    		if(toggleAxis.isChecked()){
						 	    		mA.setText(Double.toString(round(aa.angle*180/Math.PI, 3, BigDecimal.ROUND_HALF_UP)));
						 	    		//pushData("XYZPosition","Angle",round(aa.angle*180/Math.PI, 3, BigDecimal.ROUND_HALF_UP));
										storeValue.put("XYZPosition-Angle", round(aa.angle*180/Math.PI, 3, BigDecimal.ROUND_HALF_UP));
						 	    		mX.setText(Double.toString(round(qt.x, 3, BigDecimal.ROUND_HALF_UP)));
						 	    		
								 	    //pushData("XYZPosition","XPosition",round(qt.x, 3, BigDecimal.ROUND_HALF_UP));
										storeValue.put("XYZPosition-XPosition", round(qt.x, 3, BigDecimal.ROUND_HALF_UP));
						 	    		mY.setText(Double.toString(round(qt.y, 3, BigDecimal.ROUND_HALF_UP)));
						 	    		
								 	    //pushData("XYZPosition","YPosition",round(qt.y, 3, BigDecimal.ROUND_HALF_UP));
										
						 	    		storeValue.put("XYZPosition-YPosition", round(qt.y, 3, BigDecimal.ROUND_HALF_UP));
						 	    		//pushData("XYZPosition","ZPosition",round(qt.z, 3, BigDecimal.ROUND_HALF_UP));
										
						 	    		storeValue.put("XYZPosition-ZPosition", round(qt.z, 3, BigDecimal.ROUND_HALF_UP));
						 	    		mZ.setText(Double.toString(round(qt.z, 3, BigDecimal.ROUND_HALF_UP)));	
					 	    		}
					 	    	}
							}
						}
					}
 

 

 



		class IoTDevice implements DeviceListener {
		    
		    public IoTDevice() throws IoTConnectionException{
		        // connect to the remote Labeeb IoT Platform
		        con = new Device("shimmer", "ShimmerDevice","shimmer1", "mea.labeeb-iot.com","shimmer");
		        con.setSsl(true);
		        con.connect();
		    }
		     
		    public void start() throws IoTException, IoTConnectionException, InterruptedException {
	            // create a data record
	            long endTime = System.currentTimeMillis();
		    	if(endTime - 1000 > startTime){ // sending data every 1 second
		    		if(storeValue!=null && storeValue.size()>0 ){
		    			Set<String> keys = storeValue.keySet();
		    			for(String key:keys){
		    				Double value = storeValue.get(key);
		    				DataRecord data = new DataRecord(key.split("-")[0], key.split("-")[1], value);
				            // push the data record to Labeeb IoT
				            try {
				            		con.pushData(data);
				            		startTime = System.currentTimeMillis();
				            	}catch (IoTMessagingException e) {
				            		if(e.getReasonCode() == 32104)
				            			con.connect();
				            	}
			    		}
		    				
		    		}
		    		storeValue = new HashMap<String, Double>();		    			
		    	}
	             
		    }
		 
		    @Override
		    public void onRequestReceived(Request rqst) {
		        throw new UnsupportedOperationException("Not supported yet.");
		    }
		    @Override
		    public void onConnectionLost(Throwable thrwbl) {
		        throw new UnsupportedOperationException("Not supported yet.");
		    }
		     
		}
		


	private void pushData() {
		try {
			device.start();
		} catch (IoTConnectionException e) {
			Log.e("error", e.getMessage());
		} catch (IoTException e) {
			Log.e("error", e.getMessage());
		} catch (InterruptedException e) {
			Log.e("error", e.getMessage());
		}
	}



Source code and Apk :

LabeebIoTShimmer.apk

LabeebIoTShimmer.rar