My Coop Just sent me an email

jthornton

Free Ranging
8 Years
Friend
Joined
Aug 30, 2017
Messages
5,184
Reaction score
10,637
Points
702
Location
Poplar Bluff, MO
My Coop
My Coop
I just wrote a program in Python so my Raspberry Pi 3 sends me an email when the door is in motion and when it is closed or open. Cool for when I'm away I monitor the status of the door and if anything is not right I can get my buddy to check it out. I'm really having lots of fun with the RPi3.

If anyone needs the program I can upload it in the morning.

JT
 
If anyone is a tinkerer like me here is the python 2 code to send you an email and example code on how to do a task only once.
Code:
#!/usr/bin/env python

import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
import time

fromAddress = "[email protected]" # put your sender address here
password = "yoursenderpassword" # put your sender password here
toAddress = "[email protected]" # put the receiver address here

content = time.strftime("%b %d %Y %I:%M %p")
content += "\nChicken Report Test"
content += "\nDoor Status = Open"
content += "\nLight Status = On"
msg = MIMEText(content)

msg['Subject'] = "Chicken Coop"
msg['From'] = fromAddress
msg['To'] = toAddress
msg["Date"] = formatdate(localtime=True)

try:
    server = smtplib.SMTP('smtp.yourserver.com:587') # put your server here
    #server.starttls()
    server.login(fromAddress, password)
    server.sendmail(fromAddress, toAddress, msg.as_string())
    server.quit()
    print "Email sent!"
except Exception as e:
    print(e)

This is the format I used to test for a change in the door status. This example tests for a change in minutes.
Code:
#!/usr/bin/env python

import time

minute = int(time.strftime("%M"))
lastMinute = minute
while True:
    minute = int(time.strftime("%M"))
    if(lastMinute != minute): # test to see if there has been a change
        print minute
        lastMinute = minute # update so it only does this once for each change
    time.sleep(1)

JT
 
Oh and the email looks like this from the test code above.

Dec 21 2017 04:05 AM
Chicken Report Test
Door Status = Open
Light Status = On

Hmm I need to add a signature line from the coop.

JT
 
I find Python the easiest language of all the programming languages I've done. Even easier if you have a Linux PC. You can connect a monitor, keyboard and mouse to the Raspberry Pi 3 and it's just like a desktop PC for $40!

JT
 
Very cool. Do you administer your own mail server? Or does your ISP let you relay email via them?
I don't have my own mail server.

It's no different than sending an email from Thunderbird, you need your email address, password and the proper port they use to send email with.

JT
 

New posts New threads Active threads

Back
Top Bottom