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.¬†
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:
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:
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.
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:
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬†¬†¬†¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†¬†¬†¬†¬†
Very importantly, we should see that, the configured Gateway Device with IBM EAA as Connected on IBM IoT Platform as shown in below snippet:
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:
- Get the code on to Raspberry Pi 3
- git clone https://github.com/ibm-watson-iot/edge-analytics-samples.git
- Go to dslinks/java/RpiLinks directory
- cd dslinks/java/RpiLinks
- Build dslinks jar using the maven command
- mvn clean package
- 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:
- Copy target/classes/RpiUnoLink directory to DSA-PATH/dglux-server/dslinks
- sudo cp¬† -r¬† target/classes/RpiUnoLink¬† /opt/dsa/dglux-server/dslinks
- 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/
- Stop and Start DSA Server
- sudo service dsa stop
- sudo service dsa start
- 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
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†¬† ¬†¬†¬†
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:
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:
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:
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).
- Get the code on to Raspberry Pi 3
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.
Let us now define a Edge Rule with condition when temperatue crosses 27′ C:
Now, we add new action to the Edge Rule with alert containing reboot as action when the device temperature crosses¬† 27′ C:
Next, we set the above defined action to the Edge Rule and finally save the Edge Rule to be activated on to Edge Gateway:
This is about defining conditions, alerts and actions as part of Edge Rule on IBM Watson IoT Platform.
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) :
Following we should see a notification message indicating activating the Edge Rule submission is successful.
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.
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:
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.
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:
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.
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
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: