JSR363 by Example: IoT Temperature Sensor

I, Otavio and Werner did a presentation in JavaOne 2016 about JSR 363, the JSR we worked on for the last 2 years. It is final, so it means the world can use it and the implementations in real projects!

To help you get started, I did a quick demonstration project on how an IoT device would use the API.

This project implements a Temperature Sensor enabled by Intel Edison. Two actual sensors are supported: the internal temperature sensor for the Intel processor and the Omega RH USB Sensor. The sensors are represented by the TemperatureSensor interface, which exposes a single method to read the sensor and get a MeasurementRecord object.

MeasurementRecord is a basic object for IoT devices: they all have an id (String sensorId) and capture data (Quantity measurement)  at a given timestamp (ZonedDateTime time). Every IoT project has that, and before JSR363 that data would be a double, int, or another primitive value. Which made it difficult to avoid bugs when working with more than one unit, for different sensors. You had to guess or rely on Javadoc to know if the data in question was a Temperature in Celsius or Active Power (W)…

The main class is MainEdison and reads from a sensor and posts the data as JSON POST in given URL. Hence, this main class needs 2+ arguments: the URL to post the reading (in full URL format) and the name(s) of thermal zones to read from the processor (you can try thermal_zone1 for starter).

I used Maven to make a “fat jar”, so it’s easier to run. So, just “maven package” your project and it should be good to go!

Take a look at the project and please ask questions! I’ll come back to write more about JSR363 and explore more about its APIs and how to use them.

See you next time!