Last active
October 13, 2016 15:25
-
-
Save phani-vadrevu/2038a994df148c5fcdc99d0af87158f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import smtplib | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
import time | |
import math | |
from picture_server import take_picture | |
def send_email(): | |
fromaddr = "homemonitor.cs.uga@gmail.com" | |
toaddr = "pvadrevu@gmail.com" | |
msg = MIMEMultipart() | |
msg['From'] = fromaddr | |
msg['To'] = toaddr | |
msg['Subject'] = "Home Break-in!" | |
body = "Home break-in detected!!" | |
msg.attach(MIMEText(body, 'plain')) | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.starttls() | |
server.login(fromaddr, "xxxyyyzzz") | |
server.sendmail("homemonitor.cs.uga@gmail.com", "pvadrevu@gmail.com", msg.as_string()) | |
server.quit() | |
def start_monitoring(): | |
time.sleep(15) | |
image_length = 0 | |
while True: | |
new_image_length = len(take_picture('temp.jpg')) | |
if math.fabs(new_image_length - image_length) > 1000 and image_length != 0: | |
print "Break-in!" | |
send_email() | |
image_length = new_image_length | |
time.sleep(5) | |
if __name__ == "__main__": | |
start_monitoring() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
import base64 | |
# Create a TCP/IP socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Connect the socket to the port where the server is listening | |
server_address = ('localhost', 10001) | |
print 'connecting to %s port %s' % server_address | |
sock.connect(server_address) | |
try: | |
# Send data | |
message = 'TAKE_PICTURE' | |
print >>sys.stderr, 'sending "%s"' % message | |
sock.sendall(message) | |
# Look for the response | |
amount_received = 0 | |
picture_string = "" | |
while True: | |
data = sock.recv(4096) | |
if not data: | |
break | |
amount_received += len(data) | |
picture_string += data | |
finally: | |
print 'received "%s"' % amount_received | |
print 'closing socket' | |
sock.close() | |
fh = open("savedimage.jpg", "wb") | |
fh.write(picture_string.decode('base64')) | |
fh.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import pygame.camera | |
import pygame.image | |
import time | |
import socket | |
import sys | |
import base64 | |
# import os | |
def take_picture(fname=None): | |
pygame.init() | |
pygame.camera.init() | |
cam_list = pygame.camera.list_cameras() | |
# cam = pygame.camera.Camera(cam_list[0], (640,480), 'RGB') | |
cam = pygame.camera.Camera(cam_list[0]) | |
cam.start() | |
time.sleep(0.5) | |
cam.set_controls(False, False, 100) | |
cam.get_image() | |
cam.get_image() | |
image = cam.get_image() | |
if not fname: | |
fname = 'image.jpg' | |
pygame.image.save(image, fname) | |
# print cam.get_controls() | |
cam.stop() | |
with open(fname, "rb") as imageFile: | |
image_string = base64.b64encode(imageFile.read()) | |
# print "File size:", os.fstat(imageFile.fileno()).st_size | |
print "Length of image:", len(image_string) | |
return image_string | |
def start_server(): | |
# Create a TCP/IP socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
# Bind the socket to the port | |
# server_address = ('0.0.0.0', 10001) | |
server_address = ('', 10001) | |
print >>sys.stderr, 'starting up on %s port %s' % server_address | |
sock.bind(server_address) | |
# Listen for incoming connections | |
sock.listen(1) | |
while True: | |
# Wait for a connection | |
print >>sys.stderr, 'waiting for a connection' | |
connection, client_address = sock.accept() | |
try: | |
print >>sys.stderr, 'connection from', client_address | |
# Receive the data in small chunks and retransmit it | |
data = connection.recv(16) | |
print >>sys.stderr, 'received "%s"' % data | |
if data: | |
if data == "TAKE_PICTURE": | |
pic_data = take_picture() | |
print 'sending pic data back to the client' | |
connection.sendall(pic_data) | |
finally: | |
# Clean up the connection | |
print "closing connection!" | |
connection.close() | |
if __name__ == "__main__": | |
# take_picture() | |
start_server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment