Summary: Building a house monitor with python, openCV, and cloud storage.

I wanted an easy way to check on my house when I was away, and since it can get cold in the winter I also wanted to keep track of the house temperature.

I found that the simplest way to so this was to:

  1. capture images of a room in my house,
  2. read the temperature of the house,
  3. add the time and temperature to the image as text, and
  4. save the image to my cloud synced folder that I can view when I am away.

I have an Arduino taking the house temperature every 20 minutes (see the Arduino Programming post) and then saving it to my Thinspeak account. The house monitor script grabs the last temperature reading saved to Thingspeak.

The Code

I used python to write the script and openCV to manage the webcam and writing text to the image.

  #!/usr/bin/env python
      import cv2
      import time
      import datetime
      import ConfigParser
      import sys
      import requests
      import json


      def readThingspeak():
          result = requests.get('https://api.thingspeak.com/channels/8220/feeds.json?results=1')

          print "Status: %i"  %result.status_code
          temp=result.json()
          #print temp
          newResult =temp ['feeds'][0]['field1']
          print newResult

          return (newResult)


      config=ConfigParser.RawConfigParser()
      config.read('config.cfg')
      saveLocation=config.get('FileLocation','saveDir')
      fileName=config.get('FileLocation','fileName')
      fileCounter=int(config.get('FileLocation','fileCount'))

      imageText=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      dataFileText=datetime.datetime.now().strftime('%Y-%m-%d/t%H:%M:%S')

      camera_index = 0; x=0; y=50
      capture = cv2.VideoCapture(camera_index)
      ret, photo=capture.read()

      temperature=readThingspeak()
      imageText=imageText + ", Temperature %s" %temperature

      if ret:
        print "Camera successful"
        print imageText

        print saveLocation

        cv2.putText(photo,imageText, (x,15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0),1)

        capture.release()

        retVal=cv2.imwrite(saveLocation + fileName + str(fileCounter)+ ".jpg",photo)
        if retVal:
          print "Saved"
          print "Filecounter: %i" %fileCounter
          fileCounter+=1
          config.set('FileLocation','fileCount',str(fileCounter))

          with open('config.cfg', 'w') as configfile:    # save
            config.write(configfile)
        else:
          print "Save Failed"

        cv2.imshow("Camera_Test",photo)
        cv2.waitKey(5000)
        cv2.destroyAllWindows()
      else :
          print "Camera Error"

To keep track of the number of images taken and to set my save directory I created a config.cfg for the script.

[FileLocation]
savedir = [your share directory]
filename = Camera
filecount = 0