This is my second blog entry about this project. I must have saved the first one to the wrong area on Element14, and I'll work on moving it after I finish this entry. As I mentioned in part 1, I could not get the special pin extender in time to mount the PIR detector on the Raspberry Pi with the Sense Hat. So, I elected to add another Raspberry Pi to the project and connect all devices other than the Sense Hat to it. It is a Raspnerry Pi B+, which I had as a spare.I connected the HC-SR501 PIR module using 3 pins. I first tried using 3.3V on the Raspberry Pi to go to the HC-SR501 module. I had read a couple of places that the module worked better at that voltage. Well, not for me. I connected Vcc on the HC-SR501(henceforth referred to as PIR) to 5Von the Pi, out on the PIR to GPIO7 on the PI, and ground on the PIR to ground on the PI. I turned the sensitivity down a bit(counter clockwise on the left pot).
I then setup samba on the Raspberry Pi 2 B. I am going to use a simple method to communicate. When motion is detected on the B+, it will create a file in the directory the 2B is sharing. The 2 B runs a script that watches that directory and when the file is created, the fog machine gets turned on, and the notification file is deleted, so we can get the next notification.
Here are the two programs I used for my PIR communications. I have not figured out how to make them look right on the blog, and I apologize for that The B+ has the PIR device and has the samba share mounted in a directory called /lee/lemonpi. The workgroup name I use for samba is lee and the Raspberry Pi 2B has a hostname of lemonpi. Th Pi B+ runs the following Python script(pir-trigger.py).
from subprocess import call
import RPi.GPIO as GPIO #Import GPIO library
import time #Import time library
GPIO.setmode(GPIO.BOARD) #Set GPIO pin numbering
pir = 7 #Associate pin 26 to pir
GPIO.setup(pir, GPIO.IN) #Set pin as GPIO in
print "Waiting for sensor to settle"
time.sleep(2) #Waiting 2 seconds for the sensor to initiate
print "Detecting motion"
while True:
if GPIO.input(pir): #Check whether pir is HIGH
nice_time = time.strftime('%l:%M%p %Z on %b %d, %Y') # ' 1:36PM EDT on Oct 18, 2010'
print "Motion Detected at", nice_time
call(["touch","/lee/lemonpi/notice"])
time.sleep(2) #D1- Delay to avoid multiple detection
time.sleep(0.1)
The Raspberry Pi 2B is running samba. It is sharing a directory called /public and the last few lines of /etc/samba/smb.conf look like this:
[public]
comment = Lemon Pi's public area
path = /public
guest ok = yes
browseable = yes
create mask = 0775
directory mask = 0775
read only = no
The Raspberry Pi 2 B is running the following Python script(turn_on_fog.py):
import pyinotify
from sense_hat import SenseHat
import time
from subprocess import call
sense = SenseHat()
start_time = 0 # start at 0 so first pir event will trigger
class EventHandler(pyinotify.ProcessEvent):
def process_IN_CLOSE_WRITE(self, event):
global start_time
elapsed_time = time.time() - start_time
if elapsed_time > 60: # Only trigger once per this many seconds
start_time = time.time() # reset timer
sense.show_message("Fog envelops you") # or start fog machine here
print "Triggered by closing file name " + event.pathname
# if you want to see when ignored events come in, uncomment this block
# else:
# print "ignoring pir notice", elapsed_time
# if a second trigger comes in while I was busy processing the first, an
# error message will be printed. It may safely be ignored.
call(["sudo","rm", event.pathname])
def watch(filename):
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CLOSE_WRITE
wm.add_watch(filename, mask)
eh = EventHandler()
notifier = pyinotify.Notifier(wm, eh)
notifier.loop()
if __name__ == '__main__':
watch('/public') # trigger on any new file in this directory
The only thing you may not recognize there is import pynotify. If you don't have that installed, you can just do a "sudo pip install pynotify". If you don't have pip installed, just do a "sudo apt-get install python-pip"
This all proves my idea for using 2 PIs should work. I'll have to modify Charles Hantt's programs to incorporate similar logic.