-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullscreencamera_mqtt.py
69 lines (49 loc) · 1.86 KB
/
fullscreencamera_mqtt.py
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
import webbrowser
import cv2
import paho.mqtt.client as mqttClient
import time
import subprocess
Connected = False
def show_webcam(payload):
if (payload == 1):
p = subprocess.Popen(["chromium-browser", "--no-sandbox", "--kiosk", "--test-type", "http://CAMERAFEEDURL:PORT",]) #### Edit for your camera url
if (payload == 0):
p = subprocess.Popen(["killall", "chromium-browse",])
p = subprocess.Popen(["chromium-browser", "--no-sandbox", "--kiosk", "--test-type", "--app=http://RETURNURL.COM",]) ### Edit url
def on_message(client, userdata, message):
print "Message received: " + message.payload
process_trigger(message.payload)
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
def process_trigger(payload1):
if (payload1 == 'ON'):
print "Opening webcam"
show_webcam(1)
if (payload1 == 'OFF'):
print "Closing webcam"
show_webcam(0)
broker_address= "broker_ip_here"
port = 1883
user = "username"
password = "password"
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect = on_connect #attach function to callback
client.on_message = on_message #attach function to callback
client.connect(broker_address, port=port) #connect to broker
client.loop_start() #start the loop
while Connected != True: #Wait for connection
time.sleep(0.1)
client.subscribe("pi/door/motion")
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print "exiting"
client.disconnect()
client.loop_stop()