How to connect a Raspberry Pi and a PIR sensor to Altair SmartCore 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 SmartCore to send data streams using Altair SmartCore HTTP REST API
  • Build a motion detection system with Altair SmartCore

Difficulty:

Ingredients

  • 1 x Raspberry Pi.
  • 1 x PIR Sensor

TOP

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

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

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 SmartCore. Altair SmartCore is a huge database that collects all the information that your sensors send.

In addition to storing data, the true power of Altair SmartCore 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 SmartCore. In order to improve the App, you can implement a listener

With Altair SmartCore 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.

TOP

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

TOP

3. Altair SmartCore Project and Device Registration

Device

If you´re registered in Altair SmartCore, 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.

Apikey

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

TOP

4. Data Transferred from Raspberry Pi to Altair SmartCore

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 SmartCore. 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
    Host: api.altairsmartcore.com
    Accept: application/json
    User-Agent: RaspberryPi-SmartCore
    Content-Type: application/json
    Apikey: YOUR APIKEY HERE
    Content-Length: YOUR CONTENT LENGTH HERE
    Connection: close
  • Data
    {
    "protocol":"v2",
    "device":"YOUR DEVICEs ID_DEVELOPER HERE",
    "at":"now",
    "data":{"uID":"YOUR DATA HERE"}
    }

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

  • Data configuration

TOP

5. Code implemented on Raspberry Pi

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

  • Data configuration

TOP

6. Checking Data is collected in Altair SmartCore

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

TOP

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 SmartCore 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 SmartCore 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 SmartCore
TOP