The current AHA project code is posted below.
It can also be downloaded from here : RDIF_Example
//Made By TrustFm for Hw2Sw.com
//Documentation :
//The TAG ID has this format :
// HEX : 2 | 30 31 30 30 34 45 32 37 31 37 | 37 46 | 3 -> Total 14 bytes (chars)
// Start byte : 2 | 10 ascii chars (10 bytes) | 2 ascii chars (bytes) checksum | End byte : 3
// We DEFINE the TAG ID as the 10 ascii chars + 2 ascii chars of the checksum -> total 12 chars/bytes [we remove the start/end bytes]
#include <SoftwareSerial.h> //for serial communication on any digital pins.
//More info : http://arduino.cc/en/Reference/SoftwareSerial
//Type definition of Authorized Person
typedef struct
{
String Name;
String Tag_ID;
} Authorized_Person;
//Type definition of House
typedef struct
{
String Name;
boolean Is_Alarm_System_OK; // True : alarm system not broken -- False : alarm system broken
boolean Is_Alarm_Activated; // True : no-one in the house [armed] -- False : some authorized person in house
Authorized_Person Last_Authorized_Person;
int Max_Duration_To_Exit_Home; // in ms
int Max_Duration_To_Enter_Home; //in ms
} House;
//Authorized_Persons is an array of Authorized_Person type
const int How_Many_Authorized_Persons = 3;
Authorized_Person Authorized_Persons[How_Many_Authorized_Persons];
//House
House My_House;
//Define pins
const int RFID_TX_Pin = 2; // the TX pin that the RDIF Sensor is attached
const int Security_Pin = 3; // the pin that the security line wire is attached. If the security line breaks then the alarm system is considered broken.
const int Green_Led_Pin = 4; // the green led -> deactivated alarm
const int Red_Led_Pin = 5; //the red led -> blinks for Max_Duration_To_Exit_Home and then stays red (alarm is activated)
//Vars for leds ...
unsigned long Current_Ms;
unsigned long Previous_Ms = 0;
long Red_Blink_Duration_Ms = 0; //How many time the RED LED blinks
int Red_Led_State = LOW;
long Red_Blink_Interval = 1000; // interval at which to blink (milliseconds). Only red led can blink !
//RFID ...
String Parsed_Tag_ID, Stored_Tag_ID;
char c;
SoftwareSerial RFID(RFID_TX_Pin , 255); // RX, TX for serial communication on any digital pins.
// RX port : 2 -- TX port : 255 (do not need any TX port)
////////////////////////////////////////////////////////////////////////////
//AUX FUNCTIONS BEGIN
int Get_Authorized_Person_Index(String Tag_ID_To_Test) {
int res = -1; //-1 means not authorized !
for (int i=0; i <= How_Many_Authorized_Persons-1 ; i++) {
if ( Authorized_Persons[i].Tag_ID == Tag_ID_To_Test ) {
res = i;
break;
}
}//end for
return res;
}
String Print_House_Status() {
String Stemp;
Stemp = "STATUS : " + My_House.Name + ". ";
if (My_House.Is_Alarm_System_OK == true) {
Stemp += "The alarm system is ok & ";
} else {
Stemp += "The alarm system is NOT ok & ";
}
if (My_House.Is_Alarm_Activated == true) {
Stemp += "is activated. You have " + (String) My_House.Max_Duration_To_Exit_Home + " ms to EXIT or DEACTIVATE the alarm ... ";
} else {
Stemp += "is NOT activated. ";
}
Stemp += "Last authorized person was " + My_House.Last_Authorized_Person.Name + ". ";
return Stemp;
}
boolean CheckSum_Tag_ID(String Tag_ID) {
boolean res = false;
unsigned int b1,b2,b3,b4,b5,checksum;
//Convert Tag_ID String into array of chars in order to use sscanf
char charBuf[13];
Tag_ID.toCharArray(charBuf, 13);
sscanf(charBuf , "%2x%2x%2x%2x%2x%2x", &b1, &b2, &b3, &b4, &b5, &checksum);
//Control now the TAG ID
if ( (b1 ^ b2 ^ b3 ^ b4 ^ b5) == checksum ) {
return true;
} else {
Serial.println("Tag ID is INVALID");
return false;
}
}
//AUX FUNCTIONS END
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//The setup BEGIN
void setup()
{
//Set Security_Pin in input mode
pinMode(Security_Pin, INPUT);
//Set the leds in output mode
pinMode(Green_Led_Pin, OUTPUT);
pinMode(Red_Led_Pin, OUTPUT);
//Setup serial
Serial.begin(9600);
//Setup RFID serial
RFID.begin(9600);
//Define our Authorized Persons (Name , Tag_ID)
Authorized_Persons[0] = (Authorized_Person) {"Mario" , "01004E27177F"}; //Mario is the default authorized user too
Authorized_Persons[1] = (Authorized_Person) {"John" , "01003D13CFE0"};
Authorized_Persons[2] = (Authorized_Person) {"Anna" , "01004D4B1512"};
//Define My house : Name , Is_Alarm_System_OK , Is_Alarm_Activated , Last_Authorized_Person , Max_Duration_To_Exit_Home , Max_Duration_To_Enter_Home
My_House = (House) { "House at center of Paris" , true , true , Authorized_Persons[0] , 10000 , 10000 };
//Print initial house status
Serial.println(Print_House_Status());
}
//The setup END
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
//The main LOOP BEGIN
void loop(){
//Inizialize :
Stored_Tag_ID = "";
//Control if the alarm system is ok
if ( digitalRead(Security_Pin) == HIGH ) {
My_House.Is_Alarm_System_OK = true;
//Read the RFID TAG BEGIN
RFID.listen(); //Enables the selected software serial port to listen.
//Only one software serial port can listen at a time; data that arrives for other ports will be discarded.
//Any data already received is discarded during the call to listen() (unless the given instance is already listening).
if ( RFID.isListening() ) { //Tests to see if requested software serial port is actively listening.
while( RFID.available() > 0 ){ //Get the number of bytes (characters) available for reading from a software serial port.
//This is data that's already arrived and stored in the serial receive buffer.
c=RFID.read(); //Reads one char/byte at a time
Parsed_Tag_ID += c; //Store the char into the Parsed_Tag_ID string
if ( Parsed_Tag_ID.length() == 14 ) { //The TAG ID has 14 chars in total
if ( (Parsed_Tag_ID[0]==2) && (Parsed_Tag_ID[13]==3) ) { //If the first char is 2 and the last one is 3 then ...
Parsed_Tag_ID = Parsed_Tag_ID.substring(1,13); //Delete the 1st and the 13th (last) char
if ( CheckSum_Tag_ID(Parsed_Tag_ID) == true ) { //Validate the Parsed Tag Id
Stored_Tag_ID=Parsed_Tag_ID;
}
}
Parsed_Tag_ID="";
}//end i have read the 14 chars
}
}
//Read the RFID TAG END
//Set Control authorized persons and set house status
if (Stored_Tag_ID != "") {
//Authorization attempt/check
int Authorized_Person_Index = Get_Authorized_Person_Index(Stored_Tag_ID);
if (Authorized_Person_Index >= 0) { //Then the person is authorized
My_House.Is_Alarm_Activated = (!My_House.Is_Alarm_Activated); //Toggle the Is_Alarm_Activated
My_House.Last_Authorized_Person = Authorized_Persons[Authorized_Person_Index];
//Now do stuff with the authorized+logged persons...
if ( My_House.Is_Alarm_Activated == false ) { //alarm is deactivated
Serial.println( "Welcome back home " + Authorized_Persons[Authorized_Person_Index ].Name + "! -> alarm is DEACTIVATED." );
Serial.println(Print_House_Status());
} else { //alarm is activated
Serial.println( "Bye bye " + Authorized_Persons[Authorized_Person_Index ].Name + "! -> alarm is ACTIVATED. You have " + (String) My_House.Max_Duration_To_Exit_Home + " ms to EXIT or DEACTIVATE the alarm ..." );
Serial.println(Print_House_Status());
}
} else { //The person is NOT authorized !
My_House.Is_Alarm_Activated = true;
Serial.println( "Person NOT recognized ! -> WARNING !!! Alarm is ACTIVATED" );
Serial.println(Print_House_Status());
}
}//if stored id exists
} else { //Alarm is broken !!!
My_House.Is_Alarm_System_OK = false;
Serial.println( "WARNING !!! Alarm broken !!!" );
Serial.println(Print_House_Status());
}
////////////////////////////////////////////////////////////////////////////////////////////////
//SET LEDS BEGIN
if (My_House.Is_Alarm_System_OK == true) {
if (My_House.Is_Alarm_Activated == true) {
digitalWrite(Green_Led_Pin, LOW); // turn the GREEN LED off
/////////////////////////////////////////////////////////////////////////
//Blink async RED light BEGIN
Current_Ms = millis();
Red_Blink_Interval=1000;
if(Current_Ms - Previous_Ms > Red_Blink_Interval) {
Previous_Ms = Current_Ms;
if (Red_Blink_Duration_Ms < My_House.Max_Duration_To_Exit_Home) {
if (Red_Led_State == LOW) // if the LED is off turn it on and vice-versa:
Red_Led_State = HIGH;
else
Red_Led_State = LOW;
digitalWrite(Red_Led_Pin, Red_Led_State); // set the RED LED with the Red_Led_State of the variable
Red_Blink_Duration_Ms = Red_Blink_Duration_Ms + Red_Blink_Interval;
} else { //Time to exit passed the limit !
digitalWrite(Red_Led_Pin, HIGH);
Serial.println( "ALARM ACTIVE ! In this portion of code we will put the sensors to trigger the alarm" );
}
}
//Blink async RED light END
/////////////////////////////////////////////////////////////////////////
} else { //Alarm is deactivated by authorized person
digitalWrite(Green_Led_Pin, HIGH); // turn the GREEN LED on
digitalWrite(Red_Led_Pin, LOW); // turn the RED LED off
Red_Blink_Duration_Ms = 0; //reset the red blinking duration !
}
} else { // the alarm is broken
digitalWrite(Green_Led_Pin, LOW); // turn the GREEN LED off
/////////////////////////////////////////////////////////////////////////
//Blink async RED light BEGIN
Current_Ms = millis();
Red_Blink_Interval=100;
if(Current_Ms - Previous_Ms > Red_Blink_Interval) {
Previous_Ms = Current_Ms;
if (Red_Led_State == LOW) // if the LED is off turn it on and vice-versa:
Red_Led_State = HIGH;
else
Red_Led_State = LOW;
digitalWrite(Red_Led_Pin, Red_Led_State); // set the RED LED with the Red_Led_State of the variable
}
//Blink async RED light END
/////////////////////////////////////////////////////////////////////////
}
//SET LEDS END
////////////////////////////////////////////////////////////////////////////////////////////////
}
//The main LOOP END
///////////////////////////////////////////////////////////////////////////
Video demonstration :
In the next article of the A-H-A project we will present two different types of sensors .
A simple magnetic reed door switch and a Passive InfraRed Motion sensor.
Both of these objects were bought from ebay.