How to connect a Raspberry Pi and a PIR sensor to Altair SmartWorks for building a motion detection system.

0. Introduction

This easy tutorial helps you to build an easy motion detection system.
To do this, we use a Raspberry Pi and a PIR Sensor (Passive Infrared Sensor).
In this project you will learn how to:

  • Connect a Raspberry Pi to Altair SmartWorks to send data streams using Altair SmartWorks HTTP REST API
  • Build a motion detection system with Altair SmartWorks



  • 1 x Raspberry Pi.
  • 1 x PIR Sensor


1. Project Schema

This Project is a quick way to create a motion detection system and understand the basics of combining Raspberry Pi, PIR sensor and Altair SmartWorks.

We will also use TheThingBox image on our SD card. To download that image and install it on your raspberry pi, you can visit

Once the installation is done on your SD card, open your browser and type your device IP with the port "1880".

Here is overall diagram of the project:

  • Schema

All the data streams sent by Raspberry Pi are collected and stored in Altair SmartWorks. Altair SmartWorks is a huge database that collects all the information that your sensors send.

In addition to storing data, the true power of Altair SmartWorks is to let you build Apps very quickly with few lines of Groovy code. In this scenario we are going to build a very simple movement detector and counter, and send the data to Altair SmartWorks. In order to improve the App, you can implement a listener

With Altair SmartWorks you can build very complex Apps to support all the logic of your product on the cloud and integrate it with other devices and IT systems. But for now let's keep it simple.


2. Raspberry Pi and PIR sensor. Connections description

The connections in Raspberry Pi with PIR sensor are extremely simple. You can see in the photos below.

  • Raspberry Pi
  • PIR sensor


3. Altair SmartWorks Project and Device Registration


If you´re registered in Altair SmartWorks, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.


Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
You need this number to complete the example.


4. Data Transferred from Raspberry Pi to Altair SmartWorks

First of all, we have to create a box of GPIO box with the following configuration:

  • GPIO

We are now building the call to Altair SmartWorks. In order to build an HTTP request and send the data from Raspberry pi, you usually have to execute something like this:

  • HTTP request
    POST /streams HTTP/1.1
    Accept: application/json
    User-Agent: RaspberryPi-SmartCore
    Content-Type: application/json
    Connection: close
  • Data
    "data":{"uID":"YOUR DATA HERE"}

But we're using here TheThingBox in our SD Card, so you only have to add a "Altair SmartWorks" box and fill the empty "Api Key" and "Device" with your own data:

  • Data configuration


5. Code implemented on Raspberry Pi

This is the Javascript code used inside the "detect Movement" box on the Raspberry Pi:

  • Data configuration


6. Checking Data is collected in Altair SmartWorks

Finally, once you click in the DEPLOY button, all the system is finished and must work. If you get here, your device must be sending streams when the sensor detects some movement.

Its time to test our control panel and check if we have new streams.
Go to “Data management” ? “Data streams” and you will see your data.

  • menu option


7. From here on: Enhancing the project

Now you know how to connect a Raspberry Pi and a PIR sensor to send data to Altair SmartWorks and how to create a simple App to count people in movement. But you can enhance this project in many ways:

  • Try to add a listener in Altair SmartWorks to raise an alarm when an event occurs.
  • Create your own control panel to check the status of the Raspberry Pi, by building an HTML page with a request to our API to ask for the status value of your device
  • Create another device that you can interact with (like a sound alarm) and build an app combining actions between the two devices through Altair SmartWorks