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)