Created
September 17, 2016 08:21
-
-
Save RcrdBrt/756e1286e812496f0a83d74e68c86e01 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
sys.dont_write_bytecode = True # avoid corruptions | |
import serial | |
import time as t | |
import os.path | |
import ssl | |
import RPi.GPIO as gpio | |
import lcd_driver as lcd | |
import threading as thr | |
import pychromecast as pych | |
import smtplib | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
import urllib.parse | |
import json | |
# SETUP | |
l = lcd.lcd() | |
btn_red = 21 #40 | |
gpio.setmode(gpio.BCM) | |
gpio.setup(btn_red, gpio.IN, pull_up_down=gpio.PUD_UP) | |
alarm_enabled = False | |
username = '[email protected]' | |
passwd = 'password' | |
fromaddr = '[email protected]' | |
toaddrs = ['[email protected]', '[email protected]'] | |
info_sent = False | |
# EXEC | |
# write log utility | |
def log_write(string): | |
with open('alarm-status.txt', 'r+', encoding='utf-8') as f: | |
f.seek(0) | |
f.truncate() | |
f.write(string) | |
f.flush() | |
print('logfile written with', string) | |
# alarm ringing | |
def kick_alarm(): | |
print('starting the kick') | |
server = smtplib.SMTP('smtp.gmail.com:587') | |
print('Chosed the server var') | |
server.starttls() | |
print('started TLS') | |
server.login(username, passwd) | |
# info = 'Subject: %s\n\n%s' % ('Ingresso ' + t.strftime('il %d/%m/%y alle %H:%M:%S'), 'E\' entrato qualcuno in casa.') | |
# server.sendmail(fromaddr, toaddrs, info) | |
print('logged in into server mail') | |
print('waiting time for the alarm to be disabled......') | |
cast = pych.get_chromecast(friendly_name="Chromecast") | |
print('locked the chromecast for the transmission') | |
t.sleep(13) # insert code time + 2 sec of pych = 15 sec | |
if alarm_enabled == True: | |
print('alarm is not dismissed') | |
cast.wait() | |
mc = cast.media_controller | |
mc.play_media('http://path/to/file/on/a/http/server', 'video/mp4') # creepy alarm sound | |
print('Chosen the media to play on the chromecast') | |
mc.play() | |
print('Playing media') | |
l.lcd_clear() | |
l.printlcd('ALARM!!!', 1) | |
msg = 'Subject: %s\n\n%s' % ('VIOLATION ' + t.strftime('%d/%m/%y %H:%M:%S'), 'There\'s been a house violation') | |
server.sendmail(fromaddr, toaddrs, msg) | |
server.quit() | |
t.sleep(180) # time | |
mc.stop() | |
else: | |
server.quit() | |
def check_hcsr04(): | |
global alarm_enabled | |
global info_sent | |
if os.path.exists('/dev/ttyUSB0'): | |
port = serial.Serial("/dev/ttyUSB0", baudrate=9600) | |
print('arduino is on ttyUSB0') | |
else: | |
if not os.path.exists('/dev/ttyUSB1'): | |
print('arduino not found...') | |
return -1 | |
port = serial.Serial("/dev/ttyUSB1", baudrate=9600) | |
print('arduino is on ttyUSB1') | |
while True: | |
print('listening on the serial...') | |
decoded = port.readline().decode('utf-8') | |
if info_sent == True: | |
print('alarm triggered, waiting 15 seconds to kick...') | |
kick = thr.Thread(target=kick_alarm) | |
kick.start() | |
l.lcd_clear() | |
l.printlcd('Accesso rilevato', 1) | |
l.printlcd('Comporre sblocco', 2) | |
while True: | |
print('wait btn_red comb') | |
gpio.wait_for_edge(btn_red, gpio.FALLING) | |
t.sleep(2) # tempo da tener premuto per disabilitare | |
if gpio.input(btn_red) == gpio.LOW: | |
alarm_enabled = False | |
l.lcd_clear() | |
l.printlcd('Allarme', 1) | |
l.printlcd('disattivato',2) | |
log_write('0') | |
t.sleep(4) | |
break | |
break | |
else: | |
print('I sucessfully got the info JSON, checker is initialized') | |
info_sent = True | |
continue | |
print('alarm disabled!') | |
print('waiting for kick_alarm thread') | |
port.close() | |
kick.join() | |
info_sent = False | |
return 0 | |
# alarm starting logic | |
def alarm_start(delay): | |
print('Alarm started with delay setting of', delay) | |
global alarm_enabled | |
alarm_enabled = True | |
log_write('1') | |
l.printlcd('Avvio allarme', 1) | |
l.printlcd('tra 5 minuti', 2) | |
t.sleep(6) | |
l.lcd_clear() | |
l.backlight(0) | |
t.sleep(delay) # 300 in btn_red mode | |
return_code = check_hcsr04() | |
if return_code == -1: | |
l.printlcd('Errore di', 1) | |
l.printlcd('collegamento', 2) | |
t.sleep(6) | |
l.lcd_clear() | |
l.printlcd('Occorre', 1) | |
l.printlcd('ricollegare', 2) | |
t.sleep(6) | |
l.lcd_clear() | |
alarm_enabled = False | |
log_write('0') | |
elif return_code == 0: | |
l.lcd_clear() | |
l.printlcd('Buona', 1) | |
l.printlcd('giornata!', 2) | |
t.sleep(4) | |
class GetHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
parsed_path = urllib.parse.urlparse(self.path) | |
global alarm_enabled | |
self.send_response(200) | |
self.end_headers() | |
print('alarm_enabled is', alarm_enabled) | |
if alarm_enabled == True: | |
self.wfile.write(b'1') | |
else: | |
self.wfile.write(b'0') | |
return | |
def https_runner(): | |
httpd = HTTPServer(('0.0.0.0', 3001), GetHandler) | |
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='./fullchain.pem', keyfile='./privkey.pem', server_side=True) | |
httpd.serve_forever() | |
if __name__ == '__main__': | |
try: | |
print('starting the program') | |
thr.Thread(target=https_runner).start() | |
while True: | |
l.lcd_clear() | |
l.backlight(0) | |
print('start alarm') | |
with open('alarm-status.txt', 'r+', encoding='utf-8') as f: | |
if f.read() == '1': | |
print('contenuto file', f.read()) | |
print('Restoring previous state') | |
alarm_start(0) | |
continue | |
print('Waiting for btn_red interrupt') | |
gpio.wait_for_edge(btn_red, gpio.FALLING) | |
print('Interrupt on btn_red catched!') | |
alarm_start(240) # 300 in prod mode | |
except KeyboardInterrupt: | |
gpio.cleanup() | |
print('Bye!\n') | |
log_write('-1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment