import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
import os
to = 'martinjudiny@gmail.com'
gmail_user = 'martin.judiny@gmail.com'
gmail_password = 'KoMejNo4'
smtpserver =smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_password)
today= datetime.date.today()
arg ='ip route list'
proc =subprocess.Popen(arg, shell=True, stdout=subprocess.PIPE)
data=proc.communicate()
split_data=data[0].split()
local_ip=split_data[split_data.index('src') + 1]
public_ip = os.system('wget http://ipecho.net/plain -O - -q > test.txt; echo')
public_ip = open('test.txt', 'r').read()
msg_content = "Alarm detected. Public ip is %s:8081, local ip is %s" % (public_ip, local_ip)
msg = MIMEText(msg_content)
msg['Subject'] = 'Raspberry Pi Alarm on %s' % today
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()

GPIO.setmode(GPIO.BCM)
GPIO.setup(7,8,  GPIO.IN)
count = 0
while True:
   if(GPIO.input(7) == False & count == 0):
    count = count + 1
    send_alarm()
  time.sleep(10)
  count = 0
