The PIR sensors are part of an ongoing development but need to be covered here
even before their full potential is realized.
Currently my sensors merely turn on the outside lights for five minutes
and record the time and state in the database.
I have the outputs of the PIR modules wired directly to the
Raspberry Pi model B GPIO using the Pi Plate Prototyping board and three wire connectors.
The following python program is a multiprocess program.
Each process is loaded with data from the MySQL table of modules.
#!/usr/bin/python
#
-*- coding: utf-8 -*-
from multiprocessing import Process
import RPi.GPIO as GPIO
import httplib2
import time
import pifacecommon.mcp23s17 as p
# This program reads PIR sensors and turns on or off a relay or relays depending on the
# values in the table.
# ==========NOTE!!! The relays this set controls are inverse logic.
# ===================i.e. an inactive output (high) on piface turns the relay on
# =================== and an active low turns it off. This is all dependant on the
# =================== relay board common negative being pulled low by an active
# =================== piface output on the relay board common negative.
# =================== thus on boot the relays are not activated. Until a low is presented
# =================== to the relay board common. Then any relay that does not have an active
# =================== piface output (low) is turned on so the board needs to be activated by
# =================== setting the common negative and both relay controls low at the same time.
# =================== print(“Setting Up”)
# =================== mcp.gpioa.value = mcp.gpioa.value | 28
#=================== print(“Activated GPIO binary 4,8 & 16”)
def pirthread(
id,
pir,
no,
andval,
orval,
):
pir_id = id
pir_no = int(no)
pir_high = int(andval) & 0xff
pir_low = int(orval) & 0xff
lighton = pir + ‘_Light_On’
lightoff = pir + ‘_Light_Off’
debounce = False
while True:
try:
h = httplib2.Http()
if not(debounce):
print (pir_id + ‘ waiting for input ‘ + str(pir_no) \
+ ‘ at ‘ + str(time.strftime(‘%H:%M:%S’)))
# print(str(pir_no) + ” is ” + str(GPIO.input(pir_no)))
debounce = False
GPIO.wait_for_edge(pir_no, GPIO.RISING)
time.sleep(.2)
if GPIO.input(pir_no):
print (pir_id + ‘ setting output ‘ + str(pir_no) \
+ ‘ high’)
mcp.gpioa.value = mcp.gpioa.value & pir_high
(resp, content) = \
h.request(“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′”
+ id
+ “%20Active’,Last_Active%20=%20NOW()%20,Active%20=%20’1’%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20′”
+ pir_id + “‘”, ‘GET’)
(resp, content) = \
h.request(“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′”
+ lighton
+ “‘,Last_Active%20=%20NOW()%20,LastCheck%20=%20NOW()%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20’Coop_Outside_Lights'” , ‘GET’)
(resp, content) = \
h.request(“http://192.168.0.159/wd-db.php?sqlcmd=INSERT%20INTO%20`pir_alarms`(`pir`,%20`alarm`)%20VALUES%20(‘”
+ lighton
+ “‘,%20NOW())”
, ‘GET’)
print (lighton)
# with open(“/var/log/pir_log”, “a”) as f:
# f.write( str(time.strftime(“%m-%d %H:%M:%S”)) + ” – ” + pir_id + chr(13) + chr(10))
time.sleep(300)
(resp, content) = \
h.request(“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′”
+ id
+ “%20Clear’,Last_Inactive%20=%20NOW()%20,Active%20=%20’0’%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20′”
+ pir_id
+ “‘”
, ‘GET’)
(resp, content) = \
h.request(‘http://192.168.0.159/getlight.php’, ‘GET’)
print (content.decode())
if (content.decode() == lighton):
print (“Match”)
print (pir_id
+ ‘ setting output ‘
+ str(pir_no)
+ ‘ low’)
mcp.gpioa.value = mcp.gpioa.value | pir_low
(resp, content) = \
h.request(“http://192.168.0.159/wd-db.php?sqlcmd=UPDATE%20WD_Master%20SET%20Char_Value%20=%20′”
+ lightoff
+“‘,Last_Inactive%20=%20NOW()%20,LastCheck%20=%20NOW()%20WHERE%20Equipment=%20’Coop_RPI’%20and%20System%20%20=%20’Coop_Outside_Lights'”)
else:
print (content.decode() + “<>” + lighton)
else:
debounce = True
GPIO.remove_event_detect(pir_no)
except (KeyboardInterrupt, SystemExit):
print (”’
Ending
Process ”’
+ pir_id)
GPIO.cleanup()
break
mcp = p.MCP23S17()
h = httplib2.Http()
print (“Setting Up”)
mcp.gpioa.value = mcp.gpioa.value | 124
print (“Activated GPIO binary 4,8 & 16”)
process_list = []
(resp, content) = h.request(“http://192.168.0.159/pir-threads.php”,
GET”)
process_values = content.decode().split(“|”)
for myvars in process_values:
if myvars > “”:
tmpvars = myvars.split(“,”)
GPIO.setmode(GPIO.BCM)
GPIO.setup(int(tmpvars[2].strip()), GPIO.IN,
pull_up_down=GPIO.PUD_DOWN)
p = Process(target=pirthread, args=(tmpvars[0].strip(),
tmpvars[1].split()[0].strip(),
tmpvars[2].strip(),
tmpvars[3].strip(),
tmpvars[4].strip()))
process_list.append(p)
# print(tmpvars[0].strip())
# print(tmpvars[1].split()[0].strip())
# print(tmpvars[2].strip())
try:
for Process in process_list:
Process.start()
for Process in process_list:
Process.join()
except (KeyboardInterrupt, SystemExit):
mcp.gpioa.value =mcp.gpioa.value & 227
GPIO.cleanup()
print (”’
All Done with the PIR
”’)
Things to to:
I have added a check to see if it is between sunset and sunrise.
i need to have the PIR units on only at night. During the day they go off all the time
because of reflected sunlight.
I need write code to retain images from the IR cameras
during a sensor alarm.
written by Doug Wyman