solodrama
Posts: 4
Joined: Tue Apr 28, 2020 2:58 pm

PIR Sensor | Write Data If No Motion Detected

Thu Apr 30, 2020 2:39 pm

Hello everyone,

I'm trying to write a code to record data motion and no motion moments. The code works well if I just want to record the moments there is motion.

I would like to record the moments when there is no motion every 1 minute. For instance, the code will do that, recording data as "1" to CSV file if there is motion and if there is no motion for 1 min, it will record it as "0" the code below is doing that but I would like to do that if no motion session continue, I would like to record that in every minute as "0" and if there is again motion it should record it again as "1".

Thank you for your help.

Code: Select all


from gpiozero import MotionSensor
from collections import Counter
from threading import Timer
import threading
import datetime
import time
import csv



pir = MotionSensor(6)

def time_now():
 now = datetime.datetime.now().strftime("%H:%M")
 now = str(now)
 return(now)

def date_now():
 today = datetime.datetime.now().strftime("%d/%m/%Y")
 today = str(today)
 return(today)

def nomotion():
    print("no motion")


counter = 0
y = datetime.datetime.now()
while True:
         pir.wait_for_motion()
         print('Motion {0},{1}'.format(counter,y))
         counter += 1
         data = '1'
         with open('data.csv', mode= 'a') as output:
                 csvwriter = csv.writer(output, quoting = csv.QUOTE_MINIMAL)
                 csvwriter.writerow([date_now(), time_now(), data])
                 output.close()
         pir.wait_for_no_motion()

         t = Timer(5.0, nomotion)
         t.start()
         data2 = '0'
         with open('data.csv', mode= 'a') as output2:
          csvwriter = csv.writer(output2, quoting = csv.QUOTE_MINIMAL)
          csvwriter.writerow([date_now(), time_now(), data2])
          output2.close()
          time.sleep(2)


pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR Sensor | Write Data If No Motion Detected

Thu Apr 30, 2020 7:00 pm

Your code will never do what your want because you are using pir.wait_for_motion() this stops you code until that condition is met.

You need to change the way your code works so that it cycles on a while loop so that after 60 seconds if no high ouptut from the PIR has occurred you write the data you want to the CSV file , but if a high output from the PIR does occur you reset the 60 second timer and write your required data to the CSV file.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PiGraham
Posts: 3929
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: PIR Sensor | Write Data If No Motion Detected

Thu Apr 30, 2020 7:28 pm

Set a variable true in an edge detection callback function when motion is detected.
Set a timer to log to file every minute. Test the variable to decide whether to write 1 (motion occurred in the last minute) or 0 (o motion was detected in the last minute).
Then set the variable false.
Alternatively increment a count variable every time motion is detected and log the activity count for each minute.

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: PIR Sensor | Write Data If No Motion Detected

Fri May 01, 2020 10:58 am

Here is a basic example

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(6, GPIO.IN)

end = (time.time()) + 60


def writetofile(data):
    daten = (time.strftime("%d/%m/%Y"))
    timen = (time.strftime("%H:%M"))
    with open("/home/pi/data.csv", "a") as output:
        output.write(daten + "," + timen + "," + data + "\n")
        

def motion(channel):
    data = "1"
    writetofile(data)
    print("Motion")
    end = (time.time()) + 60
    return end

   
GPIO.add_event_detect(6, GPIO.RISING, callback=motion, bouncetime=500)   

while True:
    if (time.time()) > end:
        data = "0"
        writetofile(data)
        print("No Motion")
        end = (time.time()) + 60
        
    time.sleep(1)
    
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

solodrama
Posts: 4
Joined: Tue Apr 28, 2020 2:58 pm

Re: PIR Sensor | Write Data If No Motion Detected

Tue May 05, 2020 8:22 am

thank you guys. I'll try your suggestions, I hope I can make it work because I'm really beginner for python things.

Return to “Beginners”