---

Friday, February 21, 2014

Raspberry Pi Fridge Minder: receive an email when the door is opened

I decided to see what other kinds of project can be made from the MonkMakes Raspberry Pi Electronics Starter Kit, that I designed.


The kit comes with project cards and a link to download the accompanying Python scripts. And projects already include a light meter and email notifier, so I decided to combine these two projects into one and make something that will send an email whenever the fridge door is opened. 


This is particularly timely as the MonkMakes team are trying to loose some weight at the moment. The paper template on the Raspberry Pi GPIO connector is called a Raspberry Leaf.

It turned out to be pretty straightforward. The breadboard layout is shown below:


BTW. A big thank you to Fritzing for a great design tool.

The photoresistor needs to be inside the fridge, so we can use two of the male to female jumper leads. This will of course also provide us with proof to the eternal question of whether the light really goes off when you close the fridge door.

You can see the completed breadboard and Pi below.


To install the program on the Raspberry Pi, you probably want to connect using SSH (see this Adafruit tutorial). Run the command "nano fridge.py" and paste the following code into the editor.

# fridge.py

import RPi.GPIO as GPIO
import time, math
import smtplib, time

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587

GPIO.setmode(GPIO.BCM)

a_pin = 18
b_pin = 23
led_pin = 24

GPIO.setup(led_pin, GPIO.OUT)
GPIO.output(led_pin, False)

def discharge():
    GPIO.setup(a_pin, GPIO.IN)
    GPIO.setup(b_pin, GPIO.OUT)
    GPIO.output(b_pin, False)
    time.sleep(0.01)

def charge_time():
    GPIO.setup(b_pin, GPIO.IN)
    GPIO.setup(a_pin, GPIO.OUT)
    GPIO.output(a_pin, True)
    t1 = time.time()
    t2 = t1
    while GPIO.input(b_pin) == 0:
        t2 = time.time()
        if (t2 - t1) > 1.0:
            return 0
    return (t2 - t1) * 1000000

def analog_read():
    discharge()
    return charge_time()


def send_email(username, password, recipient, subject, text):
    print(username, password, recipient, subject, text)
    smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(username, password)
    header = 'To:' + recipient + '\n' + 'From: ' + username
    header = header + '\n' + 'Subject:' + subject + '\n'
    msg = header + '\n' + text + ' \n\n'
    smtpserver.sendmail(username, recipient, msg)
    smtpserver.close()

username = raw_input("Sending gmail address? ")
password = raw_input("Sending gmail password? ")
recipient = raw_input("Send email to? ")
subject = "FRIDGE VIOLATION"
message = "Someone just opened the fridge door!"

while True:
    reading = analog_read()
    if reading > 0: #dark so timeout
        GPIO.output(led_pin, True)
        send_email(username, password, recipient, subject, message)
        time.sleep(120)
        GPIO.output(led_pin, False)
    time.sleep(0.1)

You can test out the program without installing it on your fridge. Start the program using the command:

sudo python fridge.py

There may be a few warnings about GPIO channels being in use, which you can ignore. Cover the light sensor with a cloth or place it somewhere where it will be completely dark.

You will be prompted for your gmail email address, password and who you want to be sent an email when the fridge door is opened.


$ sudo python fridge.py 
Sending gmail address? myname@gmail.com
Sending gmail password? jkshdgfkayig
Send email to? youremail@somemail.com

When its running, you can uncover the sensor and you should see some trace that the email has been sent. Similarly, any errors will appear. 

The light sensor having fairly thin wires attached can just be shut in the door.




You may be interested in the books I have written about the Raspberry Pi.


No comments: