Close

Alarm System using Aquila

A project log for Aquila IoT Platform

Internet of Things rapid prototyping platform.

RodmgRodmg 08/20/2014 at 17:210 Comments

In this tutorial we will make an alarm that works automatically, although it could be also manually triggered.

4 Altairs will be used; one will be the Hub, other the Altair with the buzzer, and the two remaining will have one of the two sensors that will trigger the alarm.

Also, some concepts that may cause you some confusion will be explained.

It’ll have two sensors that will tell the Altair that has the buzzer when to sound.


Events are happenings that occur within an Altair and that we do not have direct control over it through the Hub.

This events sorted by sensor are:

Movement Sensor

Door Sensor

As you can see, an event would be something like a notification from an Altair to all the other Altairs plugged through the network.

And, through the hub is where you configure if another Altair “listens” and reacts with an Action. All this through the Interactions menu inside the hub.

(Also, actions can be controlled directly through the hub. Imagine it as a TV remote controller).

Knowing this, here we list all the actions that the Altair with the buzzer will have.

Knowing how the alarm works, here are the needed components listed:

In the next section the code for each Altair will be shown; with its explanation.

Alarm Diagram:

Movement Sensor Diagram:

Door Sensor Diagram:

Hub Screenshot:

Interaction configuration on Hub:

Code for the Alarm:

#include <Wire.h>
#include <Aquila.h>

#define BUZZER 9
#define LED 14

bool alarmActive = false;
bool alarmArmed = false;

bool alarmArm(uint8_t param, bool gotParam)
{
  alarmArmed = true;
}

bool alarmDisarm(uint8_t param, bool gotParam)
{
  alarmArmed = false;
  alarmActive = false;
}

bool alarmOff(uint8_t param, bool gotParam)
{
  if(alarmArmed)
    alarmActive = false;
}

bool alarmOn(uint8_t param, bool gotParam)
{
  if(alarmArmed)
    alarmActive = true;
}

void setup()
{
  pinMode(BUZZER, OUTPUT);
  pinMode(LED,OUTPUT);
  
  Aquila.begin();
  Aquila.setClass("mx.makerlab.alarma");
  Aquila.setName("Alarma");
  
  Aquila.addAction("Alarm Off", alarmOff);
  Aquila.addAction("Alarm On", alarmOn);
  
  Aquila.addAction("Armada", alarmArm);
  Aquila.addAction("Desarmada", alarmDisarm);
  
  Aquila.sendClass(BROADCAST);
}

void loop()
{
  Aquila.loop();
  
  if(alarmActive)
  {
    digitalWrite(LED,LOW);
    for(int i = 0; i < 100; i++)
    {
      tone(BUZZER, i*10);
      Aquila.loop();
      delay(10);
    }
    
  }
  else
  {
    noTone(BUZZER);
    digitalWrite(LED,HIGH);
  }
}

Code for the Movement sensor:

#include <Wire.h>
#include <Aquila.h>

#define MOV 18
#define LED 15

Event movimiento;

void setup()
{
  pinMode(MOV, INPUT);
  pinMode(LED, OUTPUT);
  
  Aquila.begin();
  Aquila.setClass("mx.makerlab.alarma");
  Aquila.setName("Sensor");
  
  movimiento = Aquila.addEvent("Hay movimiento");
        
  Aquila.sendClass(BROADCAST);
}

void loop()
{
  Aquila.loop();
  
  if(digitalRead(MOV) == HIGH)
  {
    digitalWrite(LED,LOW);
    Aquila.emit(movimiento);
  }
  else 
  {
    digitalWrite(LED,HIGH);
  }
  
}

Code for the Door Sensor:

#include <Wire.h>
#include <Aquila.h>

Event dooropened;
Event doorclosed;

const int doorSensor = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int doorState = 0;         // variable for reading the pushbutton status
int prevState = 0;

void setup() 
{
  Aquila.begin();
  Aquila.setClass("sensor.magnetic");
  Aquila.setName("Puerta");
  
  dooropened = Aquila.addEvent("Door Opened");
  doorclosed = Aquila.addEvent("Door Closed");
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(doorSensor, INPUT_PULLUP);
  
  prevState = digitalRead(doorSensor);
  digitalWrite(ledPin, HIGH);
  
  Aquila.sendClass(BROADCAST);
}

void loop() 
{
  Aquila.loop();
  // read the state of the pushbutton value:
  doorState = digitalRead(doorSensor);


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (prevState != doorState)
  {
    prevState = doorState;
    if (doorState == HIGH) 
    {
      Aquila.emit(dooropened);
      // turn LED on:
      digitalWrite(ledPin, LOW);
    }
    else 
    {
      Aquila.emit(doorclosed);
      // turn LED off:
      digitalWrite(ledPin, HIGH);
    }
  }
}

Discussions