Skip to content

Instantly share code, notes, and snippets.

@cudmore
Created October 31, 2017 22:04
Show Gist options
  • Save cudmore/576a808108acf22ff0a259cc1fc30c2a to your computer and use it in GitHub Desktop.
Save cudmore/576a808108acf22ff0a259cc1fc30c2a to your computer and use it in GitHub Desktop.
import os #for interrupt
import time, datetime
import home
from random import randrange #for testing
tempInterval = 3*60 # 1*60 #2*60 == 2 minutes
lasttime = -tempInterval
numEventsToPurge = 200 #ammass this number of events and then purge
h=home.home()
h.logfile_open()
#testing
testinterval = 5 #seconds
lasttest = -testinterval
logNumber = 0
def cleanup():
print '\n'
print '-->> turning ir and white lights off'
h.ir(0)
h.white(0)
print '-->> closing log file'
h.logfile_close()
print '-->> testhome.py is done.'
while True:
try:
now = time.time()
elapsed = now - lasttime
#testing
if (now - lasttest) > testinterval:
lasttest = now
ts = h.timestamp()
#h.logfile_write(ts, 'test1', randrange(0,5), randrange(10,20))
#check hour and adjust lights (will log to file)
hour = datetime.datetime.fromtimestamp(now).hour
turnOnIR = (hour >= 18 or hour < 6) and (h.ir()==0)
turnOnWhite = hour>=6 and hour<18 and (h.white()==0)
#swapped
#turnOnWhite = (hour >= 18 or hour < 6) and (h.ir()==0)
#turnOnIR = hour>=6 and hour<18 and (h.white()==0)
if turnOnWhite:
h.white(1)
h.ir(0)
h.logfile_write(ts, 'whitelight', 1, 0)
h.logfile_write(ts, 'irlight', 0, 1)
elif turnOnIR:
h.ir(1)
h.white(0)
h.logfile_write(ts, 'whitelight', 0, 0)
h.logfile_write(ts, 'irlight', 1, 0)
if (elapsed > tempInterval):
lasttime = now
try:
ts = h.timestamp()
(temp, hum) = h.temperature()
#if temp and hum: #don't log errors
print ts, 'tandh is:', temp, hum
h.logfile_write(ts, 'tandh', temp, hum)
except:
raise
#h.log['a'] is added to with h.logfile_write()
'''
numEvents = len(h.log['a'])
if numEvents > numEventsToPurge:
h.push_sql_log(logNumber)
logNumber += 1
'''
time.sleep(0.005)
except KeyboardInterrupt:
cleanup()
raise
cleanup()
import os
import datetime
import picamera
actuallyRecord = 1
videoDuration = 60*5 #seconds
#the start time stamp of the script (determines directory)
now = datetime.datetime.now()
dateStr = now.strftime('%Y%m%d')
#make destination direcory
dstDir = '/home/pi/video/' + dateStr + '/'
if not os.path.exists(dstDir):
os.makedirs(dstDir)
abortedDuringAcquisition = False
while True:
try:
with picamera.PiCamera() as camera:
#camera.stop_recording()
if(actuallyRecord==1):
#camera.resolution = (2592, 1944)
camera.led = False
camera.resolution = (640, 480)
camera.framerate = 15
startTime = datetime.datetime.now()
startTimeStr = startTime.strftime('%Y%m%d_%H%M%S')
#the file we are about to record/save
thisVideoFile = dstDir + startTimeStr + '.h264'
print('Start: dur=' + str(videoDuration) + ' file=' + thisVideoFile)
try:
#camera.start_recording(thisVideoFile, resize=(1024, 768))
camera.start_recording(thisVideoFile, resize=(640, 480))
camera.wait_recording(videoDuration)
camera.stop_recording()
except KeyboardInterrupt:
print ' -->> aquisition cancelled'
abortedDuringAcquisition = True
raise
stop = datetime.datetime.now()
stopStr = stop.strftime('%Y%m%d_%H%M%S')
print(' Stop: ' + stopStr)
#todo: log what we just did
#todo: check temparature and humidity and lof to file
#todo: check time and turn IR/White lights on and or off
except KeyboardInterrupt:
#print 'Terminated with a user pressing ctrl-c'
#print ' last file was: ' + thisVideoFile
if abortedDuringAcquisition and os.path.isfile(thisVideoFile):
print ' Removing last file:' + thisVideoFile
#there is a chance we get here and the file was done
#todo: user should be responsible for removing last file?
os.remove(thisVideoFile)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment