Friday, June 10, 2016

Raspberry PI + DS18b20 Temperature Sensor + Apache Quarks + IBM Cloud - Watson IOT platform



                         Raspberry PI + Apache Quarks + IBM Watson IOT platform  

Internet Of Things is here and expected to change personal lives and also change the way companies are doing Business. IDC predicts an explosive Internet of Things spending to reach $1.7 Trillion by year 2020. Below are the key points in IOT value chain:

1.      Connect and control Devices / Sensors.

2.      Capture and Manage IoT data.

3.      Visualize and Analyze data.

4.      Act / React, trigger a corrective or preventive action of Business Value.


The Blog post aims to achieve first 3 points by connecting Raspberry Pi with DS18b20 - Temperature sensor which acts a Device on edge and sends data to Cloud platform.  However additional proof point is usage of Apache Quarks , which is open source programming model to control and analyze streaming data on edge devices which otherwise sends hundreds of events per second. The advantage is :
  • Ability to send only a limited or relevant data from device, hence a reduced amount of data storage and thereby reduction in communication and storage costs.
  • Logic and decision making on the device, and ability to react locally to events.

Architecture View




STEPS:
a. Setup Raspberry PI device wired to Temperature sensor
b. Register Raspberry PI with IBM Watson IoT Platform
c. Download and compile Apache Quarks and set up Eclipse environment.
d. Create Quarks application that will send sensor data to IoT Platform.
e. Run the application on Device passing IoT device registration parameters.
f. View live data on Watson IoT Service, or through custom programs.



1.  Setup Raspberry PI device wired to Temperature sensor.

Setup Raspberry PI with Raspbian OS, wired to temperature sensor DS18B20. Below is the standard wiring diagram and actual R-Pi device with wifi dongle connected.

       

Log on to your R-Pi and at command prompt enter 2 commanda: sudo modprobe w1-gpio  &&  sudo modprobe w1_therm to load device kernel modules required for using temperature sensor. Reference link .

Further, install Pi4J on R-Pi, which will be used for sending the device parameters along with external temperature parameter.


2. Register Raspberry PI with IBM Watson IoT Platform

Log in the IBM Bluemix Account. (New account can be created with 30 day trial period). Add the IoT Platform starter - Boiler plate. which is a combination of NodeJs, Cloudant DB and IoT Platform service. Create new device on the IoT service providing the name, device Id & authentication token. These parameters together are added into "device.cfg" file which is an input parameter to the Quarks application.



                             

3.  Download and compile Apache Quarks and set up Eclipse environment.

Followed instructions as per this link : https://github.com/apache/incubator-quarks/blob/master/DEVELOPMENT.md.  The generated Jar files can be found in "target" directory folder, to be copied to build path while creating the new Quarks Application.

4. Create Quarks application that will send sensor data to IoT Platform.

Create new Java Application in Eclipse. Below is the brief application flow:

      a. Create a DirectProvider and Topology.
                DirectProvider dp = new DirectProvider(); 
                Topology topology = dp.newTopology("RPiSensor");

      b. Use IotfDevice to connect to IBM Watson IoT. 

                IotDevice device = new IotfDevice(topology, new File(deviceCfg));
      c. Create a stream of Raspberry PI sensor readings, where each tuple  
         contains:
                  1. The current time
                  2. External Sensor temperature (temperature recorded by DS18b20.)
                  3.  R- Pi - CPU temperature.
                  4.  R - Pi - CPU Voltage.
                        
                 TStream<JsonObject> readings = systemInfo(topology, 10, TimeUnit.SECONDS);

            This enables controlling data being sent from the device. In the above 
         case, data is sent from the device every 10 seconds. It is possible to  
         send only specific temperature ranges above or below a range, further 
         triggering an alert notification and possible action back to another device.

         The topology.poll method receives an endless source stream and 
         iterated for the above 4 values in a JsonObject.
       d. External temperature sensor (DS18b20) readings is read from path - 
      /sys/bus/w1/devices/w1_slave,where as the CPU temperature and voltage 
         is obtained from Pi4J - SystemInfo.
       e. Send the stream of sensor readings as device events to IBM Watson IoT 
         platform & submit the toplogy to provider.
       f. Export the application Jar from Eclipse as Runnable Jar, place it on the 
        Raspberry Pi  device along with device.cfg file.

5. Run the application on Device passing IoT device registration parameters.

Run the application on the device, which should return the 4 parameters - "time", "cpuTemperature", "cpuVoltage" & "external temp" (from DS18b20 sensor) 






6.  View live data on Watson IoT Service, or through custom programs. 
On Watson IoT platform , check the registered device for connection and streaming of live data.







               
                 
 Further, for data visualization, can use IBM IoT Foundation sample from the following link => https://github.com/ibm-messaging/iot-visualization. Below is the output Graph that displays the temperatures recorded by the sensor on time scale. 






Summary:
This example demonstrates reading of temperature from a sensor wired to Raspberry PI, which in practical scenario could be IoT device running Apache Quarks on a Refrigerated Shipping containers, or a land based refrigerated warehouse storing perishable goods and monitored for temperature fluctuations or predict failures well in advance.

Analytics on Edge device using Apache Quarks API helps send only the required data or data of interest , instead of continuously streaming all the events from sensors saving cost, better ROI & higher business value.



Hemant Kanago
https://in.linkedin.com/in/hemantrk




3 comments: