Use the webcam of your PC or a Raspberry Pi to detect an intruder, forward the image to Bluemix and from there to Twitter. We’ll use Reactive Blocks to graphically build the gateway part of the application, and Node-RED in the cloud.
This tutorial is an introduction to several technologies. You can learn the following:
The outcome of this tutorial is a running application that detect movements in pictures and tweets these pictures via Bluemix.
The target audience for this tutorial are developers that want to send data into Bluemix. For this tutorial, no specific Java knowledge is needed, but for developing applications further, skills with Java programming will be helpful.
In the Raspberry Pi is connected to a USB camera. The application also works with your PC and any built-in camera. The camera takes pictures. Once the application detects a difference in the pictures, it forwards a message including the picture to an application running on Bluemix. From there, we prepare the data and forward it to Twitter.
Follow these instructions to install Reactive Blocks.
Open the Node-RED editor for your application from the Bluemix dashboard.
Drag the following three nodes into the canvas:
The result should look like this (note that this image does not show preloaded template, you should just add your stuff at the bottom):
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.
Now we build the application running on your Raspberry Pi or PC.
This is how the application works:
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 yourOrg with the name of your organization that you chose on Bluemix.
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.
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 IoT Foundation. It is suited to send many events and commands between gateways and the cloud. (We have used HTTP because of the size of the images.)
Eclipse Java ReactiveBlocks Twitter HTTP Bluemix Node-RED