Last active
August 29, 2015 14:07
-
-
Save wehappyfew/2e54c60987661db82612 to your computer and use it in GitHub Desktop.
A script that reads input from a serial port (eg Arduino on COM3) using the pyserial library and sends an email (using the GMAIL SMTP settings) when a condition is met.
This file contains 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
# wehappyfew | |
# Distributed over IDC(I Don't Care) license | |
__author__ = 'wehappyfew' | |
import time | |
import serial | |
import smtplib | |
# | |
RECIPIENT = "the_recipients email" | |
GMAIL_SENDER = "the_senders email" | |
GMAIL_PASS = "the password" | |
GMAIL_SMTP_SERVER = "smtp.gmail.com" | |
PORT = 587 | |
SUBJECT = "RED ALERT !!!" | |
TEXT = "IOU iou IoU IOU IOU iou \n " \ | |
"It worked!!! :P" | |
# the pyserial library reds thr input from the serial port ( COM ) at spesific baud rate | |
s = serial.Serial('COM3',9600) | |
#the function that is going to be called to send the email when movement is sensed | |
def send_email(): | |
print("Sending the email..") | |
smtpserver = smtplib.SMTP( GMAIL_SMTP_SERVER,PORT) | |
smtpserver.ehlo() | |
smtpserver.starttls() | |
smtpserver.ehlo() | |
smtpserver.login(GMAIL_SENDER, GMAIL_PASS) | |
header = 'From: ' + GMAIL_SENDER + '\n' + 'To:' + RECIPIENT | |
header = header + '\n' +'Subject: ' + SUBJECT + '\n' | |
print header | |
msg = header + '\n' + TEXT +'\n\n' | |
smtpserver.sendmail(GMAIL_SENDER,RECIPIENT,msg) | |
smtpserver.close() | |
while True: | |
message = s.readline() | |
print(message) | |
if message[0] == 'M': | |
send_email() | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment