Tutorial: Intrusion Detection to Twitter via IBM Bluemix

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:

  • How to setup Reactive Blocks and build an application for a PC or gateway, such as the Raspberry Pi.
  • Setup and run a Node-RED application.
  • Send an image from the Reactive Blocks applications into Bluemix and Node-RED via HTTP.
  • Publish a tweet from Node-RED.

Requirements

  • Windows or Mac OS X
  • Oracle Java 8
  • Eclipse 4.5 (Eclipse 4.4 or 4.3 work as well)
  • A subscription to Bluemix
  • A subscription to Reactive Blocks (free)
  • A Twitter account

Outcome

The outcome of this tutorial is a running application that detect movements in pictures and tweets these pictures via Bluemix.

Target Audience

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.

Overview

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.

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 Foundation 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 (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.

Import the Example

Now we build the application running on your Raspberry Pi or PC.

  • After Eclipse restarted, 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:

This is how the application works:

  1. The block Camera Motion Detection takes a picture every second. Once two pictures are sufficiently different, it pushes out a picture via pin motionDetected.
  2. 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.
  3. 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.)
  4. 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 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

http://yourOrg.mybluemix.net/red/#

Adjust the url in the Java method makePost() in Reactive Blocks to be

http://yourOrg.mybluemix.net/input/

Of course, replace yourOrg 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

  • In the generated example, find the file IntrusionDetection.launch.
  • Right-click it, and select Run As/IntrusionDetection.
  • Move in front of the camera.
  • Check your Twitter!

What’s Next?

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.)

Have a look at this tutorial to learn how to use the IBM IoT Foundation with Reactive Blocks.

Tags

Eclipse Java ReactiveBlocks Twitter HTTP Bluemix Node-RED