-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.py
More file actions
89 lines (79 loc) · 2.54 KB
/
Copy pathcontrol.py
File metadata and controls
89 lines (79 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import RPi.GPIO as GPIO
import time
import face3
import paho.mqtt.client as mqtt
import json
from picamera import PiCamera
import requests
import json
import base64
#import door
#import buzzer
#import light
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/home/home1") #subscribe to corresponding channel (same with the id(unique) of Pi)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
message = json.loads(msg.payload)
if (message['Type'] == "ViewPhoto"):
#TO-DO: Require camera to take picture.
camera = PiCamera()
camera.start_preview()
camera.capture('/home/pi/Desktop/viewphoto.jpg')
camera.stop_preview()
camera.close()
url = 'http://54.183.198.179/uploadphoto.php'
image = open('/home/pi/Desktop/viewphoto.jpg','rb')
image_read = image.read()
image_64 = base64.encodestring(image_read)
payload = {"Homename":"home1","Image":image_64}
r=requests.post(url, data=json.dumps(payload))
if message['Type'] == "OpenDoor":
door.OpenDoor()
pass;
if message['Type'] == "SendAlert":
buzzer.alarm()
pass;
if message['Type'] == "TurnOnLight":
light.TurnOn()
pass;
if message['Type'] == "TurnOffLight":
light.TurnOff()
pass;
if message['Type'] == "HomeStatus":
url = 'http://54.183.198.179/refreshstatus.php'
pass
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("54.183.198.179", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_start()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.IN)
flag=0
try:
while True:
if GPIO.input(12)==0:
print "nobody"
flag=0
time.sleep(0.5)
if GPIO.input(12)==1:
print "somebody here"
if flag==0:
print "camera on"
face3.StartFaceDetection()
flag = 1
time.sleep(6)
except KeyboardInterrupt:
GPIO.cleanup()
client.loop_stop()
print "all cleanup"