Hi nerdz, today i’ll show you how to easily control remotely your room with a stupid motion detector, a piro sensor (or you can use other sensors, like ultrasonic, infrared, light sensor, pressure, etc etc…) and two lines of PHP code and C.
* Useful if you want to know if anyone enter in your room and the time that remains there! *
You can get this sensor from AirWick deo! 😎
How it works?
Your piro sensor, when detect a motion, notify arduino, that was programmed to send to serial port a value (“m” or “n”). These value are interpreted by a serial reader (c# program, objective-c, java, c++, or somethig else) that call a PHP page that send email if is enabled sending mode. Second php page, permit you to control arduino (enable/disable/show logs/clear all).
Steps
Connect your piro sensor to arduino, easy way, like this:
After that, write your simple arduino code:
/*
* Room Spy with Piro sensor
* (c)2012 - Alberto Pasca
* www.albertopasca.it
*/
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) { // MOTION START
Serial.println("m");
pirState = HIGH;
}
} else {
if (pirState == HIGH) { // MOTION STOP
Serial.println("n");
pirState = LOW;
}
}
}
Well, test your code, Arduino can able to send to serial “m” or “n”. If do this, you’re ok!
Now write a serial reader (read these (p1 || p2) old post to know how) and call your php page. Here C# snippet:
try {
SerialPort port = new SerialPort( "COM9", 9600, Parity.None, 8, StopBits.One );
port.Open();
string data = string.empty;
while ( true ) {
data = port.ReadLine();
}
} catch ( Exception ee ) { Console.WriteLine( ee.Message ); }
My Serial Reader, write this output (“ok” or “ko”, based on php output):
Now, i create a control page, that do this simple things. If enable, write to roomcmd.dat “1″ value, else write “0″. Log and Clear read or clean log file.
Last step, is the “mail script” (called from SerialReader), that read values from file “roomcmd.dat” that can contains “0″ or “1″. Decide to send or not notifications.