Eric yuan
Published © GPL3+

Trespasser Detector

This is a project which helps you to detect the one who break into your rooms without your authorization.

BeginnerFull instructions provided3 hours896
Trespasser Detector

Things used in this project

Story

Read more

Code

code

C/C++
The code is actually pretty sample, I use the web IDE from particle to finish the coding. One key factor is the method to detect the door status and another key is update the status to cloud after detect the alarm, Also there are some other features such as cease the alarm mandatory etc.
#include<application.h>

#define SWITCH_1 D0
#define SWITCH_2 D1
#define RELIEF_DET D2
#define BEEP    D3


#define DOOR_NONE  0    //the initial status of the door. 
#define DOOR_CLOSE 1
#define DOOR_OPEN  2
#define DOOR_ERROR 3

#define DEBUG

#define YES     2
#define NO      1
#define INITIAL 0

#define BEEP_ON 1
#define BEEP_OFF 0

#define THRESHOLD_TIME 5000

int doorState=DOOR_NONE;

String doorStatus;

unsigned char door_detect(void)
{
    static unsigned char flag_reed1=0;
    static unsigned char flag_reed2=0;
    static unsigned char stage=0;
    
    unsigned char door_status=0;

    switch(stage)
    {
        case 0:
            flag_reed1=0;
            flag_reed2=0;
            if((digitalRead(SWITCH_1)==0)||(digitalRead(SWITCH_2)==0))  //if one of the swtich had been activated
                stage=1;
            break;  
        case 1:     //detect which switch had been activated
            if((digitalRead(SWITCH_1)==0)&&(digitalRead(SWITCH_2)==1))  
            {
                flag_reed1=1;
                flag_reed2=0;
                stage=2;
            }
            else
           if((digitalRead(SWITCH_1)==1)&&(digitalRead(SWITCH_2)==0))  
            {
                flag_reed1=0;
                flag_reed2=1;
                stage=2;
            }
            else
            if((digitalRead(SWITCH_1)==1)&&(digitalRead(SWITCH_2)==1))  //hardware error
            {
                stage=3;
            }
            else
            if((digitalRead(SWITCH_1)==0)&&(digitalRead(SWITCH_2)==0))  //hardware error
            {
                stage=3;
            }
            break;
        case 2: //detect another switch
            if(flag_reed1==1)
            {
                if((digitalRead(SWITCH_1)==1)&&(digitalRead(SWITCH_2)==0))  
                {
                    door_status=DOOR_OPEN;
                    stage=0;//go back to the original stage
                    Serial.println("Detect open door action");
                } 
                //need to add some code to determine the interval time btween those two switch.
            }
            else
            if(flag_reed2==1)
            {
                if((digitalRead(SWITCH_1)==0)&&(digitalRead(SWITCH_2)==1))  
                {
                    door_status=DOOR_CLOSE;
                    stage=0;//go back to the original stage
                    Serial.println("Detect close door action");
                }  
                  //need to add some code to determine the interval time btween those two switch.
            }
            break;
        case 3:
            door_status=ERROR;
            stage=0;
            break;
        default:
            break;
    }
    return door_status;
}


void beepAlarm(unsigned char state)
{
    if(state==BEEP_ON)
       digitalWrite(BEEP,HIGH);
    else
        digitalWrite(BEEP,LOW);
}

int doorDetecter(String led)
{
    return 1;
}


void setup()
{
  //  Particle.variable("doorState", &doorState, INT);
//    Particle.function("status", doorDetecter);
    pinMode(SWITCH_1,INPUT);
    pinMode(SWITCH_2,INPUT);
    pinMode(RELIEF_DET,INPUT);
    pinMode(BEEP,OUTPUT);
    
    pinMode(D7,OUTPUT);
    digitalWrite(D7,LOW);
    digitalWrite(SWITCH_1,HIGH);
    digitalWrite(SWITCH_2,HIGH);
    digitalWrite(RELIEF_DET,HIGH);
    digitalWrite(BEEP,LOW);
    Serial.begin(9600);
}



void loop() {
    static unsigned long olderTime;
    static unsigned long ExitTime;
    static unsigned char reliefAlarm=0;
    static unsigned char forceExit=0;
    static unsigned char firstPress=0;
    unsigned char temp=0;
    unsigned int publicValue=0;
    
    
    if(doorState==DOOR_NONE)        //detect the door status only when the status is unknow. will not process it if the last 
                                    //state is op and not realified
    {
        temp = door_detect();
        
        if(temp==DOOR_OPEN)        
        {
            olderTime=millis();
            doorState=DOOR_OPEN;
            digitalWrite(D7,HIGH);  //turn on the led to indicate the door status
            Serial.println("record the current times for alarm");
        }
        else
        if(temp==DOOR_CLOSE)       
        {
            olderTime=millis();
            doorState=DOOR_CLOSE;
            digitalWrite(D7,LOW);   //turn off the led once closed the door
            Serial.println("record the currnet time");
            
        }   
    }
    
    if(doorState==DOOR_OPEN)        //start to detect the relief button 
    {
        if((digitalRead(RELIEF_DET)==0)&&((millis()-olderTime)<=THRESHOLD_TIME))  //detect whether the user had pressed the relief button or not.set up the flag if pressed
        {
            Serial.println("Relief alarm button detected");
            reliefAlarm=YES;
            digitalWrite(D7,LOW);  //turn off the led once the relief button had pressed
        }
        else
        if((millis()-olderTime)>THRESHOLD_TIME)  //if 5 seconds passed after open the door,maybe it's too long
        {
            doorState=DOOR_NONE;    //make the door state go to initial mode
            if(reliefAlarm==YES)        //if in 5s, the user had pressed the relief button
            {
                //do nothing here since the batton had pressed
                digitalWrite(D7,LOW);   //turn off the led once the alarm had turned off
                Serial.println("Relief the alarm,go back to initial stage");
                reliefAlarm=INITIAL; //go back to the initial alarm status
                beepAlarm(BEEP_OFF);   // just incase it turned on accidenlty
            }
            else
            {
                reliefAlarm=NO; //not reliefed the alarm
                digitalWrite(D7,HIGH);
                Serial.println("Detect an alarm");
                forceExit=1;
                beepAlarm(BEEP_ON);
             //   doorStatus="Alarmed";
                bool success;
                success=Particle.publish("doorStatus","Alarmed");
                if(!success)
                    Serial.println("Failed to publish this event");
                else
                    Serial.println("Event had published successfully");
            }
        }   
    }
    else
    if(doorState==DOOR_CLOSE)       //no other action when close the door,but need to change back five seconds later
    {
        if((millis()-olderTime)>=THRESHOLD_TIME)
        {
            Serial.println("Go back to initial stage");
            doorState=DOOR_NONE;
        }
    }
        
    if(forceExit==1)//method to relief the alarm after detect an alarm. 
    {
        if((digitalRead(RELIEF_DET)==0)&&(firstPress==0))  //which mean the user pressed the relief button after five seconds later.
        {
            firstPress=1;
            olderTime=millis();
        }
        if(((millis()-olderTime)>=THRESHOLD_TIME)&&(digitalRead(RELIEF_DET)==0))  //if the button had pressed down for more than 5 seconds,then,also relief the alarm
        {
            firstPress=0;   //go to initial stage;
            forceExit=0;    //go to initial stage;
            reliefAlarm=INITIAL;
            beepAlarm(BEEP_OFF);
            digitalWrite(D7,LOW);
            Serial.println("Force relief the alarm,go back to initial stage");
        }
        
            
    }
}

Credits

Eric yuan

Eric yuan

5 projects • 1 follower

Comments

Add projectSign up / Login