树老大 发表于 2014-11-6 14:08:52

How to build a wireless motion sensor for your Raspberry Pi

Jonathan Evans, the founder of PrivateEyePi has written a tutorial on how to construct a wireless motion sensor and interface it to a Raspberry Pi.In this tutorial I’ll show you how to construct a wireless motion sensor and interface it to a Raspberry Pi minicomputer. I’ve detailed two options that both work very well and have long lasting battery power consumption. The first option involves hacking a key fob remote that has four buttons. We will use the remote to send a signal every time the motion detector senses motion. The four buttons of the remote gives you the ability to run 4 wireless motion sensors.The second option uses a much more sophisticated, but low cost, RF transmitter and receiver made by Ciseco. Ciseco have developed firmware that can be loaded onto the RF modules to give them different personalities (temperature sensor, button, hall affect, relay etc…). The XRF is more expensive than the key fob but it offers much better transmission distances that will penetrate walls a lot better than the key fob remote. The XRF also has the ability to send battery levels and operate different networks that give you an almost limitless amount of sensors that can be connected to the Raspberry Pi.Rough Cost comparison
Quick and dirtyProfessional
Key fob$7XRF receiver$17
Momentary receiver$5XRF transmitter$17
Motion sensor$10Motion sensor$10
Total$22Total$44
Motion sensor or passive infrared sensor (PIR)For the motion sensor I’ve selected the HC–SR501 sensor shown in figure 1. There are a number of these sensor available with varying reliability but I like this particular one because it has three adjustable controls allowing you to fine tune focal distance (3m to 7m) and the amount of time (0.5 seconds to 5 seconds)it waits before a reset. It also has a dip switch for the repeatable trigger mode which will send multiple triggers when motion is detected. It has three pins:Pin 1 : VCC (DC 4.5-20V)Pin 2 : SensorPin 3 : GroundFigure 1 – HC SR01 motion sensorWireless option 1 – Quick and dirtyThe first option uses an off-the-shelf key fob and receiver as shown in Figure 2. This is going to require a moderate level of electronics because you will need to hack into the key-fob for the electronics required to send the signal. It is available at most online electronic stores. With the receiver at around $5 and key-fob at $7 this is a very cost effective option. There are also other similar cheaper units available that do not have a key-fob (internet search OOK on-off-key RF transmitter/receiver). The reason I’ve chosen the key-fob is because it is battery operated and already optimized for sending a signal from a battery operated device. You can also buy very cheap RF transmitter and receivers, however you will need to develop the transmitter and receiver software which is complex and best avoided if possible.The key-fob in figure 1 has four buttons so you have four “channels” you can use. That’s useful if you have a room with multiple sensors (door sensors, motion detectors and window sensors). You can wire them all to one transmitter (yes you need **** Hidden Message *****Figure 8 – The professional wireless motion sensor transmitterFigure 9 is the circuitry for the receiver that connects to the Raspberry Pi. The XRF pins 1 and 10 go to +3V and Ground respectively on the Raspberry Pi. XRF pins 2 and 3 go to pin 10 and 12 of the Raspberry Pi respectively. Pins 10 and 12 on the Raspberry Pi are the transmit and receive serial interface pins.Figure 9 – Professional wireless motion sensor receiverProfessional wireless motion sensor softwareThe Python code in the Code 2 block below will monitor the serial port of the Raspberry Pi and print “Motion Detected” to the screen when motion is detected. After the motion detector reset time limit has been exceeded it will print “Motion detector reset” to the screen.
#!/usr/bin/env python“””Wireless Motion Sensor: Professional using XRFFor the Raspberry Pi“””
import serialfrom time import sleepimport sys
defmain():      # loop until the serial buffer is empty      currvalue=”      # declare to variables, holding the com port we wish to talk to and the speed      port = ‘/dev/ttyAMA0′      baud = 9600
      # open a serial connection using the variables above      ser = serial.Serial(port=port, baudrate=baud)
      # wait for a moment before doing anything else      sleep(0.2)   
      whileTrue:                   while ser.inWaiting():                        # read a single character                        char = ser.read()
                        # check we have the start of a LLAP message                        if char == ‘a’:                              # start building the full llap message by adding the ‘a’ we have                              llapMsg = ‘a’
                              # read in the next 11 characters form the serial buffer                              # into the llap message                              llapMsg += ser.read(11)
                              # now we split the llap message apart into devID and data                              devID = llapMsg                              data = llapMsg
                              if data.startswith(‘BUTTONAON’):                                        print“Motion detected”
                              if data.startswith(‘BUTTONAOF’):                                        print“Motion detector reset”
                        sleep(0.2)
if __name__ == “__main__”:      main()

Code 1 – Professional wireless motion sensor Python code for Raspberry PiOver to youSo now that you have built your wireless motion sensor you will want to put it into service and notify you, or sound an alarm when motion is detected. At PrivateEyePi we have many more tutorials like this that show you how to build your own alarm system (projects.privateeyepi.com).For project parts used in this tutorial please visit www.privateeyepi.com/store

qq3727813 发表于 2015-5-13 21:01:17

kklllllllllllllllllllllllllllllllllllllllllll

qq3727813 发表于 2015-5-13 21:01:25

kklllllllllllllllllllllllllllllllllllllllllll

veronica 发表于 2015-5-13 21:57:04

ding
页: [1]
查看完整版本: How to build a wireless motion sensor for your Raspberry Pi