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
- “No presence detected”
- “Presence detected”
Door Sensor
- “Door is closed”
- “Door is opened”
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.
- “Stop alarm” (Parar)
- This will make the Altair to stop buzzing, even if it was activated through the Hub or through the sensors; If the alarm is “Armed” it will remain armed even after executing “Alarm Off”.
- “Sound alarm” (Sonar)
- It’s the manual execution of the alarm, it could be used as a “Sound check”
- “Arm” (Armar)
- This will make all the sensors to be on guard and will set the Altair with the buzzer ready to make some noise.
- “Unarm" (Desarmar)
- This will make the Altair with the buzzer to ignore everything that the sensors tell; there is no way it would buzz, even it you execute the action “Sound alarm”
Knowing how the alarm works, here are the needed components listed:
- 4 Altair
- 1 Buzzer
- 1 Movement Sensor
- The wiring order may change due model variations, please check how your sensor is plugged before wiring.
- 1 Door sensor
- 1 Protoboard
- Size doesn’t matter, as it will be only used for plugging the buzzer
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
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.