{"id":375,"date":"2017-02-04T06:39:38","date_gmt":"2017-02-04T06:39:38","guid":{"rendered":"http:\/\/www.billconnelly.net\/?p=375"},"modified":"2025-07-05T04:15:10","modified_gmt":"2025-07-05T03:15:10","slug":"freezer-monitor","status":"publish","type":"post","link":"https:\/\/www.billconnelly.net\/?p=375","title":{"rendered":"Freezer Monitor"},"content":{"rendered":"<p>We had a rash of freezers failing around the department, and I wasn&#8217;t going to become a victim. So I built something simple that has proven it&#8217;s worth. For a total cost of less than $100 you can sleep easy knowing your freezer door hasn&#8217;t been left open.<\/p>\n<p><!--more--><\/p>\n<p>The electronics are dead easy: You buy an <a href=\"https:\/\/www.adafruit.com\/product\/1782\">MCP9808 breakout board<\/a>, 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&#8217;s also probably a good idea to coat the breakout board in silicon sealant.<\/p>\n<p><a href=\"http:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/Temperature-Sensor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-376\" src=\"http:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/Temperature-Sensor.png\" alt=\"Temperature Sensor\" width=\"834\" height=\"646\" srcset=\"https:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/Temperature-Sensor.png 834w, https:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/Temperature-Sensor-300x232.png 300w\" sizes=\"(max-width: 834px) 100vw, 834px\" \/><\/a><\/p>\n<p>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 <span style=\"font-family: 'Lucida Console'\">sudo raspi-config<\/span>. It should be pretty obvious what to do: go down to advanced options, and SSH and I2C are both there.<\/p>\n<p>Now you need to install python support for the MCP9808. In the terminal, type<\/p>\n<pre class=\"prettyprint\">\r\nsudo apt-get update\r\nsudo apt-get install build-essential python-dev python-pip python-smbus git\r\nsudo pip install RPi.GPIO\r\ncd ~\r\ngit clone https:&#47;&#47;github.com&#47;adafruit&#47;Adafruit_Python_MCP9808.git\r\ncd Adafruit_Python_MCP9808\r\nsudo python setup.py install\r\n<\/pre>\n<p>With that done, confirm that it is all working with:<\/p>\n<pre class=\"prettyprint\">\r\ncd examples\r\nsudo python simpletest.py\r\n<\/pre>\n<p>Where you should see the temperature at the sensor printing out. Press Ctrl-C to quit.<\/p>\n<p>Now I suggest you set up a gmail address specifically for your freezer. It&#8217;s not hard. Also, we&#8217;re going to save the gmail password in a public folder, so you&#8217;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 <a href=\"https:\/\/www.google.com\/settings\/security\/lesssecureapps\">less secure apps<\/a> (I think).<\/p>\n<p>Get back to your home directory, and open a new file called read_temp.py <\/p>\n<pre class=\"prettyprint\">\r\ncd ~\r\nsudo nano read_temp.py\r\n<\/pre>\n<p>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).<\/p>\n<pre class=\"prettyprint linenums\">#!&#47;usr&#47;bin&#47;python\r\n\r\nimport time\r\nimport Adafruit_MCP9808.MCP9808 as MCP9808\r\nimport smtplib\r\nfrom collections import deque\r\nfrom email.mime.text import MIMEText\r\n\r\n#print &#39;**************************************&#39;\r\n#print &#39;-----EXECUTING TEMP SENSOR SCRIPT-----&#39;\r\n#print &#39;**************************************&#39;\r\n\r\n\r\n#Connect to i2c sensor with default pins (SCL=P9_19, SDA = P9_20)\r\nsensor = MCP9808.MCP9808()\r\n\r\n#Initialize sensor\r\nsensor.begin()\r\n\r\nemails = &#39;email1@gmail.com,email2@gmail.com,email3@gmail.com&#39; #comma separated values\r\n\r\ntemp_buffer = deque(maxlen=4) #4 position buffer for the last four reads\r\n\r\n\r\ndef send_mail(address, string):\r\n    #This should really be in a try statement\r\n        text = string\r\n        msg = MIMEText(text)\r\n        me = &#39;YOUREMAILADDRESS@gmail.com&#39;\r\n        them = address\r\n        msg&#091;&#39;Subject&#39;&#093; = &#39;Temperature Warning&#39;\r\n        msg&#091;&#39;From&#39;&#093; = me\r\n        msg&#091;&#39;To&#39;&#093; = them\r\n        mailserver = smtplib.SMTP(&#34;smtp.gmail.com&#34;, 587)\r\n        mailserver.ehlo()\r\n        mailserver.starttls()\r\n        mailserver.ehlo()\r\n        mailserver.login(&#34;YOUREMAILADDRESS@gmail.com&#34;, &#34;PASSWORD&#34;)\r\n        mailserver.sendmail(me, msg&#091;&#39;To&#39;&#093;.split(&#39;,&#39;), msg.as_string())\r\n        mailserver.close()\r\n        #print &#39;Email Sent&#39;\r\n\r\ndef mean(l): #The worlds silliest function\r\n    return sum(l)&#47;len(l)\r\n\r\ndef buf_to_str(buf):\r\n    string = &#34;&#34;\r\n    for el in buf:\r\n        string = string + str(el) + &#34;, &#34;\r\n    return string\r\n\r\ndef check_temp():\r\n    temp = sensor.readTempC()\r\n    temp_buffer.append(temp)\r\n    if mean(temp_buffer) &gt; -10:\r\n        string = &#34;The temperature is currently &#34; + str(temp) + &#34;. The last four readings have been &#34; + buf_to_str(temp_buffer) + &#34;. The time is &#34; + time.strftime(&#39;%H:%M:%S&#39;)\r\n        send_mail(emails, string)\r\n    time.sleep(60)\r\n\r\nwhile True:\r\n    check_temp()\r\n<\/pre>\n<p>Press Ctrl-X to exit, and confirm the save. Confirm that works properly with <span style=\"font-family: 'Lucida Console'\">sudo python read_temp.py<\/span>. <\/p>\n<p>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 <span style=\"font-family: 'Lucida Console'\">sudo nano launcher.sh<\/span>. Then type in the following:<\/p>\n<pre class=\"prettyprint\">\r\n#!&#47;bin&#47;sh\r\nsleep 10\r\nsudo python &#47;home&#47;pi&#47;read_temp.py\r\n<\/pre>\n<p>We need to set that file to be executable, so type <span style=\"font-family: 'Lucida Console'\">sudo chmod 777 launcher.sh<\/span>. Finally, we need to launch that file on startup. So enter <span style=\"font-family: 'Lucida Console'\">sudo nano \/etc\/rc.local<\/span> and at the end of that file (but before the <span style=\"font-family: 'Lucida Console'\">exit 0<\/span> line), type:<\/p>\n<pre class=\"prettyprint\">\r\n&#47;home&#47;pi&#47;launcher.sh &\r\n<\/pre>\n<p>Making sure that the final line of that file is <span style=\"font-family: 'Lucida Console'\">exit 0<\/span>. Press Ctrl-X to exit, and then save.<\/p>\n<p>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&#8217;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.<\/p>\n<div id=\"attachment_383\" style=\"width: 731px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/freezerThermometer.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-383\" src=\"http:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/freezerThermometer.jpg\" alt=\"Raspberry Pi mounted on the outside of the freezer. Inset: Silicon coated MCP9808 inside the freezer.\" width=\"721\" height=\"800\" class=\"size-full wp-image-383\" srcset=\"https:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/freezerThermometer.jpg 721w, https:\/\/www.billconnelly.net\/wp-content\/uploads\/2017\/02\/freezerThermometer-270x300.jpg 270w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/a><p id=\"caption-attachment-383\" class=\"wp-caption-text\">Raspberry Pi mounted on the outside of the freezer. Inset: Silicon coated MCP9808 inside the freezer.<\/p><\/div>\n<p>If you give this a try, and it&#8217;s not working, I&#8217;m happy to try to help, but I&#8217;m no Unix expert, so I can&#8217;t promise I&#8217;ll be any use.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We had a rash of freezers failing around the department, and I wasn&#8217;t going to become a victim. So I built something simple that has proven it&#8217;s worth. For a total cost of less than $100 you can sleep easy knowing your freezer door hasn&#8217;t been left open.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/posts\/375"}],"collection":[{"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=375"}],"version-history":[{"count":12,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":391,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/391"}],"wp:attachment":[{"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.billconnelly.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}