Camera surveillance

A project that combines camera surveillance and a monitor for the heating system to be controlled by the same RPi.

#This program drives the RPi for the heating system and camera
import os
import glob
import time
import commands
import threading
from time import sleep
import gdata.spreadsheet
import gdata.spreadsheet.service
from ftplib import FTP
import ftplib
from time import sleep
import picamera
import RPi.GPIO as GPIO
import subprocess
import datetime as dt
import smtplib #for sending mails
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)
 
n=1
upld=1 #determines if image will be uploaded as well
filename = '/home/pi/Desktop/camera/' + str(n) + '.jpg'
 
# load the kernel modules needed to handle the sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
 
temperature = 0.00
 
#FTP information
FTPhost = 'zzzzzzzzzzz'
FTPdomain = 'zzzzzzzzzzzzz'
FTPpass = 'zzzzzzzzzzzzzzzzzzzzzz'
 
#ALL GOOGLE SERVICES ARE NOW DISABLED DUE TO THEIR OAUTH PARANOIA
#GOOGLE:
#account details
gmail = 'zzzzzz'
gpass = 'qqqqqqqqqqqqqq'
ghost = 'vvvvvvvvvvvvvvvvvv'
 
#Spreadsheets
spr_client = gdata.spreadsheet.service.SpreadsheetsService()
spr_client.email = gmail
spr_client.password = gpass
spr_client.source = 'Example Spreadsheet Writing Application'
spr_client.ProgrammaticLogin()
 
# ID's of the different spreadsheets used for storing data
spr_key_event = '1QNmkzUWZKyZ6BYDu0B7UBJCcdiC9bLrg_vxXxBZIhtk' #Event log
spr_key_rainy_day = '1sidW4BTGui7frjRxehZlySCPe4oo_lSshNmGQDP0v3k' # Rain last 24H in 10 min intervals
spr_key_rainy_week = '1EFfRVuUIYqD9QObG0M_AgSmYjHYMd3kv2yvmHUrNSZM' # Rain last 2 weeks in 1H intervals
spr_key_rainy_year = '16R1nxxtNsrQNKULwiFXLzkvZUYaRc_N2XbgqcoVFnKE' #Rain last year with once daily updates
spr_key_temp_out = '1EflJUkzfxBI5vPTSAMh5Ds8Y01O63RGd5hCg2IFM50w' #Outside temperature the last 24h (Temp out)
spr_key_temp_now = '1iyAU5eHBeYHG3XHisUlPSWFEFM4dnc8_RkOR7AU108g' #Outside temperature right now (Temp now)
spr_key_temp_all = '1srQPRg9ZUfFLY_5z8m6DLgbz-Cz833S2HRz2alsWrRo' #All temperature sensors (Temp all) for internal use
spr_key_temp_extremes = '1Oe3DlM4uobJMgSfJw8Fucyd4qgDt7WgXn0BVFZRuFRw' #Outside temperature extremes
 
worksheet_id = 'od6' # worksheet #1 by default always has a value of 'od6' if necessary
 
TEMP_ALL_UPDATE = True
ONLINE_SENSORS = True #Used for checking the online sensors once every 10 mins
 
def LogFile (func): #Generic function to write to logfile if spreadsheet updates fail
         
        f = open('/home/pi/Desktop/heat.txt', 'a')
        f.write(" Event registered for " + func + " at " + str(time.strftime('%d/%m/%Y %H:%M:%S')))
        f.close()
     
def SprUpdate ():
        global spr_key_sensors
        global dict
        entry = spr_client.InsertRow(dict, spr_key_sensors)
        print dict
        sleep (0.1) #wait for the update before deleting the first line
        delfeed = spr_client.GetListFeed(spr_key_sensors)
        spr_client.DeleteRow(delfeed.entry[0])
 
def SprUpdateCell (value, cellno, spr_key): #Function to update single cells (temp, cell number and spreadsheet)
        try:            
                query = gdata.spreadsheet.service.CellQuery()
                query.return_empty = "true"
                cells = spr_client.GetCellsFeed(spr_key, query=query)
                batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()  
                cells.entry[int(str(cellno))].cell.inputValue = str(value)
                batchRequest.AddUpdate(cells.entry[int(str(cellno))])  
                updated = spr_client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)
                 
        except:
                LogFile (" SprUpdateCell had a problem. Temperature: " + str(value) + " Cell: " + str(cellno) + " Key: " + str(spr_key) + " ")
 
def termo (tID): #Read the temperature of a thermometer
        global temperature
 
        file = open("/sys/bus/w1/devices/" + tID + "/w1_slave") # Path of the sensor
        # Read all of the text in the file. 
        text = file.read() 
        # Close the file now that the text has been read. 
        file.close() 
        # Split the text with new lines (\n) and select the second line. 
        secondline = text.split("\n")[1] 
        # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
        temperaturedata = secondline.split(" ")[9] 
        # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
        temperature = float(temperaturedata[2:]) 
        # Put the decimal point in the right place and display it. 
        temperature = temperature / 1000
 
def termoRead(spr_key):
 
        global dict
        global TempIDs
         
        #Open the CSV and write the header instead of old content
        r = open('/home/pi/Desktop/csv/heat.csv', 'w')
        r.write (time.strftime('Date %d-%m-%Y Time %H:%M:%S') + "," + "Degrees (C)" + '\r\n')
        r.close()
 
        #Compare the two arrays in order to know which sensors to use
        dict = {} #Prepare data for spreadsheet
        for i in range(len(OnlineIDs)):
                a=OnlineIDs[i]
                for t in range(len(TempIDs)): 
                        b=TempIDs[t][1]
                        if a==b:
                                termo (str(TempIDs[t][1]))
                                #print str(TempIDs[t][0]) + " is " + str(temperature) + " deg C"
                                dict[str(TempIDs[t][2])] = str(temperature) #Build the list for each run if it should be used at a later stage
                                r = open('/home/pi/Desktop/csv/heat.csv', 'a') #Append the readings to the CSV
                                r.write (str(TempIDs[t][3]) + "," + str(temperature) + '\r\n')
                                r.close()
 
                                #print str(TempIDs[t][1])
                                #print str(TempIDs[t][2])
                                #print str(TempIDs[t][3])
                                #print str(TempIDs[t][4])
                                #SprUpdateCell (temperature, str(TempIDs[t][4]), spr_key)
        Upload ('heat')
 
def Upload (filename): #Uploads the specified CSV file to the domain
         
        global FTPhost, FTPdomain, FTPpass
        fileup = '/home/pi/Desktop/csv/' + str(filename) + '.csv'
 
        try:
                session = ftplib.FTP(str(FTPhost), str(FTPdomain), str(FTPpass))
                file = open(str(fileup),'rb')  # file to send
                session.cwd('/wp-content/uploads/') #change directory
                session.storbinary('STOR ' + str(filename) + ".csv", file) # send the file
                file.close()                            # close file and FTP
                session.quit()
                 
        except:
                print "FTP upload failed "
 
def shootusb ():
#       print " shooting image with fswebcam "
    os.system('fswebcam -r 1280x720 /home/pi/Desktop/camera/%H%M%S.jpg')
#        fswebcam -r 1280x720 /home/pi/Desktop/camera/usbcam.jpg
#       print "USB shot with fswebcam went fine"
 
def shootrpi ():
# Shooting image with RPI cam
 
    global n
    global upld
    global filename
    filename = '/home/pi/Desktop/camera/' + str(n) + '.jpg'
    with picamera.PiCamera() as camera:
        try:
            camera.led = False
            camera.resolution = (2592, 1944)
            camera.framerate = 1
            camera.exposure_mode = 'auto'
    #               camera.iso = 0
    #               camera.shutter_speed = 5000000
            camera.start_preview()
            camera.annotate_background = True
            camera.annotate_text = 'Ferskenvej 34, Copenhagen ' + dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            camera.exif_tags['IFD0.Artist'] = 'Webcam at Ferskenvej 34, Copenhagen'
            camera.exif_tags['IFD0.UserComment'] = 'Made with Rapberry Pi Cam'
            camera.exif_tags['IFD0.Copyright'] = 'Copyright (c) 2017 Jesper Bonde Petersen'
            sleep(4)
            camera.capture(filename, resize=(1280, 960))
            sleep(2)
            camera.close
#           print "RPI shot " + str(n) + " was made"
            n=n+1
            upld=upld+1
            if upld==60:
                upld=1 #'Upload one copy to http://jesperbonde.dk/wp-content/uploads/webcam.jpg
            if n==3500:
                n=1
         
        except:
            print "Capture failed for pix no " + str(n)
 
def shootftp ():
    try:
        session = ftplib.FTP('zzzzzzzzzzzzzzzzzzz','vvvvvvvvvvvvvvvv','qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq')
        file = open(str(filename),'rb')  # file to send
        session.cwd('/wp-content/uploads/') #change directory
        session.storbinary('STOR webcam.jpg', file)     # send the file
        file.close()                                    # close file and FTP
        session.quit()
#       print "------->  Shot " + str(n) + " was uploaded"
         
    except:
        print "FTP upload failed for pix no. " + str(n)
 
#Array with information about all tested sensors incl. color code, sensor ID, spreadsheet name, function and spreadsheet cell
TempIDs = ["BLACK", "28-000005b16567", "out", "Outside temperature", "16", "X"],\
          ["BROWN", "28-000005b226ae", "water", "Water tank temp", "17", "A"], \
          ["RED", "28-000005b217e2", "radf", "Radiators forward temp", "18", "B"],\
          ["ORANGE", "28-000005b23020", "radr", "Radiators return temp", "19", "C"],\
          ["YELLOW", "28-000005b21d0d", "heatf", "Received power heating temp", "20", "D"], \
          ["GREEN", "28-000005b1b646", "heatr", "Returned power heating temp", "21", "E" ],\
          ["BLUE", "28-000005b1d70e", "floorf", "Floor heating forward temp", "22", "F"], \
          ["PURPLE", "28-000005b217eb", "floorr", "Floor heating return temp", "23", "G"],\
          ["BLACK - LOST BROWN", "28-000005b1a9e9", "xx", "xx", "24", "X"],\
          ["BROWN", "28-000005bxxxxx", "XX", "xx", "25", "Y"],\
          ["RED", "28-000005b22544", "dining", "Dining Room", "26", "Z"],\
          ["ORANGE", "28-000005b1725b", "living", "Living Room", "27", "Q"]
 
#Read the sensors that are connected when booting
OnlineIDs = commands.getoutput(' ls /sys/bus/w1/devices/ | grep -e "28-"').splitlines()  # Ids for sensors online
print "Total amount of installed sensors: " + str(len(TempIDs))
print "Amount of online temp sensors: " + str(len(OnlineIDs))
print "ID of online sensors: " + str(OnlineIDs)
 
#termoRead(spr_key_temp_all)
while True:
 
    shootrpi()
    sleep(3)
    if upld==2:
        shootftp()
        sleep(5)
 
    minw = str(time.strftime ("%M"))# Watching minutes
 
    if minw[-1:] == "0": #Update the sensor csv spreadsheet every 10 min
        if TEMP_ALL_UPDATE == True:
            a=threading.Thread(target=termoRead(spr_key_temp_all))
            a.start()
            TEMP_ALL_UPDATE = False
 
    if minw[-1:] == "5": #Check for active online sensors every 10 min
        if ONLINE_SENSORS == True:
            OnlineIDs = commands.getoutput(' ls /sys/bus/w1/devices/ | grep -e "28-"').splitlines()  # Ids for sensors online
            sleep (20)
            ONLINE_SENSORS = False
 
    if minw[-1:] == "9": 
        #Prepare for next Update 
        TEMP_ALL_UPDATE = True
        ONLINE_SENSORS = True