In the first blog post of this Iot blog post series we've prepared the Raspberry Pi for the upcoming blog posts.
In this blog post we'll create a Java app on your free developer account on the SAP HANA Cloud Platform that will receive the sensor values from the Raspberry Pi.
Once this is up and running, the Java app on the Raspberry Pi will be "a piece of cake" :-)
NOTE AND DISCLAIMER
Please be aware that this blog post will not provide you with code that you should use productively. It actually is focusing on getting an app up-and-running for demo purposes.
So take this whole blog post series and its sample code AS IS and with the purpose it was build: to inspire you to think about other IoT (Internet of Things) scenarios you could run on the SAP HANA Cloud Platform and NOT as a best practice for productive environments.
And yes: there ARE other (maybe more elegant/funky/techie/cool) ways to tackle this special exercise (e.g. using HANA XS instead of the Java app, or using REST instead of using a Java Servlet, or using Maven, or ... , or... , or ...). But I've choosen this way as I've thought this is the best and easiest way to get started. Feel free to share in the community other ways to do this.
So let's get started.
Table of Contents
The Business Scenario: Rui's Fish Import/Export Inc.
In our scenario we'll assume we are in the Fish business and transport fish from one location to the other. As the fish we are transporting is very valuable we want to be able to track the temperature inside the fish boxes remotely.
For that purpose we have installed inside our truck a Raspberry Pi that can send data to the internet. In addition to that we have connected 4 temperature sensors to the RaspberryPi that we'll use to determine the temperature inside the fish boxes. Those temperature values will be sent every minute to your free developer account on the SAP HANA Cloud Platform.
The applications we'll need
Our solution will have in its center an SAP HANA Cloud Platform account. We'll use an account under the free developer license.
The software solution well build will have three building blocks.
Java app on the Raspberry Pi
Firstly we'll provide our Raspberry Pi with a Java app. That app will send an http request to a Java app on the SAP HANA Cloud Platform and send sensor data to it.
Java app on SAP HANA Cloud Platform account
Secondly we'll have a Java app on your account on the SAP HANA Cloud Platform that will be the receiving the data from your Raspberry Pi. That app will also persist the data on the HANA instance of your account.
The Java app will also be able to create a JSON output with the sensor data so it can be consumed by the SAPUI5 application we have.
SAPUI5 app on SAP HANA Cloud Platform
The SAPUI5 app will provide us with a dashboard showing the current temperature of the fish boxes in the truck. That application will consume the JSON output from the Java app on the SAP HANA Cloud Platform account.
Get a free developer account for SAP HANA Cloud Platform
As a developer you can get a free developer account under a developer license. You register on https://hanatrial.ondemand.com and after activating your account via an email you get access to the cockpit of your developer account (https://account.hanatrial.ondemand.com/cockpit).
Setup your development environment
You'll need to install Eclipse IDE for Java EE Developers as well as the SAP HANA Cloud Platform Tools and the SAP HANA Tools Eclipse plugins.
In a last step you also need to download the Java Web SAP HANA Cloud Platform SDK and unzip it on your drive. I've used for this blog post the SDK version 1.65.10.
To prepare your project you should also already add a new server under the Servers view for deployment on your localhost.
All of these steps are drilled-down in detail on a tutorial at the hcp.sap.com website. Just follow the steps there and you are all set for the next steps.
Deploy the Java app on your SAP HANA Cloud Platform account
The Java app on your account will be a Dynamic Web Project. So go ahead and create such a project in your Eclipse.
To do that switch to your Java EE perspective (Window > Open Perspective > Other > Java EE) and create such a project (File > New > Dynamic Web Project). Ensure your screen looks like the screenshot below.
Meaning you have selected the correct Target runtime (Java Web) as well as the right Dynamic web module version (2.5) and the right Configuration (Default Configuration for Java Web).
Getting the libs
Now you need to get a couple of libraries first to use the app. Three of them can be found in the Java Web SDK of the SAP HANA Cloud Platform (1.xx.yy). They are called eclipselink-xxx.jar, javax.persistence-yyy and com.sap.security.core.server.csi_1.0.1.jar. You can find all three files in one of the samples in the SDK folder under samples > persistence-with-jpa > WebContent > WEB-INF > lib. In the SDK version I've used (1.65.10) the files are called eclipselink-2.5.1.jar, javax.persistence-2.1.0.jar and com.sap.security.core.server.csi_1.0.1.jar. Copy all three files into the lib folder of your Eclipse project (WebContent > WEB-INF > lib).
The other library you need is the GSON library from Google, which helps us generate JSON output quite elegantly. You can get it here. You also need to copy that jar file into the lib folder of your Eclipse project (WebContent > WEB-INF > lib).
Setup project facets
We'll use JPA in our project hence we need to activate the corresponding project facet in our project. Simply right-click on your project in the Eclipse project explorer and select Build path > Configure Build Path. After that click on the Project Facets entry on the left side of the window and activate on the right the JPA project facet in version 2.0.
Once you click on it you'll also notice at the bottom of the window a link called "Further configuration available". Click on that link and configure the facet as shown in the screenshot below.
Add the source code
We'll need in total four Java classes. One is the servlet, the other one a helper class for the persistence and two other classes containing the data model of our app.
The two persistence classes
Click on the folder of your project that is called Java Resources, right-click on it and select New > Class. In the Package field enter the value org.persistence and in the filed Name you need to enter Measurement. Once entered, click on Finish.
Now do the same for the class Sensor. In the Package field enter also the value org.persistence and in the filed Name you need to enter Sensor. Once entered, click on Finish.
Substitute the generated code with the following code.
Code for Measurement.java file
package org.persistence;
import static javax.persistence.GenerationType.AUTO;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries({
@NamedQuery(name = "AllMeasurements", query = "select m from Measurement m"),
@NamedQuery(name = "LastSensorReading", query = "select m from Measurement m where m.sensorId = :paramSensorId and m.storedAt = (SELECT MAX(r.storedAt) from Measurement r where r.sensorId = :paramSensorId)"),
@NamedQuery(name = "LastReadingsFromSensor", query = "select p from Measurement p where p.sensorId = :paramSensorId order by p.storedAt DESC") })
public class Measurement implements Serializable {
private static final long serialVersionUID = 1L;
public Measurement() {
}
@Id
@GeneratedValue(strategy = AUTO)
private Long id;
private String unit;
private Timestamp storedAt;
private Double value;
private long sensorId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public Timestamp getStoredAt() {
return storedAt;
}
public void setStoredAt(Timestamp dateStored) {
this.storedAt = dateStored;
}
public Double getValue() {
return value;
}
public void setValue(Double sensorValue) {
this.value = sensorValue;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public long getSensorId() {
return sensorId;
}
public void setSensorId(long param) {
this.sensorId = param;
}
}
Code for Sensor.java file
package org.persistence;
import java.io.Serializable;
import javax.persistence.*;
import org.persistence.Measurement;
import java.util.Collection;
import static javax.persistence.GenerationType.AUTO;
@Entity
@NamedQueries({ @NamedQuery(name = "GetListOfSensors", query = "select s from Sensor s") })
public class Sensor implements Serializable {
private static final long serialVersionUID = 1L;
public Sensor() {
}
@Id
@GeneratedValue(strategy = AUTO)
private long id;
private String device;
private String type;
private String description;
private Measurement lastMeasurement;
@OneToMany
private Collection<Measurement> measurements;
public Measurement getLastMeasurement() {
return lastMeasurement;
}
public void setLastMeasurement(Measurement lastMeasurement) {
this.lastMeasurement = lastMeasurement;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDevice() {
return device;
}
public void setDevice(String param) {
this.device = param;
}
public String getType() {
return type;
}
public void setType(String param) {
this.type = param;
}
public String getDescription() {
return description;
}
public void setDescription(String param) {
this.description = param;
}
public Collection<Measurement> getMeasurement() {
return measurements;
}
public void setMeasurement(Collection<Measurement> param) {
this.measurements = param;
}
}
Finally you need to update the persistence.xml file so that you add a few new lines to register the two persistence classes to it.
Open the persistence.xml file (Java Resources > src > META-INF) by right-clicking on the persistence.xml file and click on Open With > Text Editor.
Between the persistence-unit tag you need to have 6 additional lines added which are marked bold below.
Be aware to only add the 6 new lines, because the file contains the name of your project in the persistence-unit under the name property.
So your persistence.xml file looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="iotscenario">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>org.persistence.Measurement</class>
<class>org.persistence.Sensor</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
The SensorsServlet
The SensorsServlet is the servlet that will receive the data and also take care to create corresponding JSON output.
Create a servlet called SensorsServlet. To do that click on the folder of your project that is called Java Resources, right-click on it and select New > Servlet. As Java package enter the value com.sap.iot.sensors and as Class name you enter SensorsServlet. Once entered click on Finish.
Now just replace the code you have there with the following code:
package com.sap.iot.sensors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.persistence.Measurement;
import org.persistence.Sensor;
import com.google.gson.Gson;
import com.sap.security.core.server.csi.IXSSEncoder;
import com.sap.security.core.server.csi.XSSEncoder;
/**
* Servlet implementation class SensorsServlet
*/
public class SensorsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
private EntityManagerFactory emf;
// Number of sensor readings that should be sent as response
private static final int MAX_NUMBER_SENSOR_READINGS = 10;
/**
* @see HttpServlet#HttpServlet()
*/
public SensorsServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = encodeText(request.getParameter("action"));
DataHelper dataHelper = new DataHelper(emf);
// Add a sensor
if (action != null && action.equalsIgnoreCase("addsensor")) {
Sensor sensor = extractSensorData(request);
dataHelper.addSensor(sensor);
}
// Add a sensor value
if (action != null && action.equalsIgnoreCase("addsensorvalue")) {
Measurement measurement = extractMeasurementData(request);
dataHelper.addMeasurement(measurement);
}
// Provide a JSON output of all sensor values (measurements)
if (action != null && action.equalsIgnoreCase("showallmeasurements")) {
List<Measurement> sensorMeasurements = dataHelper.getAllSensorReadings();
outputJsonForAllMeasurements(response, sensorMeasurements);
}
// If no action parameter is provided simply print out the sensor data
// as JSON
if (action == null) {
List<Sensor> sensors = dataHelper.getListOfSensors();
// Step into each sensor and add the related measurements to it
for (int i = 0; i < sensors.size(); i++) {
Sensor sensor = sensors.get(i);
List<Measurement> sensorMeasurements = dataHelper.getLastSensorReadings(sensor.getId(), MAX_NUMBER_SENSOR_READINGS);
sensor.setMeasurement(sensorMeasurements);
Measurement sensorLastMeasurement = dataHelper.getLastSensorReading(sensor.getId());
sensor.setLastMeasurement(sensorLastMeasurement);
sensors.set(i, sensor);
}
outputJsonAllData(response, sensors);
}
}
/*
* Creates a JSON output of all sensor values (measurements)
*
* @param response The HTTP-response object
*
* @param sensorMeasurements The list of sensors values (measurements)
*/
private void outputJsonForAllMeasurements(HttpServletResponse response, List<Measurement> sensorMeasurements) {
Gson gson = new Gson();
try {
response.getWriter().println("{");
for (int i = 0; i < sensorMeasurements.size(); i++) {
response.getWriter().println(gson.toJson(sensorMeasurements.get(i)));
if (i != sensorMeasurements.size() - 1) {
response.getWriter().println(",");
}
}
response.getWriter().println("}");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Creates a JSON output out of the provided list of sensors with their
* corresponding sensor values (measurements)
*
* @param response The HTTP-response object
*
* @param sensors The list of sensors with their respective measurements
*/
private void outputJsonAllData(HttpServletResponse response, List<Sensor> sensors) {
Gson gson = new Gson();
try {
response.getWriter().println("{");
for (int i = 0; i < sensors.size(); i++) {
response.getWriter().println('"' + "sensor" + i + '"' + ":");
response.getWriter().print(gson.toJson(sensors.get(i)));
if (i != sensors.size() - 1) {
response.getWriter().println(",");
}
}
response.getWriter().println("}");
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* Extracts a Sensor object out of the parameters provided in the
* HTTP-request
*
* @param request The HTTP-request object
*
* @return The derived Sensor object
*/
private Sensor extractSensorData(HttpServletRequest request) {
Sensor sensor = new Sensor();
String idString = encodeText(request.getParameter("id"));
if (idString != null && idString.length() > 0) {
sensor.setId(Long.parseLong(idString));
}
sensor.setType(encodeText(request.getParameter("type")));
sensor.setDevice(encodeText(request.getParameter("device")));
sensor.setDescription(encodeText(request.getParameter("description")));
return sensor;
}
/*
* Extracts a Measurement object (sensor values) out of the parameters
* provided in the HTTP-request
*
* @param request The HTTP-request object
*
* @return The derived Measurement object
*/
private Measurement extractMeasurementData(HttpServletRequest request) {
Measurement measurement = new Measurement();
// Get sensorId
String sensorIdString = encodeText(request.getParameter("sensorid"));
if (sensorIdString != null && sensorIdString.length() > 0) {
measurement.setSensorId(Long.parseLong(sensorIdString));
}
// Get unit of measured value
measurement.setUnit(encodeText(request.getParameter("unit")));
// Get measured value and calculate the value to be stored
String sensorValue = encodeText(request.getParameter("sensorvalue"));
String sensorValueMultiplier = encodeText(request.getParameter("sensorvaluemultiplier"));
String sensorValueCalibration = encodeText(request.getParameter("sensorvaluecalibration"));
if (sensorValueCalibration != null && sensorValueCalibration.length() > 0 && sensorValue != null && sensorValueMultiplier != null && sensorValueMultiplier.length() > 0
&& sensorValue.length() > 0) {
measurement.setStoredAt(new Timestamp(new Date().getTime()));
Double valueDouble = Double.parseDouble(sensorValue);
Double multiplierDouble = Double.parseDouble(sensorValueMultiplier);
Double valueGap = Double.parseDouble(sensorValueCalibration);
Double value = (valueDouble * multiplierDouble) + valueGap;
measurement.setValue(value);
}
return measurement;
}
/*
* Encodes a text to avoid cross-site-scripting vulnerability
*
* @param request The text to be encoded
*
* @return The encoded String
*/
private String encodeText(String text) {
String result = null;
IXSSEncoder xssEncoder = XSSEncoder.getInstance();
try {
result = (String) xssEncoder.encodeURL(text);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
/** {@inheritDoc} */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void init() throws ServletException {
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDB");
Map properties = new HashMap();
properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);
emf = Persistence.createEntityManagerFactory("iotscenario", properties);
} catch (NamingException e) {
throw new ServletException(e);
}
}
/** {@inheritDoc} */
@Override
public void destroy() {
emf.close();
}
}
Please be sure to adapt the init() method so that
emf = Persistence.createEntityManagerFactory("iotscenario", properties);
has the same name like your project. It needs to be the same like defined in the persistence-unit entry of your persistence.xml file (under <persistence-unit name="iotscenario">).
The DataHelper class
This class will help us with some methods to persist data.
Create a classe called DataHelper. To do that click on the folder of your project that is called Java Resources, right-click on it and select New > Class. As Java package enter the value com.sap.iot.sensors and as Class name you enter DataHelper. Once entered click on Finish.
Now just replace the code you have there with the following code:
package com.sap.iot.sensors;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.persistence.Measurement;
import org.persistence.Sensor;
public class DataHelper {
private EntityManagerFactory emf;
public DataHelper(EntityManagerFactory emf) {
this.emf = emf;
}
/*
* Persists a measured sensor value (measurement)
*
* @param measurement The measured sensor value
*/
public boolean addMeasurement(Measurement measurement) {
boolean result = false;
EntityManager em = emf.createEntityManager();
// System.out.println("Trying to commit sensor data for sensor " +
// measurement.getSensorDescription());
try {
if (measurement != null && measurement.getValue() != null) {
em.getTransaction().begin();
em.persist(measurement);
em.getTransaction().commit();
}
} catch (Exception e) {
System.out.println("ERROR: persisting measurement didn't work " + e.getMessage());
} finally {
em.close();
}
return result;
}
/*
* Persists a new sensor
*
* @param sensor The sensor object to be added
*/
public boolean addSensor(Sensor sensor) {
boolean result = false;
if (sensor != null) {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
em.persist(sensor);
em.getTransaction().commit();
result = true;
} catch (Exception e) {
System.out.println("ERROR: persisting sensor didn't work " + e.getMessage());
result = false;
}
em.close();
}
return result;
}
/*
* Provides a list of a defined number of sensor readings for a specific
* sensor. The method will provide the newest sensor readings (measurements)
* first
*
* @param sensorId The sensor id of the sensor that you wish to get the
* measured values from
*
* @param numberOfReadings The maximum number of readings you'll get back
*/
@SuppressWarnings("unchecked")
public List<Measurement> getLastSensorReadings(long sensorId, int numberOfReadings) {
List<Measurement> result = null;
EntityManager em = emf.createEntityManager();
try {
Query q = em.createNamedQuery("LastReadingsFromSensor");
q.setParameter("paramSensorId", sensorId);
// To not affect performance we just retrieve the first 20 result
// sets
q.setMaxResults(numberOfReadings);
result = q.getResultList();
Collections.sort(result, new Comparator<Measurement>() {
public int compare(Measurement m1, Measurement m2) {
return m1.getStoredAt().compareTo(m2.getStoredAt());
}
});
} catch (Exception e) {
}
em.close();
return result;
}
/*
* Provides a list of ALL sensor readings. To avoid too many data the output
* is restricted to a maximum of 500 entries
*/
@SuppressWarnings("unchecked")
public List<Measurement> getAllSensorReadings() {
List<Measurement> result = null;
EntityManager em = emf.createEntityManager();
try {
Query q = em.createNamedQuery("AllMeasurements");
q.setMaxResults(500);
result = q.getResultList();
} catch (Exception e) {
}
em.close();
return result;
}
/*
* Provides the last measured sensor value for a sensor
*
* @param sensorId The sensor id of the sensor that you wish to get the
* measured value from
*/
public Measurement getLastSensorReading(long sensorId) {
Measurement result = null;
EntityManager em = emf.createEntityManager();
try {
Query q = em.createNamedQuery("LastSensorReading");
q.setParameter("paramSensorId", sensorId);
result = (Measurement) q.getSingleResult();
} catch (Exception e) {
}
em.close();
return result;
}
/*
* Provides a list of all sensors
*/
@SuppressWarnings("unchecked")
public List<Sensor> getListOfSensors() {
List<Sensor> result = null;
EntityManager em = emf.createEntityManager();
try {
Query q = em.createNamedQuery("GetListOfSensors");
result = q.getResultList();
} catch (Exception e) {
}
em.close();
return result;
}
}
Adapt the web.xml
At the end we'll need to modify the web.xml file for the servlet mapping of the Servlet. Instead of
<servlet>
<description></description>
<display-name>SensorsServlet</display-name>
<servlet-name>SensorsServlet</servlet-name>
<servlet-class>com.sap.iot.sensors.SensorsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SensorsServlet</servlet-name>
<url-pattern>/SensorsServlet</url-pattern>
</servlet-mapping>
we need to change the url-pattern and add a new resource into the web.xml for the jdbc connection so that the part of the servlet-mapping looks like this
<resource-ref>
<res-ref-name>jdbc/DefaultDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
<servlet>
<description></description>
<display-name>SensorsServlet</display-name>
<servlet-name>SensorsServlet</servlet-name>
<servlet-class>com.sap.iot.sensors.SensorsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SensorsServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
The resulting project
At the end your project structure should look like this:
Deploy the app
Now deploy the app to your free developer account. Just follow the instructions in the tutorial HCP and Java - Hello World on the hcp.sap.com website if you don't know how to do that. Check the step called "Deploy, Run and Test the Application in the Cloud" in that tutorial and adapt the instructions there to your project here.
Test the app
If you did everything right you should now be able to add a sensor to the database. Let's test this.
When calling your app for the first time you'll get an empty JSON output with an opening and closing curly bracket.
Adding a sensor
To add a sensor to your database you should call the servlet this way:
https://<name of your application><your account name>.hanatrial.ondemand.com/<name of your project>/?action=addsensor&type=temperature&device=Truck 1- RaspPi&description=RaspberryPi CPU
If your account is called p1234567890trial, the project is called iotscenario and you defined the name fishimport as the name of your app when you've deployed it to your account the link looks like this:
https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario/?action=addsensor&type=temperature&device=Truck 1- RaspPi&description=RaspberryPi CPU
Just enter the link and press return. After that call your app again (in this example the link to the app is https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario), and instead of getting empty curly brackets you should see something similar like this:
{
"sensor0":
{"id":1,"device":"Truck 1 - RaspPi","type":"temperature","description":"RaspberryPi CPU","measurements":[]}
}
You can add as many sensors as you want. Most importantly each of them as a unique id. That id is used to assign measurements to it. That's what we'll do in the next step.
Adding a sensor value (measurement)
Now that we have a sensor added to the database we can also store the sensor values related to it.
To add a sensor value (or measurement how the object is called in the database) you need to call the servlet with different parameters. Taking the example from above where we've created a sensor with the id 1, the link to create a corresponding measurement looks like this:
https://fishimportp1234567890trial.hanatrial.ondemand.com/iotscenario/?action=addsensorvalue&sensorid=1&unit=Celsius&sensorvalue=16&sensorvaluemultiplier=0.1&sensorvaluecalibration=0
Again type-in the link and execute it. Afterwards call the servlet again without any parameters and you'll get an output similar to this one:
{
"id":1,"device":"Truck 1 - RaspPi","type":"temperature","description":"RaspberryPi CPU",
"lastMeasurement":
{"id":1,"unit":"Celsius","storedAt":"Nov 10, 2014 9:53:13 AM","value":-1.6,"sensorId":1},
"measurements":
[
{"id":1,"unit":"Celsius","storedAt":"Nov 10, 2014 9:53:13 AM","value":-1.1,"sensorId":1}
]
}
The output provides you now with the last measured sensor value (lastMeasurement) as well as a list of all stored measurements (measurements). Although it should provide you with ALL measurements you have assigned to the sensor there is a limit that I've added for performance reasons.
If you check the code of the servlet there is a variable inside SensorsServlet called MAX_NUMBER_SENSOR_READINGS. In the code I've provided this is set to 10. Of course you can modify this number and adapt it to your needs.
Summary
We are now set to send sensor values to our app in the cloud which also provides a JSON output of that data that we can consume in an UI.
In the next blog post we'll create the sending part on the RaspberryPi so that it can send it's temperature to our app in the cloud.
Looking forward for your feedback.
Best,
Rui