presence

 

A few days ago I have installed a few PIR sensors around the house and connected them to the old Arduino Duemilanove which was lying useless after I switched the system to two Mega boards. The PIRs I got from eBay, they run off 12V and output TTL signal which stays on as long as movement is detected, so I just connected them directly to Arduino's digital inputs.


This third Arduino is running a simple program based on the debouncing routine from here (I think). Here's the full code:



const int maximum = 12;

int i, current;

int previous[maximum];

int state[maximum];

long time[maximum];


void setup() {

 

  for ( i = 2; i < maximum; i++ ) {

    pinMode( i, INPUT );

    previous[i] = LOW;

    state[i] = LOW;

    time[i] = 0;

  }


  Serial.begin( 9600 );

 

  Serial.println( "Ready." );

 

}



void loop() {


  for ( i = 2; i < maximum; i++ ) {

   

    current = digitalRead( i );

   

    if ( current != previous[i] ) {

     

      time[i] = millis();

     

    }

   

    if ((millis() - time[i]) > 50) {

      // whatever the switch is at, its been there for a long time

      // so lets settle on it!

    

      if ( previous[i] != state[i] ) {

        Serial.print( i );

        Serial.print( ':' );

        Serial.print( current );

        Serial.print( '!' );


        state[i] = current;

      }

    }


    previous[i] = current;


  }


}



The Server is now running another thread which is pooling this new Arduino for data, and updating Sample table in MySQL when movement is detected. On top of that I concocted a quick hack in form of a script which runs every minute (I also upgraded the scripts engine so I can decide if the script is to be run every second or every minute) to update Probe table which is then used by the clients to display presence in UI. I was also having some fun with Skype API and added another quick hack, the system will send me a Skype message whenever presence in any zone changes to 1 after more than 15 minutes. Should you wish to be on the recipients list, gimme a shout ;)

Monday, 28 June 2010

 
 

next >

< previous

© 2009 - 2010 Piotr Farbiszewski. All rights reserved.