Overview

Skill Level: Intermediate

Recipe mandates familiarity with Edge Analytics using IBM Edge Analytics Agent in IBM Watson IoT Platform

This recipe is continuation of Edge Analytics recipe. Post configuration of IBM Edge Analytics Agent on Raspberry Pi 3 as Gateway, this recipe describes the steps involved in defining the Edge Rule with publishing alert to Edge Gateway for device action.

Ingredients

  • Raspberry Pi 3 with IBM Edge Analytics Agent (EAA) configured to connect to IBM Watson IoT Platform. Refer to Edge Analytics recipe.
  • If IBM EAA is installed more than once, then make sure to remove random names present in DSA-PATH/dglux-server/conns.json before starting DSA Server on Raspberry Pi 3 which are not active links.
  • Install JDK 1.8 on Raspberry Pi 3 referring to RpiBlog
  • Install git on Raspberry Pi 3
    • sudo apt-get update
    • sudo apt-get install git
  • Install maven on Raspberry Pi 3
    • sudo apt-get update
    • sudo apt-get install maven
  • Install Java RXTX Library in Raspberry Pi 3 for Serial Communication with Arduino Uno board
    • sudo apt-get install librxtx-java
    • sudo¬† cp¬†¬† /usr/lib/jni/librxtxSerial.so¬†¬† $JAVA_HOME/jre/lib/arm
    • sudo¬† cp¬† /usr/share/java/RXTXcomm.jar¬†¬† $JAVA_HOME/jre/lib/
  • Make ready the Arduino Uno device with Sketch Program
  • Arduino Groove Base Shield installed on Arduino Uno device
  • Temperature Sensor connected to A0 on Arduino Groove Base Shield
  • Moisture Sensor connected to A2 on Arduino Groove Base Shield
  • Buzzer connected to D3 on Arduino Groove Base Shield
  • Connect Arduino Uno to Raspberry Pi 3 using USB Cable. After connecting, make sure, we should see presence of device /dev/ttyACM0 on Raspberry Pi 3.

Step-by-step

  1. Introduction

    This recipe is continuation of Edge Analytics Recipe. In this recipe, we are going to showcase:

    • Defining Edge Rule with alerts to push on to Gateway (Raspberry Pi 3)
    • Raspberry Pi 3 invoking the action associated with alert on the attached device (Arduino Uno)
    • Arduino Uno executes the invoked action on itself

    To proceed with recipe, recipe expects one has gone through prior Edge Analytics Recipe and familiarized with installation and usage of DGLux Tool, Installing DS Links in DGLux Tool, Configuring DS link IBM EAA in DGLux Tool to get connected to IBM Watson IoT Platform.

    Refer to steps described in Edge Analytics Recipe to install DSA Server on Raspberry Pi 3, install and configure IBM EAA on Raspberry Pi 3 and install required softwares on to Raspberry Pi 3 as specified in ingredients section above.

    As part of this recipe, we are going to describe how to build new DS Links for DGLux Tool to interact with IBM EAA running on Raspberry Pi 3(Gateway Device), co-ordinate between Raspberry Pi 3 and Arduino Uno (attached device) to handle alerts pushed on to Raspberry Pi 3 and carry out device actions on Arduino Uno. 

  2. Making Ready Arduino Uno to emit Temperature and Moisture Data

    This section describes about the steps to be carried out on Arduino Uno. Install Arduino Base Groove Shield on top of Arduino Uno and connect the sensors and buzzer to the pins as shown in below diagram:

    uno_ckt

     

    Push the sketch to Arduino Uno using the Arduino IDE. After successful uploading of sketch program on to Arduino Uno, Open Serial Monitor from Arduino IDE, we should see the temperature and moisture data coming out of Arduino Uno Device as shown in below pasted snippet:

    uno_serial_output

    As the Arduino Device is ready with the uploaded sketch, connect to Raspberry Pi 3 using the USB Cable and make sure, we should see presence of device /dev/ttyACM0 on Raspberry Pi 3 to proceed further.

  3. Installing RpiUno DSLink on Raspberry Pi 3

    As mentioned in the ingredients section, make ready the Raspberry Pi 3 Device with the required softwares and IBM EAA configured, with the help of, the steps given in Edge Analytics Recipe. The below image shows the details from DGLux Tool after successful configuration of IBM EAA:

                  eaa_configured                          eaa_connected

    Very importantly, we should see that, the configured Gateway Device with IBM EAA as Connected on IBM IoT Platform as shown in below snippet:

     gateway_cntd

     

    In this section, we’ll see the steps to build RpiUno DS Link and steps to use the RpiUno DS Link on Raspberry Pi 3 to receive temperature and moisture data from attached Arduino Uno Device.

    Building RpiUno DS Link:

    1. Get the code on to Raspberry Pi 3
      • git clone https://github.com/ibm-watson-iot/edge-analytics-samples.git
    2. Go to dslinks/java/RpiLinks directory
      • cd dslinks/java/RpiLinks
    3. Build dslinks jar using the maven command
      • mvn clean package
    4. Copy dslinks jar to RpiUno DS Link directory
      • cp target/dslinks-0.0.1.jar¬†¬† target/classes/RpiUnoLink

    Installing RpiUno DS Link into DGLux Tool:

    1. Copy target/classes/RpiUnoLink directory to DSA-PATH/dglux-server/dslinks
      • sudo cp¬† -r¬† target/classes/RpiUnoLink¬† /opt/dsa/dglux-server/dslinks
    2. Copy Java RXTX Library and JAR into JAVA_HOME for Serial Communication with Arduino Uno board
      • sudo cp¬† /usr/lib/jni/librxtxSerial.so¬† $JAVA_HOME/jre/lib/arm
      • sudo cp¬† /usr/share/java/RXTXcomm.jar¬† $JAVA_HOME/jre/lib/
    3. Stop and Start DSA Server
      • sudo service dsa stop
      • sudo service dsa start
    4. Log into DGLux Tool, we should be able to see new link with the name RpiUnoLink and We should see values retrieved from Arduino Uno Device in DGLux Matrix Window
      • Data->sys->links
      • Data->downstream->RpiUno->RpiUno

                  rpiuno_connected                             rpiuno_data   

    Seeing the temperature and moisture data from Arduino Uno Device in the DGLux Metrics Window confirms that we have successfully installed the RpiUno DSLink in to DGLux Tool running on Raspberry Pi 3. This picturises that we have an attached device Arduino Uno attached to Gateway Device (Raspberry Pi 3) through DS Link.

    Next, we restart the IBM EAA DSLink from DGLux tool, so that the attached device gets added on to IBM Watson IoT Platform  automatically. To Restart the DSLink, follow the path on DGLux tool: Data->sys->links->ibm-watson-iot-edge-analytics-dslink-java-1.0.0 (Right Click) ->Restart Link:

    ibm_eaa_restart

     

    After few seconds restarting the IBM EAA link from DGLux tool, we should be able to see an attached device added automatically on to IBM Watson IoT Platform with the name RpiUno as shown in below snippet:

    attached_dev_added

    Clicking on RpiUno attached device on IBM Watson IoT interface, we should be able to see the device data consisting of temperature and moisture values sent to Platform for further actions as shown in below snippet:

    sensor_values

     

    This section concludes about the steps needed to build and install RpiUno DSLink to receive temperature and moisture data from Arduino Uno to IBM Watson IoT Platform through Raspberry Pi 3 as gateway using Distributed Service Acrchitecure (DSA).

  4. Defining Alerts and Device Actions with Edge Rule

    The IBM EAA can receive edge rule from IBM Watson IoT Platform to trigger an alert when ever a specified condition in the edge rule meets through received device events, these are referred as local alerts to edge gateway. The RpiUno DS Link implicitly subscribes to alerts triggered by EAA. As the EAA triggers an alert, it distributes the alert along with the action message to the DS Links that are subscribed with the EAA for receiving alerts through DS broker. As the RpiUno DS Link receives the action message from EAA, it first verifies the action is for it’s device or not, if so, an appropriate method is called to handle the specified action on the device (Arduino Uno in this case).

    This section assumes we are already familiar with Creating Schema and Defining Edge Rules on IBM Watson IoT Platform, if not, please refer to Setup Rules Section in our earlier recipe on Edge Analytics to get familiar with these entities.

    In this section, we are going to see defining Alerts and Actions on IBM Watson IoT Platform based on a temperature rule. Define a schema for attached device following steps in Setup Rules Section from Edge Analytics recipe before proceeding to create a rule based of temperatue value.

    add_schema_dtype   add_schema_temp

     

    Let us now define a Edge Rule with condition when temperatue crosses 27′ C:

       edge_rule_createedge_rule_condition

     

    Now, we add new action to the Edge Rule with alert containing reboot as action when the device temperature crosses¬† 27′ C:

    edge_rule_atype  edge_rule_acondig

     

    Next, we set the above defined action to the Edge Rule and finally save the Edge Rule to be activated on to Edge Gateway:

    edge_rule_set_action  edge_rule_cnd_action

     

    This is about defining conditions, alerts and actions as part of Edge Rule on IBM Watson IoT Platform.

  5. Activating Edge Rule on to Edge Gateway and Handling of Reboot Device Action on Arduino Uno

    As we save the Edge Rule in previous section, we are presented with a window as shown here. Select the defined edge rule and click on Submit Command to activate the Edge Rule on to Edge Gateway (Raspberry Pi 3) :

    edge_rule_activate

    Following we should see a notification message indicating activating the Edge Rule submission is successful.

    edge_rule_activation_success

     

    Closing the Edge Rule definition window and selecting Browse Rules, we should be able to see the Edge Rule which is requested for activation is Active on Edge Gateway.

    edge_rule_active

    As the Edge Rule becomes active on Edge Gateway (IBM EAA), as per our alert configuration in activated edge rule, we should not see any device events being sent to IBM Watson IoT Platform:

    edge_rule_no_events

    Now, refer to DGLux Metrics Window to get current temperatue sensor values from Arduino Uno and whenever we see temperature crossing 27’C (just hold temperature sensor for few secods to see hike in sensor value), we should hear a buzzer sound (a beep) and the Arduino Uno gets rebooted as per the defined action with the alert.

    edge_rule_temp_27

    We can verify the handling of Reboot action on Arduino Uno by looking into RpiUno DS Link’s log located at the path DSA_PATH/dglux_server/logs/RpiUno.log as shown here:

    edge_rule_device_action_success

     

    This concludes about the steps involved in activating the Edge Rule to send an alert to attached device with the device action as “Reboot”. The device receives the device action and carries out the reboot operation on itself. We can validate this either by listening to beep sound once or check in the RpiUno DSLink log as shown in this section above.

  6. Conclusion

    As this recipe is next part of our earlier Edge Analytics recipe, in this recipe, we have showcased the steps to:

    • Build and Install DS Link to receive temperature and moisture sensor data on edge gateway using DGLux Tool
    • Automatic registration of attached device on IBM Watson IoT Platform with the help of IBM Edge Analytics Agent
    • Defining Edge Rule to send an alert to attached device with “Reboot” as action
    • Validation of device action execution on attached device
  7. Where to go from here?

    We have bunch of other related analytic recipes in the recipes pool. Below given are the links to some of our related recipes on analytics for your reference:

6 comments on"Handling Alerts and Device Actions with Edge Analytics in IBM Watson IoT Platform"

  1. Dave Dougherty December 09, 2016

    Hi…. I have a question or two, that may be the same question. How come you didn’t define the attributes ‘type’ and ‘devicetype’ on the RpiUno link as in the System link recipe? Where does the ‘attached’ devietype that shows in the IoTP dashboard come from? How does the EAA become aware of a particular link, i.e. how does it know to subscribe to the Broker for that links events to be analyzed? thanks kindly, dave

  2. Recipes@WatsonIoT December 12, 2016

    All the details are covered as part of DS link implementation to make the user experience smooth in installing and auto configuring the DS link for Arduino Uno as attached device to Raspberry Pi. If you look at the code for DS Link on given github link, you can see the details.

  3. In step 3, somehow I do not see the temp + moisture in DGLux. The status of Aduino has been connected but with no data in DGLux.
    I have the data strems coming from /dev/ttyACM0. So, Arduino is sending the data.

    Note:
    Arduino is plain without any shield or sensors attached to it, I think this should not be the problem as long as it is sending something to Raspberry on its hanging analog pins.

    Any idea what I missed? Restarting IBM EAA or ArduinoUno links do not help also. No devices appear also in Watson IoT Platform (Note that I have successfully completed 1st recipe for IBM EAA).

    Many thanks.

  4. Recipes@WatsonIoT January 07, 2017

    Hi Andi,

    Arduino without any shield or sensors attached will also send out the data from the specified pins as part of the sketch. If you have installed the DS link for Uno, you should be able to see the data from Uno under metrics window if you navigate to Data->downstream->RpiUno->RpiUno. Also, you can track what’s happening in the log file located at path – DSA_PATH/dglux_server/logs/RpiUno.log. Have a look into log file, you’ll get clue what’s happening with RpiUno DS Link.

    • Here is the log from RpiUno on Raspberry Pi. It seems that it can not find /dev/ttyACM0 which actually exists and sending the data.
      Any pointer on how to find out about this? Many thanks.

      pi@raspberrypi:~ $ date
      Sat 26 Nov 15:37:17 WIB 2016
      pi@raspberrypi:~ $ tail -15 /opt/dsa/dglux-server/logs/RpiUnoLink.log | more
      [2016-11-26 15:11:50.906799] 2016-11-26 15:11:50.904 [nioEventLoopGroup-2-2] INFO com.ibm.iot.dslinks.RpiUnoLink.Main – Rpi Uno Reques
      ter initialized
      *** Logging started for RpiUnoLink ***
      APP_NAME = RpiUnoLink
      APP_BASE_NAME = rpiuno
      CLASSPATH = /opt/jdk1.8.0/jre/lib/RXTXcomm.jar:/opt/dsa/dglux-server/dslinks/RpiUnoLink/dslinks-0.0.1.jar
      JAVACMD = java
      *** Logging ended for RpiUnoLink ***
      [2016-11-26 15:21:53.168776] == Executing bash with arguments [/opt/dsa/dglux-server/dslinks/RpiUnoLink/rpiuno, –name, RpiUno, –node
      s, nodes.json, –key, .key, –log, info, –broker, http://127.0.0.1:37747/conn, –token, 1B5wqekUYRARDrcf79Myfmnhbvh0AyRRfgTWILiip6oP9
      JW2] (pid: 1159) ==
      [2016-11-26 15:21:53.294013] Complete CMD Line: java -classpath /opt/jdk1.8.0/jre/lib/RXTXcomm.jar:/opt/dsa/dglux-server/dslinks/Rpi
      UnoLink/dslinks-0.0.1.jar com.ibm.iot.dslinks.RpiUnoLink.Main –name RpiUno –nodes nodes.json –key .key –log info –broker http://1
      27.0.0.1:37747/conn –token 1B5wqekUYRARDrcf79Myfmnhbvh0AyRRfgTWILiip6oP9JW2
      [2016-11-26 15:22:02.513681] java.lang.ClassNotFoundException: gnu.io.RXTXCommDriver thrown while loading gnu.io.RXTXCommDriver
      [2016-11-26 15:22:04.117893] java.lang.ClassNotFoundException: gnu.io.RXTXCommDriver thrown while loading gnu.io.RXTXCommDriver
      [2016-11-26 15:22:04.128537] 2016-11-26 15:22:04.119 [Thread-3] INFO com.ibm.iot.dslinks.RpiUnoLink.RpiUnoResponder – Could not find C
      OM port.
      [2016-11-26 15:22:04.136683] 2016-11-26 15:22:04.124 [Thread-3] INFO com.ibm.iot.dslinks.RpiUnoLink.Main – Rpi Uno Responder initializ
      ed
      [2016-11-26 15:22:04.661906] 2016-11-26 15:22:04.658 [nioEventLoopGroup-2-2] INFO com.ibm.iot.dslinks.RpiUnoLink.RpiUnoRequester – EAA
      Alerts Path – /downstream/ibm-iot-edge-analytics/IBMEdgeAnalyticsAlert
      [2016-11-26 15:22:04.674900] 2016-11-26 15:22:04.669 [nioEventLoopGroup-2-2] INFO com.ibm.iot.dslinks.RpiUnoLink.Main – Rpi Uno Reques
      ter initialized
      pi@raspberrypi:~ $ ls /dev/ttyACM0
      /dev/ttyACM0
      pi@raspberrypi:~ $ cat /dev/ttyACM0
      smp:17.13,mois:42

      status temp:17.04,mois:42

      status temp:17.13,mois:43

      status temp:18.32,mois:44

      status temp:19.35,mois:45

      • It is solved now.
        Somehow RXTXcomm.jar was not there in the requested path (/opt/jdk1.8.0/jre/lib/).
        I just copied the file to the requested path to satisfy the request.

        DGLux now is showing “Sensor” and “Temperature” data in realtime.

        The log in Raspbery Pi for Rpi Uno is as follows.
        *** Logging started for RpiUnoLink ***
        APP_NAME = RpiUnoLink
        APP_BASE_NAME = rpiuno
        CLASSPATH = /opt/jdk1.8.0/jre/lib/RXTXcomm.jar:/opt/dsa/dglux-server/dslinks/RpiUnoLink/dslinks-0.0.1.jar
        JAVACMD = java
        *** Logging ended for RpiUnoLink ***
        [2016-11-26 17:34:52.184829] == Executing bash with arguments [/opt/dsa/dglux-server/dslinks/RpiUnoLink/rpiuno, –name, RpiUno, –node
        s, nodes.json, –key, .key, –log, info, –broker, http://127.0.0.1:43360/conn, –token, mtl1YthQZJUZpEEDgiKe4FCmRJtjl7xb3g7kcAQsqZyHY
        fJB] (pid: 31844) ==
        [2016-11-26 17:34:52.316226] Complete CMD Line: java -classpath /opt/jdk1.8.0/jre/lib/RXTXcomm.jar:/opt/dsa/dglux-server/dslinks/Rpi
        UnoLink/dslinks-0.0.1.jar com.ibm.iot.dslinks.RpiUnoLink.Main –name RpiUno –nodes nodes.json –key .key –log info –broker http://1
        27.0.0.1:43360/conn –token mtl1YthQZJUZpEEDgiKe4FCmRJtjl7xb3g7kcAQsqZyHYfJB
        [2016-11-26 17:34:58.792299] 2016-11-26 17:34:58.784 [Thread-3] INFO com.ibm.iot.dslinks.RpiUnoLink.Main – Rpi Uno Responder initializ
        ed

Join The Discussion