How to connect an Arduino Yun to Altair SmartWorks and build an Alert System.

0. Introduction

This easy tutorial helps you build an Alert System for your home or office.
To do this, we use an Arduino Yun able to detect light and the Altair SmartWorks Engine to send alerts by email.
In this project you will learn how to:

  • Connect an Arduino Yun to Altair SmartWorks to send data streams using Altair SmartWorks and cURL
  • Build an Alert App on Altair SmartWorks by programming a Listener to send an email using the SDK

Difficulty:

Ingredients

  • 1 x An Arduino Yun
  • 1 x Tiny Breadboard
  • 1 x Resitance. Range between 200KΩ and 10KΩ
  • 1 x Foto Sensor. Try to improve the project with a motion detection sensor

TOP

1. Project Schema

This project is a quick way to create an alarm app and understand the basics of combining Arduino Yun and Altair SmartWorks.

  • Schema

The Arduino Yun is programmed to send a data stream to Altair SmartWorks stating whether or not it's receiving light.

All the data streams sent by Arduino Yun 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 Alert App that sends an email to you when Arduino Yun detects light.

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.

Although this Alert App is very simplistic, you can use it to detect if someone enters a dark room and turns on the lights. In fact this project is quite useful in our data-center, where the lights are always turned off.

TOP

2. Arduino Yun. Connections description

The connections in Arduino Yun are extremely simple. You can see in the diagram below.

  • Diagram

TOP

3. Altair SmartWorks Project and Device Registration

Device

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.

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 Arduino Yun to Altair SmartWorks

In this tutorial we will use Linino's installed cURL. If you are not familiar with cURL you might want to check the official page: http://curl.haxx.se/ for further details. You can also follow this tutorial with other REST clients such as hurl (http://www.hurl.it/) or Poster plugin (https://addons.mozilla.org/en-us/firefox/addon/poster/) to check that you're doing thing right.

Your payload, the stream to send to Altair SmartWorks, will be:

This is the Arduino code to build the payload (see the whole code here):

If you want more information about how to send streams to Altair SmartWorks using the Arduino Yun, use the "How to send streams" → "ArduinoYUN" tutorial (Arduino Yun Streams Tutorial)

TOP

5. Code implemented on Arduino Yun

This is the code for the Arduino Yun. Note that some values should be replaced by your own values.

TOP

6. Checking Data is collected in Altair SmartWorks

If you get here, your device must be sending streams when you turn on and turn off the lights.

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
  • new data

TOP

7. Creating an Alert with Altair SmartWorks Listeners

A listener in Altair SmartWorks 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 service 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 “Rules” → “Listeners” and click on the new button. Select "Device" as "entity type" and select your device on the "Id" field.

Fill the fields with the following values and continue to the next step.

  • Name: The name that you prefer
  • Description: The description that you prefer
  • Entity type: Device
  • Entity id: Your device id_developer (defaultDevice@example.example)
  • Event to listen: "Event Data Persisted"
  • Be sure to have your listener enabled if you want it to be executed
  • Click on the "Next Step: design the code" button to visually design the listener functionality
  • Listener creation

In the Flow editor we'll need to follow these steps:

  • Step 1: Drag and drop in the editor workspace a "switch" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "If lights ON"
    If condition: select "if" and fill in the following field with the code context.data.light=="ON"
    Else condition: click on the button "+ add" and select "else"
    Click on the button "Done" to apply the changes
  • Flow designer Step 1: Switch node
  • Step 2: Drag and drop in the editor workspace a "send email" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "Send ON email"
    To: [[Your Email Here]]
    Subject: Alarm ON
    Message: Someone has turned on the light
    Click on the button "Done" to apply the changes
  • Step 3: Drag and drop in the editor workspace a "send email" node and Double-click it to configure it with the values:
    Label: descriptive name of the node in the editor view. For example "Send OFF email"
    To: [[Your Email Here]]
    Subject: Alarm OFF
    Message: Someone has turned off the light
    Click on the button "Done" to apply the changes
  • Step 4: Connect the "if" node ports to their corresponding "send email" node. From left to right, the ports correspond to the conditions added in the first step from top to bottom.
  • Flow designer Step 2, 3 and 4: Node Send Email configuration and connections
  • Step 5: Click on the "Save" button and check the listener data in the "Show" screen. You can have a look at the generated source code and use it later for debugging purposes.
  • Flow designer Step 5: Resulting listener + source code

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

TOP

8. From here on: Enhancing the project

Now you know how to connect an Arduino Yun device to send data to Altair SmartWorks 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 add a motion sensor to the Arduino Yun and send at least two values: light and motion status
  • Create your own control panel to check the status of the Arduino Yun, 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

TOP