How to Connect a Raspberry to Altair SmartCore and a Relay

0. Introduction

This tutorial will help you build a system that checks whether an electrical circuit is open or closed.
For this, we need a Raspberry PI able to generate a small electric field and make the change of state of a relay.
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 an Alert App on Altair SmartCore by programming a Listener to send a SMS using the SDK



  • 1 x Raspberry Pi. An alternative is to use an Arduino Uno
  • 1 x Tiny Breadboard
  • 2 x Resitance. Value of 330KΩ
  • 1 x Led
  • 1 x Transistor NPN
  • 1 x Relay


1. Project Schema

This Project is a quick way to create an alarm app and understand the basics of combining Raspberry PI and Altair SmartCore.

Here is overall diagram of the project:

  • Schema

Raspbery Pi is programmed to send a data stream to Altair SmartCore and close or open the light circuit.

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 Alert App that sends an email to you in case that Raspberry Pi detects light.

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.


2. Raspberry Pi. Connections description

The connections in Raspberry Pi are extremely simple. You can see in the diagram below.

  • Device Diagram
  • Circuit Diagram
  • Connections


3. Altair SmartCore Project and Device Registration


If youre 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.


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 SmartCore

From Raspberry Pi you have to build a HTTP request and send the data.

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


5. Code implemented on Raspberry Pi

This is the Python code installed in the Raspberry Pi. Note that some values should be replaced by your own values.


6. Checking Data is collected in Altair SmartCore

It's time to test that data is correctly collected in Altair SmartCore. You can use any API-Rest client to send test streams

  • Example API-Rest client
  • Example API-Rest client response

To test our control panel and check if we have new streams go to “Data management” → “Data streams” and you will see your data.

  • Data Stream list


7. Creating an Alert with Altair SmartCore Listeners

A listener in Altair SmartCore can be associated to any hierarchy entity, from Project to Device. If you associate a listener to a project, all devices below the services of that project will be affected. If you associate a listener to service, all devices below the groups of that services will be affected. And so on.

In this example, we create a listener associated with our device. This listener waits for an event to occur in the context of the device and then evaluate the content.

To create a listener go to the control panel, then to “Device management” → “Devices”. Locate your device and click on the name. Then click on the new button in the Listeners tab.

Fill the fields with the following values and save the new listener

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your id device
  • Event to listen: data_persisted
  • If expression:
  • Then expression:

    def email = new Email();
    def device = Device.find(“YOUR DEVICE HERE”);"YOUR EMAIL HERE";
    if ( == "ON") {
    email.message = "Electric circuit closed, status of light: " +;
    } else {
    email.message = "Electric circuit open, status of light: " +;
    device.device_properties.state =;
  • Then rule:
    leave empty
  • Else expression:
    leave empty
  • Else rule:
    leave empty
  • Be sure to have your listener enabled if you want it to be executed

Its time to test our listener. Send a data stream to this device, check your control panel and your email!!!


8. From here on: Enhancing the project

Now you know how to connect a Raspberry Pi device to send data to Altair SmartCore and how to create a simple App with a single Listener and some SDK programing in Groovy. But you can enhance this project in many ways:

  • Try to control the electric energy of a room no matter where you are.
  • Create a control panel to manage the above suggestion.