總網頁瀏覽量

2012年8月23日 星期四

Arduino PIR eMail Alarm = Arduino + PIR Sensor + WiFi Shield + Google Engine App


The PIR alert can be located at home. Arduino will send messages to Google engine app when PIR detects someone moving. I designed a simple app(email sending) on google app engine (everdevice.appspot.com) to receive messages from arduino, and then send a email to someone you want. If you don't mind the default email sender(my email address), you may try it by my sketch in my blog. In this practice, I replaced Arduino with Motoduino, and on 3G wireless environment(3G share point). Note: if you used AP router, please check the AP router setting whether it is not available by IP address direct hookup.

利用人體紅外線感測器偵測到物體移動時會發出Email通知, 此裝置放在住家或防止小偷侵入的場所是非常適合,如果不介意內設的Email發出者,有興趣的Arduino 網友可以試試底下程式!
主要利用我寫的一個google engine app (everdevice.appspot.com)專門接收arduino送來的message,然後再轉成email送出.

DIY Material:
1. Arduino or Motoduino
2. Arduino WiFi Shield
3.PIR Sensor
4. WiFi AP Router

Arduino Sketch:    
--------------------- Begin of Sketch ----------------


#include <WiServer.h>
// Wireless configuration parameters -
unsigned char local_ip[]= {192,168,1,165};   // IP address of WiShield (modify it)
unsigned char gateway_ip[]= {192,168,1,1};   // router or gateway IP address (modify it)
unsigned char subnet_mask[]= {255,255,255,0}; // subnet mask for the local network
char ssid[] = {"Arduino"};   // max 32 bytes  (modify it)
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2   (modify it)
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"0123456789"}; // max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode; infrastructure - connect to AP; adhoc - connect to another WiFi device
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

uint8 ip[] = {74,125,31,141}; //IP address of everdevice.appspot.com
char hostName[] = "everdevice.appspot.com"; // google engine app
char  url[]="/mygaetest?action=sendmail&to=xxxx@xxxx.xxx&device=DHT11&value=33&body=this";
         // From user: sinocgtchen@gmail.com (default)
         // To user: xxxx@xxxx.xxx (modify it)
         // device : device name (modify it)
         // body : mail body (modify it)

GETrequest getEverDevice(ip, 80, hostName, url);
int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;      
//the time when the sensor outputs a low impulse
long unsigned int lowIn;      

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;

boolean lockLow = true;
boolean takeLowTime;
boolean detected = false;
void setup()
{
   Serial.begin(57600);
   pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");

    Serial.println("WiFi init...");
   WiServer.init(NULL);
    getEverDevice.setReturnFunc(printData);
    delay(50);
}

void loop()
{
  if(PIR_detected() && (detected))
  {  // PIR : HIGH
      getEverDevice.submit();  
      Serial.println("email sending");
   }
   // Run WiServer
   WiServer.server_task();
    delay(1000);
}

void printData(char* data, int len) {
  while (len-- > 0) {
    Serial.print(*(data++));
  }
}

boolean PIR_detected()
{
  boolean bPIR;

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       detected = false;
       if(lockLow){
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;          
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec");
     
         detected = true;
         delay(50);
         }      
         takeLowTime = true;
       
         bPIR = true;

       }
   
     if(digitalRead(pirPin) == LOW){    
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
     
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause,
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){
           //makes sure this block of code is only executed again after
           //a new motion sequence has been detected
           lockLow = true;                      
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
           bPIR = false;
       }
    return bPIR;
}


------------------------------ End of Sketch --------------------------

Video here: http://youtu.be/kLGKAwNrjKk

More Video: http://www.youtube.com/user/sinocgtchen

My Blog(DIY) : http://sinocgtchen.blogspot.com

Motoduino information: http://motoduino.com
My email : sinocgtchen@gmail.com

2 則留言:

  1. 請問google engine app 要怎麼接收Arduino的message?

    回覆刪除
    回覆
    1. 需要自己寫一個app在雲端google engine上面,arduino先連上app server然後傳送自己定義好的協定給app, 有興趣可以study GAE的書.

      刪除