hanspython
Posts: 11
Joined: Tue Aug 21, 2018 7:32 am

[SOLVED] pir motion detection

Mon Sep 10, 2018 8:34 am

Hi to all

Is there a possibility to make a log file from the pir now everything is on the screen but i want the output in a log file is that possible?

This i my standard:
# HC-SR501 motion sensor
# https://raspberrytips.nl/hc-sr501-beweg ... pberry-pi/


import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
PIN = 26
GPIO.setup(PIN, GPIO.IN)

print "Start sensor..."
time.sleep(2)
print "Sensor alarm geactiveerd let op!.."

while True:
if GPIO.input(PIN):
print "Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S")) This line in a log file

time.sleep(2)
time.sleep(0.1)
Last edited by hanspython on Mon Sep 10, 2018 2:03 pm, edited 1 time in total.

Brandon92
Posts: 870
Joined: Wed Jul 25, 2018 9:29 pm
Location: The Netherlands

Re: pir motion detection

Mon Sep 10, 2018 9:07 am

I found this youtube video very helpful for this question.

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: pir motion detection

Mon Sep 10, 2018 9:11 am

if you run it from the command line you can simply redirect the output to file by appending

Code: Select all

 >> /home/pi/logs/mylogfile.txt
to the end of the program command ie

Code: Select all

$ ./myscript.py >> /home/pi/logs/mylogfile.txt 
edit the path to the file must exist, the file will be created or if it exists be appended to.

gordon77
Posts: 5074
Joined: Sun Aug 05, 2012 3:12 pm

Re: pir motion detection

Mon Sep 10, 2018 9:21 am

Code: Select all

import RPi.GPIO as GPIO
import time
logfile = 'log2.txt'

GPIO.setmode(GPIO.BOARD)
PIN = 26
GPIO.setup(PIN, GPIO.IN)

print ("Start sensor...")
time.sleep(2)
print ("Sensor alarm geactiveerd let op!..")

while True:
    if GPIO.input(PIN):
        print ("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))) #This line in a log file
        with open(logfile, "a") as file:
            file.write("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))+ "\n")

time.sleep(2)
time.sleep(0.1)

hanspython
Posts: 11
Joined: Tue Aug 21, 2018 7:32 am

Re: pir motion detection

Mon Sep 10, 2018 11:13 am

gordon77 wrote:
Mon Sep 10, 2018 9:21 am

Code: Select all

import RPi.GPIO as GPIO
import time
logfile = 'log2.txt'

GPIO.setmode(GPIO.BOARD)
PIN = 26
GPIO.setup(PIN, GPIO.IN)

print ("Start sensor...")
time.sleep(2)
print ("Sensor alarm geactiveerd let op!..")

while True:
    if GPIO.input(PIN):
        print ("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))) #This line in a log file
        with open(logfile, "a") as file:
            file.write("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))+ "\n")

time.sleep(2)
time.sleep(0.1)
Wonderful its working thanks

hanspython
Posts: 11
Joined: Tue Aug 21, 2018 7:32 am

Re: pir motion detection

Mon Sep 10, 2018 11:32 am

hanspython wrote:
Mon Sep 10, 2018 11:13 am
gordon77 wrote:
Mon Sep 10, 2018 9:21 am

Code: Select all

import RPi.GPIO as GPIO
import time
logfile = 'log2.txt'

GPIO.setmode(GPIO.BOARD)
PIN = 26
GPIO.setup(PIN, GPIO.IN)

print ("Start sensor...")
time.sleep(2)
print ("Sensor alarm geactiveerd let op!..")

while True:
    if GPIO.input(PIN):
        print ("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))) #This line in a log file
        with open(logfile, "a") as file:
            file.write("Let op er zijn mensen buiten de afgesproken tijd lokatie Tilburg " + (time.strftime("%H:%M:%S"))+ "\n")

time.sleep(2)
time.sleep(0.1)
Wonderful its working thanks
If i interrupt the program i get this;
File "hcsr501.py", line 22, in <module>
time.sleep(0.1)

gordon77
Posts: 5074
Joined: Sun Aug 05, 2012 3:12 pm

Re: pir motion detection

Mon Sep 10, 2018 11:57 am

How are you interrupting?

If using ctrl&C it's simply showing where it was when you stopped it.

hanspython
Posts: 11
Joined: Tue Aug 21, 2018 7:32 am

Re: pir motion detection

Mon Sep 10, 2018 1:06 pm

gordon77 wrote:
Mon Sep 10, 2018 11:57 am
How are you interrupting?

If using ctrl&C it's simply showing where it was when you stopped it.
Just ctrl&c

thanks for the replay.

Item ready for closed

gordon77
Posts: 5074
Joined: Sun Aug 05, 2012 3:12 pm

Re: pir motion detection

Mon Sep 10, 2018 1:35 pm

hanspython wrote:
Mon Sep 10, 2018 1:06 pm
...

Item ready for closed
Just modify the title to say ' [SOLVED] pir motion detection'

Return to “Beginners”