總瀏覽量

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

2012年8月4日 星期六

Arduino PS2 Keyboard Controls DC Motor

PS2 keyboard connected to Arduino to control a DC motor. The motor speed will be increased 10 units when pressed UPARROW key once, decreased 10 units when pressed DOWNARROW key once.

DIY Material:
1. Motoduino (Arduino + L293D)
2. PS2 Keyboard
3. 5V DC Motor
4. 9V battery
5. Female PS2 Connector

Description:
    Pin connection:
    PS2         Ardunio
      1     -->     3
      2     -->     No Connection
     3      -->     GND
     4      -->     Vcc
     5      -->    2
     6      -->    No Connection  






Arduino  Sketch:

#include <PS2Keyboard.h>

const int DataPin = 3;  // PS2 data pin
const int IRQpin =  2;  // PS2 clock pin

PS2Keyboard keyboard;
const int Motor_E1 = 5; // digital pin 5 of Arduino (PWM)    
const int Motor_E2 = 6;  // digital pin 6 of Arduino (PWM)  
const int Motor_M1 = 7;     // digital pin 7 of Arduino
const int Motor_M2 = 8;    // digital pin 8 of Arduino

int motorSpeed = 0;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(57600);
  Serial.println("Keyboard Ready:");
  
  pinMode(Motor_M1, OUTPUT);

}

void loop() {
  
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();
    
    switch(c)
    {
      case PS2_LEFTARROW:
      case PS2_RIGHTARROW:
      case PS2_UPARROW:
                motorSpeed = motorSpeed + 10;
                if(motorSpeed > 255)
                    motorSpeed = 255;
                break;
      case PS2_DOWNARROW:
                motorSpeed = motorSpeed - 10;
                if(motorSpeed <= 0 )
                    motorSpeed = 0;
                break;
      case PS2_ENTER:
      case PS2_TAB:
      case PS2_ESC:
      case PS2_PAGEDOWN:
      case PS2_PAGEUP:
      case PS2_DELETE:
                  break;
    }
    
    motor(0, motorSpeed);
  }
}

void motor(byte flag, int motorspeed)
{
  digitalWrite( Motor_M1, HIGH);
  analogWrite( Motor_E1, motorspeed);
  
  Serial.println("motor speed: ");
  Serial.println(motorspeed);
}

-------------------------------------------------


Motoduino Information: http://motoduino.com
My Email : sinocgtchen@gmail.com