We had a rash of freezers failing around the department, and I wasn’t going to become a victim. So I built something simple that has proven it’s worth. For a total cost of less than $100 you can sleep easy knowing your freezer door hasn’t been left open.
The electronics are dead easy: You buy an MCP9808 breakout board, and you connect it to a Raspberry Pi. The MCP9808 is a high accuracy thermometer that communicates via the I2C protocol, something that the Raspberry Pi supports natively. You solder wires to the Vdd, GND, SCL, SDA pins of the breakout board, and connect them to pins 1 (3.3 Volts), pin 9, pin 5 and pin 3, respectively, making sure that the wires are long enough to reach inside the fridge from where you can mount the Raspberry Pi. It’s also probably a good idea to coat the breakout board in silicon sealant.
In order to setup the Raspberry Pi, you first need to turn on SSH, so you can remotely access the Pi once it is in place, and turn on the I2C interface. In order to do that, turn your Pi on, log in, and run sudo raspi-config. It should be pretty obvious what to do: go down to advanced options, and SSH and I2C are both there.
Now you need to install python support for the MCP9808. In the terminal, type
sudo apt-get update sudo apt-get install build-essential python-dev python-pip python-smbus git sudo pip install RPi.GPIO cd ~ git clone https://github.com/adafruit/Adafruit_Python_MCP9808.git cd Adafruit_Python_MCP9808 sudo python setup.py install
With that done, confirm that it is all working with:
cd examples sudo python simpletest.py
Where you should see the temperature at the sensor printing out. Press Ctrl-C to quit.
Now I suggest you set up a gmail address specifically for your freezer. It’s not hard. Also, we’re going to save the gmail password in a public folder, so you’re password will be wide open. So I REALLY suggest you set up a gmail account for the freezer. You also need to go to log into the account and turn on access for less secure apps (I think).
Get back to your home directory, and open a new file called read_temp.py
cd ~ sudo nano read_temp.py
And put in the following code, while changing the relevant lines about who you want to send the emails to (the variable emails, on line 20), and the details of your new, freezer specific email address (line 29 to 38).
#!/usr/bin/python import time import Adafruit_MCP9808.MCP9808 as MCP9808 import smtplib from collections import deque from email.mime.text import MIMEText #print '**************************************' #print '-----EXECUTING TEMP SENSOR SCRIPT-----' #print '**************************************' #Connect to i2c sensor with default pins (SCL=P9_19, SDA = P9_20) sensor = MCP9808.MCP9808() #Initialize sensor sensor.begin() emails = 'firstname.lastname@example.org,email@example.com,firstname.lastname@example.org' #comma separated values temp_buffer = deque(maxlen=4) #4 position buffer for the last four reads def send_mail(address, string): #This should really be in a try statement text = string msg = MIMEText(text) me = 'YOUREMAILADDRESS@gmail.com' them = address msg['Subject'] = 'Temperature Warning' msg['From'] = me msg['To'] = them mailserver = smtplib.SMTP("smtp.gmail.com", 587) mailserver.ehlo() mailserver.starttls() mailserver.ehlo() mailserver.login("YOUREMAILADDRESS@gmail.com", "PASSWORD") mailserver.sendmail(me, msg['To'].split(','), msg.as_string()) mailserver.close() #print 'Email Sent' def mean(l): #The worlds silliest function return sum(l)/len(l) def buf_to_str(buf): string = "" for el in buf: string = string + str(el) + ", " return string def check_temp(): temp = sensor.readTempC() temp_buffer.append(temp) if mean(temp_buffer) > -10: string = "The temperature is currently " + str(temp) + ". The last four readings have been " + buf_to_str(temp_buffer) + ". The time is " + time.strftime('%H:%M:%S') send_mail(emails, string) time.sleep(60) while True: check_temp()
Press Ctrl-X to exit, and confirm the save. Confirm that works properly with sudo python read_temp.py.
Now we need to set it up, so this runs on startup. We should set up a bash script to execute this script, so in the terminal, type sudo nano launcher.sh. Then type in the following:
#!/bin/sh sleep 10 sudo python /home/pi/read_temp.py
We need to set that file to be executable, so type sudo chmod 777 launcher.sh. Finally, we need to launch that file on startup. So enter sudo nano /etc/rc.local and at the end of that file (but before the exit 0 line), type:
Making sure that the final line of that file is exit 0. Press Ctrl-X to exit, and then save.
And that should be that. Mount your Pi somewhere around the freezer, get the wires in through the door seals without distrupting them too much, and hang the sensor somewhere senisble, so it doesn’t get in the way too much. Make sure your Pi is connected to the internet, and you should get emails whenever the freezer gets too warm.
If you give this a try, and it’s not working, I’m happy to try to help, but I’m no Unix expert, so I can’t promise I’ll be any use.