Tutorial 18: Putting it all Together

This tutorial serves as a recap of what has been covered so far and how to bring everything together for our Raspberry Pi home automation security system. I show you how to use the motion detection system to take a picture in Python 3 and timestamp the picture for later review.

This tutorial is a little older (published in 2015!). The main principles are still relevant, so I haven't needed to update this lesson.


DIFFICULTY
MODERATE
LINUX UNDERSTANDING
LITTLE
PYTHON PROGRAMMING
MODERATE
ABOUT
0
MINUTES
  • How to write unique filenames to images taken
  • How to use previous lessons in context of the project (catching the cookie thief)

You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!

This tutorial continues to build upon the cookieMain.py script I wrote in the previous tutorial. The updated script can be found below:


import P3picam
import picamera
from datetime import datetime
from subprocess import call
from time import sleep
from cookieClient2 import transmit
from cookieAnalog2 import getTemp

motionState = False
picPath = "/home/pi/Desktop/cookie/images/"
sleepTime = 3
triggerTemp = 20

def captureImage(currentTime, picPath):
    # Generate the picture's name
    picName = currentTime.strftime("%Y.%m.%d-%H%M%S") + '.jpg'
    with picamera.PiCamera() as camera:
        camera.resolution = (1280, 720)
        camera.capture(picPath + picName)
    print("We have taken a picture.")
    return picName

def getTime():
    # Fetch the current time
    currentTime = datetime.now()
    return currentTime

def timeStamp(currentTime, picPath, picName):
    # Variable for file path
    filepath = picPath + picName
    # Create message to stamp on picture
    message = currentTime.strftime("%Y.%m.%d - %H:%M:%S")
    # Create command to execute
    timestampCommand = "/usr/bin/convert " + filepath + " -pointsize 36 \
    -fill red -annotate +700+650 '" + message + "' " + filepath
    # Execute the command
    call([timestampCommand], shell=True)
    print("We have timestamped our picture.")

def tempMonitor():
    print("About to take a reading.")
    temp = float(getTemp())
    print("Out temp is: " + str(temp))
    if temp > float(triggerTemp):
        message = "LED_ON"
        print("Transmitting data.")
        response = transmit(message)
        print(response)

while True:
    #motionState = P3picam.motion()
    tempMonitor()
    sleep(sleepTime)
    #print(motionState)
    #if motionState:
    #    currentTime = getTime()
    #    picName = captureImage(currentTime, picPath)
    #    timeStamp(currentTime, picPath, picName)
        

You shouldn’t require any further downloads to complete this tutorial.