Jump to content

IO4 Bricklet + button to switch on/off Motion Detector


ben_bienne

Recommended Posts

Hi all,

 

Actually I use a Motion Detector and Raspberry Pi with camera to take picture when something is moving. It work good. I can switch on/off the Motion Detector with my computer keyboard

 

Now I would like to do the same without my keyboard and screen but with a manual switch.

 

I have an IO4 Bricklet and a button ON/OFF. I am new in programming and I don't find the way to do this.

 

Can someone help me.

 

Here is my python code

#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
UID = "ksc" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_motion_detector import MotionDetector

import picamera
import time

camera_name = "cameraA_"

# Callback function for end of detection cycle
def cb_detection_cycle_ended():
    if not system_activ:
        return
    print('Detection Cycle Ended (next detection possible in ~3 seconds)')

# Callback function for detected motion
def cb_motion_detected():
    if not system_activ:
        return
    print('Motion Detected')
    with picamera.PiCamera() as camera:
                date_time = int(time.time())
                date_string = str(date_time)
    		camera.resolution = (1024, 768)
	camera.start_preview()
	# Camera warm-up time
	time.sleep(2)
	camera.capture(camera_name + date_string +".jpg")

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    md = MotionDetector(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd

    system_activ = True

    # Don't use device before ipcon is connected

    # Register detected callback to function cb_motion_detected
    md.register_callback(md.CALLBACK_MOTION_DETECTED, cb_motion_detected)

    # Register detection cycle ended callback to function cb_detection_cycle_ended
    md.register_callback(md.CALLBACK_DETECTION_CYCLE_ENDED, cb_detection_cycle_ended)

while True:
        print "Select an option:"
        print "1.    Turn surveillance off" if system_activ is True else "1.    Turn surveillance on"
        print "0.    Exit"
        option = int(raw_input("Option: "))
        if option == 0:
                break
        elif option == 1:
            if system_activ:
                system_activ = False
                print "Surveillance is disabled"
            else:
                system_activ = True
                print "Surveillance is enabled"
        else:
            print "Invalid input!"

    # raw_input('Press key to exit\n') # Use input() in Python 3
    ipcon.disconnect()

 

Thank's in advance

Link zu diesem Kommentar
Share on other sites

Take a look at the interrupt example for the IO-4 Bricklet.

 

You connect your button to pin 0 and a GND pin on the IO-4 Bricklet. Each time the button is pressed or released you get an interrupt callback. You can modify the cb_interrupt() function from the example like this (untested):

 

def cb_interrupt(interrupt_mask, value_mask):
    # test if interrupt is from pin 0 
    if interrupt_mask & (1 << 0):
        # test if button is pressed or released
        if value_mask & (1 << 0):
            print "button released"
        else
            print "button pressed"

            if system_activ:
                system_activ = False
                print "surveillance is disabled"
            else:
                system_activ = True
                print "surveillance is enabled"

 

Now your script can tell if the button was pressed or released. With this information you can then set your system_activ variable.

Link zu diesem Kommentar
Share on other sites

Thank you for the help! It work's!

 

Here is the complete code

#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
UID = "ksc" # Change to your UID
es = "he6" # Change to your UID

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_motion_detector import MotionDetector
from tinkerforge.bricklet_io4 import IO4

import time

system_activ = True

def cb_interrupt(interrupt_mask, value_mask):
    global system_activ
    # test if interrupt is from pin 0 
    if interrupt_mask & (1 << 0):
        # test if button is pressed or released
        if value_mask & (1 << 0):
            print "button released"
        else:
            print "button pressed"

            if system_activ:
                system_activ = False
                print ("surveillance is disabled ", +system_activ)
            else:
                system_activ = True
                print ("surveillance is enabled ", +system_activ)

# Callback function for end of detection cycle
def cb_detection_cycle_ended():
    if not system_activ:
        return
    print('Detection Cycle Ended (next detection possible in ~3 seconds)')

# Callback function for detected motion
def cb_motion_detected():
    if not system_activ:
        return
    print('Motion Detected')

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    md = MotionDetector(UID, ipcon) # Create device object
    io = IO4(es, ipcon) # Create device object
    ipcon.connect(HOST, PORT) # Connect to brickd
   
    # Don't use device before ipcon is connected

    # Register callback for interrupts
    io.register_callback(io.CALLBACK_INTERRUPT, cb_interrupt)

    # Enable interrupt on pin 0 and pin 1
    io.set_interrupt(1 << 0 )

    # Register detected callback to function cb_motion_detected
    md.register_callback(md.CALLBACK_MOTION_DETECTED, cb_motion_detected)

    # Register detection cycle ended callback to function cb_detection_cycle_ended
    md.register_callback(md.CALLBACK_DETECTION_CYCLE_ENDED, cb_detection_cycle_ended)

raw_input('Press key to exit\n') # Use input() in Python 3
ipcon.disconnect()

Link zu diesem Kommentar
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...