31 May 2012

Weibo Security System (IR sensor, arduino, php, VB)



23/05/2012


I am renting at the moment but doors have no locks, so i am planning to build a system that sends me a warning whenever the doors is cracked open. I am think to use Weibo (twitter in China), and email. it will also activate the CCTV system and start recording.


The following is the diagram that roughly explains how it works.



现在租房,但门没有锁所以打算做个微博警报器,门要是一打开,电脑就会发微博并 @梁小航OSCAR (我)。原理大概像这图吧,画得不好。最难的部份:用第三方软件发微博的代码已经拼出来了,下一步就是写 PHP 跟 VB 的 interface,最后就写 Arduino跟sensor的代码。。








============================================

24/05/2012

After setting up the demo code on the XAMPP server, i go to 


I then clicked on the button where it said 'login to weibo'

it's not going to ask you for the username and password, it will just log you in with the account you are currently using.
so if you don't want to use your main account for testing, then login with your testing account before carrying out this step.

i then ran into this error:

Fatal error: Call to undefined function: curl_init() 

After googling it, i realized i haven't enable some function in the PHP server.

If this is with a hosting company, you will need to have them install/enable it.

If this is a Windows based system, CURL can be enabled by uncommenting the curl extension line in the php.ini file - extension=php_curl.dll and restarting the web server.

If this is Unix/Linux, PHP must be compiled with CURL support and the libcurl library must be installed.

After the first problem was solved, i had another problem:

发送失败,错误:21321:applications over the unaudited use restrictions!

Solution: Because my app on Weibo isn't authorized yet, so I need to add the test accounts manually on the setting page (open.weibo.com), with the account UID, which is http://www.weibo.com/2106611782, the number bits.

And finally, it works!

==============================================
27/05/2012

Okay, I am so happy my idea of doing this project on Weibo I posted few days ago got reposted by some Arduino guys, and quite a few people noticed and commented! hoo hoo! I guess that's quite a motivation for myself.

On friday, i did some research and (finally?) got the VB email program working, although it requires a library called EASendmail to do so. 

Today, I also got the PHP code for sending Weibo twitt using address bar down. Now it's working in the VB program... 

Next step would be to use arduino to send command to trigger this via USB...

now the problem comes to the CCTV... how am i going to turn on the CCTV and start recording... where to store the data to... one option would be to take videos every 10 seconds and to store the 10-second video files onto the dropbox folder, and these files will be uploaded immediately. recording period might be  a reasonable time like 5 minutes? or 10...

==============================================

30/05/2012

Diagram for a friend in the internet who is also doing similar project




PS: for the photo-transistor Resistor, I had to use something around 100k to achieve good result at the end!




==============================================

31/05/2012



IR sensor and Arduino Source Code (draft v1, haven't tested yet on arduino :P)






#if defined(ARDUINO) && ARDUINO > 18   // Arduino 0019 or later
#include <SPI.h>
#endif
#include <Ethernet.h>
//#include <EthernetDNS.h>  Only needed in Arduino 0022 or earlier
#include <Arduino2Weibo.h>

// Ethernet Shield Settings
byte mac[] = { 0xDE0xAD0xBE0xEF0xFE0xED };

// substitute an address on your own network here
byte ip[] = { 1921682250 };

// Your Keys to Weibo (get it from http://arduino2weibo.sinaapp.com/
Weibo weibo("YOUR-TOKEN-HERE","YOUR-TOKEN-SECRET-HERE");

// ======== Pin ==========
int emitPin = 13;       // Infrared LED pin
int analogPin = 3;      // IR sensor connected to analog pin

int ambientVal = 0;     // variable to store the 
int thresVal = 0;       // variable to store the LED on value

void setup()
{
        pinMode(emitPin, OUTPUT);
        Ethernet.begin(mac, ip);
        Serial.begin(9600);

        Serial.println("sending system status ...");
        
        int wStatus = SendWeibo("System is Okay, Calibrating IR system...");
        
        // give you time to close door!
        delay(10000);
        
        // calibrate IR system
        Calibrate();
        
        // finished calibrating!
        int wStatus = SendWeibo("IR system calibrated, System is now in operation!");
        
}

void loop()
{
        if (CheckIR() == 1){
                int wStatus = SendWeibo("#Warning# Door is open!!!");
                // stop for 10 minutes
                delay(600000);
                Calibrate();
        }
        
        delay(500);

}

//=======================================================================
//============================ Functions ================================
//=======================================================================

int SendWeibo(char msg[]){

        if (weibo.post(msg)) {
                int status = weibo.wait();
                if (status == 200){
                        Serial.println("Weibo sent Successfully");
                        return 1;
                }
                else{
                        Serial.println("Weibo sent failed(#1)");
                        return 0;
                }
        }
        else{
                Serial.println("Weibo sent failed(#2)");
                return 0;
        }
        
}

int Calibrate(){

        // Emitter Off, taking ambient value
        digitalWrite(emitter,LOW);
        delay(500);
        ambientVal = analogRead(analogPin);  // read the input pin
        
        // Emitter On, taking LED ON value
        digitalWrite(emitter,LOW);
        delay(500);
        thresVal = analogRead(analogPin);  // read the input pin
        
        // taking thresVal as the mid value of off and on IR measurement
        thresVal = (thresVal + ambientVal)/2
        
        return 1;
}

int CheckIR(){
        int curVal = analogRead(analogPin);             // read the input pin  

        // if curVal is below threshold, means door is open
        if (curVal < thresVal){
                return 1;
        }
        else {
                return 0;
        }
        
}




========================================================
04/06/2012

The IR sensor code I wrote last time isn't working very well, so I re-wrote it with fewer lines and better performance as well :)

I dumped the calibration function, and take the on and off IR value each time and compare them, which gives a more accurate and responsive result.

result:




// ======== Pin ==========
int emitPin = 3;       // Infrared LED pin
int analogPin = A5;      // IR sensor connected to analog pin

int ambientVal = 0;     // variable to store the 
int thresVal = 0;       // variable to store the LED on value

void setup()
{
        pinMode(emitPin, OUTPUT);
        pinMode(analogPin, INPUT);

        Serial.begin(9600);
        Serial.println("Arduino Starting ... Close door now...");
     
        // give you time to close door!
        delay(5000);
     
        // finished calibrating!
        Serial.println("Sensor operation started!");
     
}

void loop()
{
  if (CheckIR() == 1){
    Serial.println("Door is closed!");
  }
  else{
    Serial.println("door is open!");
    delay(10000);
  }

  delay(500);
}

//=======================================================================
//============================ Functions ================================
//=======================================================================



int CheckIR(){
  digitalWrite( emitPin  ,LOW);
  delay(200);
  int offVal = analogRead(analogPin);             // read the input pin  
  digitalWrite( emitPin  ,HIGH);
  delay(200);
  int onVal = analogRead(analogPin);             // read the input pin    
  digitalWrite( emitPin  ,LOW);
 
  // if curVal is below threshold, means door is open
  if (onVal > (offVal+onVal)/2*1.2){
    return 1;
  }
  else {
    return 0;
  }
}








No comments:

Post a Comment

Note: only a member of this blog may post a comment.