Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
water_alert system
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import requests
import datetime
import os.path
import datetime
import smtplib
from email import Encoders
from email.Utils import formatdate
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import RPi.GPIO as GPIO
from time import sleep
def readadc(adcnum,clockpin,mosipin,misopin,cspin):
if adcnum>7 or adcnum<0:
return -1
GPIO.output(cspin,GPIO.HIGH)
GPIO.output(clockpin,GPIO.LOW)
GPIO.output(cspin,GPIO.LOW)
commandout=adcnum
commandout|=0x18
commandout<<=3
for i in range(5):
if commandout & 0x80:
GPIO.output(mosipin,GPIO.HIGH)
else:
GPIO.output(mosipin,GPIO.LOW)
commandout<<=1
GPIO.output(clockpin,GPIO.HIGH)
GPIO.output(clockpin,GPIO.LOW)
adcout=0
for i in range(13):
GPIO.output(clockpin,GPIO.HIGH)
GPIO.output(clockpin,GPIO.LOW)
adcout<<=1
if i>0 and GPIO.input(misopin)==GPIO.HIGH:
adcout|=0x1
GPIO.output(cspin,GPIO.HIGH)
return adcout
def create_message(from_addr, to_addr, subject, body, mime=None, attach_file=None):
msg = MIMEMultipart()
msg["From"] = from_addr
msg["To"] = to_addr
msg["Date"] = formatdate()
msg["Subject"] = subject
body = MIMEText(body)
msg.attach(body)
# attached file
if mime != None and attach_file != None:
attachment = MIMEBase(mime['type'],mime['subtype'])
file = open(attach_file['path'])
attachment.set_payload(file.read())
file.close()
Encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
return msg
def send(from_addr, to_addrs, msg):
smtpobj = smtplib.SMTP(SMTP, PORT)
smtpobj.ehlo()
smtpobj.starttls()
smtpobj.ehlo()
smtpobj.login(ADDRESS, PASSWARD)
smtpobj.sendmail(from_addr, to_addrs, msg.as_string())
smtpobj.close()
def Line():
url = "https://notify-api.line.me/api/notify"
token = "3p6pbkDNf8iyJdv6erh2w352SqzVkhZ4n8FkAS1vS1 something like that. this is random string haha"
headers = {"Authorization" : "Bearer "+ token}
message = 'Water FLOODING!!!!!!!!'
payload = {"message" : message,'stickerPackageId':2,'stickerId':24}
r = requests.post(url ,headers = headers ,params=payload)
GPIO.setmode(GPIO.BCM)
#define pin's name
SPICLK=11
SPIMOSI=10
SPIMISO=9
SPICS=8
#define IO for SPI interaction
GPIO.setup(SPICLK,GPIO.OUT)
GPIO.setup(SPIMOSI,GPIO.OUT)
GPIO.setup(SPIMISO,GPIO.IN)
GPIO.setup(SPICS,GPIO.OUT)
LED=25
GPIO.setup(LED,GPIO.OUT)
#--------------------------------------------------------------------------------------------------------------#
#gmail setting
ADDRESS = "****@gmail.com"
PASSWARD = "Gmail's password"
SMTP = "smtp.gmail.com"
PORT = 587
to_addr1 = "address you want to send"
to_addr2="address you want to send"
subject = "Water Flooding!!!"
body = "Please come as early as possible!"
#attached file arrangement
mime={'type':'text', 'subtype':'comma-separated-values'}
attach_file={'name':'test.txt', 'path':'./text.txt'}
#if attachment exists
#msg = create_message(ADDRESS, to_addr, subject, body, mime, attach_file)
#normal message
msg1 = create_message(ADDRESS, to_addr1, subject, body)
msg2 = create_message(ADDRESS, to_addr2, subject, body)
#-------------------------------------------------------------------------------------------------------------#
try:
while True:
inputVal0=readadc(0,SPICLK,SPIMOSI,SPIMISO,SPICS)
#printing output Voltage
print(inputVal0)
if inputVal0<700:
#send message
flag=0
while flag<50:
try:
send(ADDRESS, [to_addr1], msg1)
send(ADDRESS, [to_addr2], msg2)
Line()
sleep(1)
flag+=1
except:
os.system('sudo ifdown wlan0')
sleep(30)
os.system('sudo ifup wlan0')
sleep(30)
break
else:
GPIO.output(LED,GPIO.LOW)
sleep(0.2)
except KeyboardInterrupt:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment