Breast Milk — or liquid gold as many moms attest — can be extremely difficult to generate and keep in order to create a consistent supply for your little one.
One afternoon, my wife called me in tears. She noticed that the chest freezer in our pantry suddenly quit working. Months worth of breast milk was ruined. She was understandably depressed. So much so, that I never mentioned that I lost a few important things too — three racks of ribs, a pork butt, and a whole box of mozzarella sticks.
Losing breast milk really sucks, but there are lots of stories about refrigerators and freezers malfunctioning with devastating consequences. In March of 2018, two fertility clinics lost frozen embryos within days of each other — first in Ohio, and then in San Francisco.
This tutorial will tell you exactly how to build a smart freezer thermometer that SMS/Texts and emails if it has a problem. You can make this fully-monitored reliable solution for temperature and humidity in a freezer or refrigerator at a cost of around 30 dollars. The system also monitors the temperature sensor acquisition device in case it malfunctions. With this double-redundancy, you can be confident that if temperatures start to deviate or the device stops working, you will be notified in plenty of time to make arrangements.
Can’t I just throw a wireless SmartThings sensor in the freezer? Unfortunately, No — alkaline batteries and lithium batteries do not work when frozen.
Refrigerators and Freezers
The only real difference between a working refrigerator and a freezer is temperature range. However, when they malfunction or a power outage occurs the timeframes for keeping food at a safe temperature varies widely, and it depends on what’s inside. Food is one thing, but what about certain medicines, vaccines, embryos, criminal DNA evidence, or even those cryogenic head-freezing services.
Did you know? A full freezer runs more efficiently and last longer in a power outage, because there’s less space for warm air to intrude when the door opens. Also, there is more frozen material to keep the space cooler longer.
- Refrigerator |Operating Temperature: 36°F to 40°F | Spoilage: 4 Hours
- Freezer | Temperature: -2°F to 0°F | Spoilage: up to 48 hours
note: Spoilage times vary widely if you open the door. Keep it closed if you can help it.
- Raspberry Pi Zero W — $10 Raspberry Pi, great for headless applications, because it’s small, cheap, and has integrated Wifi.
- MicroSD card — 16GB is enough for the operating system and code.
- BME280 — Integrated temperature and humidity sensor. You can find these for very cheap, but you’ll want it already integrated onto a breakout board, and it needs to expose the I2C lines for this tutorial code to work.
- Hammond Miniature ABS Enclosure — Model: 1551QGY — used to enclose and mount the sensor inside the freezer.
- Flat Flex Cable — TE Connectivity Part: A9AAT-0808F — This is crucial, we need an extremely flat cable to bridge the seal of the freezer without causing an air leak. In addition, the linked cable already has solder tabs at the proper spacing for standard through-holes on the Pi and the BME280 protoboard.
We are just going to wire the sensor directly to the Raspberry Pi — No extra components required.
- Modify the cable — The protoboard for the BME280 has 7 solder points, but the cable has 8 connectors. I used a utility knife to carefully strip of one conductor.
- Solder to the sensor board — this is where choosing the right flat flex cable comes in handy. The solder tabs already line up with the holes.
- Modify enclosure — I needed to Dremel away the lip of the mounting ridge for the cable to friction fit and exit the enclosure.
- Mark your signals — before closing the clamshell enclosure, it is a good idea to mark the signals on other end of the cable with a sharpie. We’ll need to locate the two wires for I²C : Clock — SCL and Data — SDA. Also locate your power pins and ground.
- Cut Flat Flex into “flying leads” — Slice each of the flat flex conductors you need into individual wires so they are flexible enough to go through the proper connection points.
- Solder — I soldered the leads right to the board, but there are lots of way to interface between the Raspberry Pi I/O and your flat flex leads.
- Install — The flat flex goes between the freezer seal and the edge of the freezer. Sensor on the inside — Pi on the out side. Use double sided Velcro or suction cups so that you can easily take the sensor in and out.
troubleshooting notes: (1) If you use the BME280 from Adafruit, you should solder in a jumper from CS to VIN to increase I²C reliability. (2) I connected VIN to the 5V pin on the Pi. This is not always the case because some BME280 breakout boards use the 3.3V.
Software: Raspberry Pi Setup
Our goal is to bring up the Raspberry Pi Zero W with a fresh operating system, and enable I2C.
- Get Raspbian Lite onto your MicroSD card using and SD card reader for your computer. Once you can access the SD card from a computer, use the imagers available from raspberry Pi to get a fresh image installed.
2. Enable SSH — After the imager finishes you should see a root directly on the SD card named
boot . Simply create an empty file called
ssh to enable ssh. Yeah that’s it for that.
3. Setup Wifi — create a simple text file and call it
wpa_supplicant.conf. Edit the file with your own wifi credentials like this:
4. Move your SD card from your PC to the socket on your raspberry pi and apply power. You need to wait about 30 seconds or so until your device connects to wifi.
Software: Get an SSH Terminal
In order to log in to your device, you’ll need to SSH in. First though, you need to find your Raspberry Pi IP address on your local network. There are a couple of ways of doing that from your computer:
- A fresh raspberryPi image will have a hostname of
raspberrypi. Try searching for that in your router device list.
- Open the terminal on your computer and try pinging it.
- If those don’t work take a deeper dive at the IP Address page on raspberrypi.org
Once you get your IP Address use SSH in the terminal to log in to your device.
pi is a default username and the default password is
Software: I²C Setup
I²C is not on by default and the fresh install of Raspbian Lite does not have all the drivers.
- Enable I²C in the OS by opening the configuration menu:
5 Interfacing Options,
P5 I2C, and select
<YES> to enable i2c. Select
<Finish> to save and exit.
2. Install smbus and i2c-tools
sudo apt-get install -y python-smbus i2c-tools
3. Verify installation using this command. If you see something like the output below, you are good.
lsmod | grep i2c_
4. This is the moment of truth! Test your connection to the I²C bus of the BME280 sensor. You should see an address at 0x76 or 0x77 like below. Take note of the address, we’ll need it later.
sudo i2cdetect -y 1
Software: Install Python Libraries
- First let’s get
pipto make everything easier.
pipcomes pre-installed on Raspbian full but not Raspbian lite.
sudo apt install python3-pip
2. Install the BME280 pre-made raspberry pi driver, which gives us quick access to the sensor readings in our code.
sudo pip3 install RPi.bme280
3. Install the Initial State streamer library which creates a simple data stream connection to Initial State for monitoring, alarming, and the dashboard.
sudo pip3 install ISStreamer
Software: Python Code
Now we are finally ready to drop some code. To start a new program on the Pi, use nano and copy/paste the code below:
User Settingsarea is for you to fill in your desired names and sampling. It is a good idea to use 10 minutes as a preset minimum for this use-case.
- You will need an access Key from Initial State. Register for a free trial, with no credit card required. The account access key has to be specified on line 10. Copy/paste this key from your Initial State account settings. (more info).
- You’ll also need to fill in the I²C Address we found after setting up and testing the bus. Edit line 16. Mine was 0x77, yours may be something else. I’ve seen 0x76 on other BME280 sensor setups.
- WHILE TESTING: Uncomment lines 36–37 and Comment line 40. I added some code in the bottom to test your implementation more efficiently. It reduces the sleep time so you see faster sensor reads and prints them to console.
- FINAL PRODUCT: Comment lines 36–37 and uncomment line 40.
Build a Web Dashboard
Go to your Initial State account (https://iot.app.initialstate.com), click on the “❄ Pantry Freezer” bucket name on your bucket shelf, and view the data coming into your dashboard. It’s live now, so go nuts… customize your dashboard by adding and sizing line graphs, gauges, statistics, images etc. (more info). Use expressions to round decimals and convert to degrees Fahrenheit if you like. Make it easy to read
Wanna see a public dashboard of my actual fridge? https://go.init.st/clht3qr
Brass Tacks: Alert me when temperature goes wrong
Monitoring on a dashboard is one thing, but the real value comes when we turn this into a smart thermometer alarm. We need to set an alert when the temperature drifts too high. SMS/Text and email coupled with a smart phone essentially deliver audible and visual alarms. This is done via triggers in Initial State. With a hosted IoT platform, the system can watch temperatures 24/7 and send you an email and a text when things go wrong.
Make it Reliable: Run on Startup and/or Reboot
To make our setup more robust, we need to kick off our Python script every time our Pi boots. This way the temperature monitor will restart without any intervention when we have a power outage.
Adding a process that runs on boot is really easy. At the Pi terminal:
It might ask which text editor you want to use (e.g. nano). Add one line to the bottom of the crontab file save and exit:
@reboot nohup python3 /home/pi/freezer_monitor/freezer_monitor.py &
You will need to specify the exact path of your script. In the example above, I created a directory called freezer_monitor and the script is called freezer_monitor.py.
Now your code will run headless any time your Pi reboots.
Make it Reliable: The Watchdog
Anything can happen, so you need a foolproof way of “monitoring the monitor.” We could do more process monitoring on the Pi itself, but the broadest way to make sure everything is running is to make sure the “heartbeat” of data is continuing to come at regular intervals from the device. If the device breaks, looses power, looses internet, or stops working, the Initial State Watchdog trigger can alert you in a timely fashion. Access the Watchdog Trigger in the Initial State Integration Marketplace.
The bucket watchdog counts incoming data points in the specified time interval. In the image above, I set the watchdog to watch my “Pantry Freezer” Bucket and return the count every 15 minutes. Since the code sends something every 10 minutes, we expect the number in the last 15 minutes to always be > 1. This logic becomes the trigger you make in your dashboard that ultimately sends you a text and/or email.
Note: The reason you set it to 1 and not 0 is because the watchdog itself sends one data point. If the devices stops sending, then that count will go down to one.
Monitoring refrigerators and freezers should be more common than it is. Shouldn’t my freezer just do this out of the box? Shouldn’t commercial units? Well until that day, use this tutorial to create a cheap redundant system for critical assets in just a few hours.
Ahhh… Although our family is through the breastfeeding phase, I can finally sleep at night knowing my two prime briskets from my last Costco run are safe and sound (and frozen)! My freezer alarm’s got me covered.