Skill Level: Any Skill Level

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.


  • 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
  • A Twitter account


  1. Install Reactive Blocks

    Follow these instructions to install Reactive Blocks.

  2. 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.
  3. 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.



  4. 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:

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

    http://<your org>.mybluemix.net/red/#

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

    http://<your org>.mybluemix.net/input/

    Of course, replace <your org> with the name of your organization that you chose on Bluemix.

  6. 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.
  7. 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!
  8. 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 pi@
    • 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 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.
  9. Whatu2019s 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 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.

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

Expected Outcome

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

1 comment on"Intrusion Detection to Twitter with a Raspberry Pi"

  1. Hi there,
    Quick question, have there been a successful intrusion detection via picamera?
    I have followed the steps but it didn’t work, it seems that the grabber block only takes usb webcam. What changes do I need to make to have it work via picamera?

    Thank you sir

Join The Discussion