Install Reactive Blocks
Follow these instructions to install Reactive Blocks.
Sign up for Bluemix
- Sign up for Bluemix.
- From the Catalog, select Internet of Things Platform Starter or Node-RED as boilerplate.
- Click on Create.
Prepare the Node-RED Application in Bluemix
Open the Node-RED editor for your application from the Bluemix dashboard.
Drag the following three nodes into the canvas:
- A http node, from the input section.
- A function node, from the function section.
- A twitter node, from the social section.
The result should look like this:
Now configure the nodes as follows:
Double-click on the twitter node, and authenticate Node-RED to get access to your Twitter account.
Configure the http node as follows:
Configure the function node as follows:
You can copy the code from here:
node.log("Image received");msg.media = new Buffer(msg.req.body, "base64");msg.payload = "Intruder Detected";msg.res.send(200,"OK");return msg;
The code above sets the image as a buffer object, as required by the subsequent Twitter node. It also answers the HTTP post request.
After you made the adjustments, click on Deploy in the upper right corner. The cloud application is now ready.
Import the Example to Reactive Blocks
Now we build the application running on your Raspberry Pi or PC.
- After the installation of Reactive Blocks, restart Eclipse
- Open the Reactive Blocks perspective:
- Select Window → Open Perspective → Other… → Reactive Blocks
- Click the import button in the Blocks View, as shown here:
- Select Simple Examples and Tutorials
- The wizard asks you to sign in, using your Google or Facebook Id
- Import (Level 2) Intrusion Detection to Twitter Via Bluemix
- After the import, you should see the following application:
Let’s have a look how this application works:
- The block Camera Motion Detection takes a picture every second. Once two pictures are sufficiently different, it pushes out a picture via pin motionDetected.
- The limiter makes sure that we are not sending too many pictures in case there is lots of movement. It will make sure to send at most one picture every 100 seconds.
- The Convert Image block converts the image as an array of byte, using JPEG encoding in the example. (The Twitter API does not accept BMPs.)
- The block HTTP Post sends a HTTP post to the Node-RED application that we configured before, using the bytes of the image as body.
Configure the Reactive Blocks Application
The HTTP post needs to be sent to the right server. For that, double-click on the operation makePost. Then, adjust the url variable to match yours.
In the browser where you edit the Node-RED application, check the address. It may be something like
Adjust the url in the Java method makePost() in Reactive Blocks to be
Of course, replace <your org> with the name of your organization that you chose on Bluemix.
Build the Application
- To build the application, right-click in the blocks editor and select Build / Re-Build for Java Standard Edition.
- The package explorer will open the generated Java project.
Run the Application on your PC
You can run the application on your PC. (If you want to run it on a Raspberry Pi, see the next step.)
- In the generated project, find the file IntrusionDetection.launch.
- Right-click it, and select Run As / IntrusionDetection.
- Move in front of the camera.
- Check your Twitter!
Run the Application on the Raspberry Pi
In this step, we will generate a runnable JAR file that contains the application, transfer the application to the Pi and then run it.
- Find the generated project in the Package Explorer.
- Right-click on the project, select Export
- Select Java / Runnable JAR file.
The wizard to produce the runnable JAR opens:
- Select the launch configuration IntrusionDetection
- Select a file name for the exported file, ending with *.jar
- Choose Extract required libraries…
Now let’s copy the generated JAR file onto the Raspberry Pi:
- Open a terminal window at your machine.
- Navigate the the directory where the generated JAR file is located
- Type scp IntrusionDetection.jar email@example.com:~/
The command scp needs a valid username and password combination, so it will ask for a password. pi is the default username. raspberry the default password.
Replace 10.10.15.107 by the IP address of your Pi
Run the application on the Pi:
- Log in to the Raspberry Pi (for the default login see Step 4), either directly or remotely via ssh.
- Check if the runnable JAR file has been copied to the Raspberry Pi.
- Type sudo java -jar IntrusionDetection.jar to execute the application on the Raspberry Pi.
In this tutorial, we use a HTTP post request to send the data into Bluemix. Another excellent way to send data into Bluemix is via the IBM Watson IoT Platform. It is suited to send many events and commands between gateways and the cloud. In addition, it saves data and makes it accessible. For the tutorial here we used HTTP because of the size of the images. In a real application, you can also combine HTTP and Watson IoT, so that an application can use Watson IoT send events and commands, but HTTP for larger chunks of data.
The outcome of this tutorial is a running application that detect movements in pictures and tweets these pictures via Bluemix.