Monitoring Freezer Temperature with a Raspberry Pi and DHT22 Sensor Using Python 3+

Maintaining a consistent temperature in your freezer is crucial for preserving your food. A sudden rise in temperature can indicate a failure, which could lead to spoiled food and a mess to clean up. In this tutorial, we’ll show you how to use a Raspberry Pi and a DHT22 temperature sensor to monitor the temperature of your freezer and send an email alert if the temperature rises more than 5 degrees, indicating a potential failure.

What You’ll Need

  1. Raspberry Pi (any model with GPIO pins)
  2. DHT22 temperature and humidity sensor
  3. Breadboard and jumper wires
  4. 10k ohm resistor
  5. Python 3+ installed on your Raspberry Pi
  6. Internet connection for sending email alerts

Setting Up the Hardware

First, let’s connect the DHT22 sensor to the Raspberry Pi.

  1. Connect the VCC pin of the DHT22 to the 3.3V pin on the Raspberry Pi.
  2. Connect the GND pin of the DHT22 to a GND pin on the Raspberry Pi.
  3. Connect the DATA pin of the DHT22 to GPIO4 (Pin 7) on the Raspberry Pi.
  4. Place the 10k ohm resistor between the VCC and DATA pins of the DHT22 to act as a pull-up resistor.

Your setup should look something like this:

 DHT22        Raspberry Pi
 ---------    -------------
 VCC     -->  3.3V (Pin 1)
 GND     -->  GND (Pin 6)
 DATA    -->  GPIO4 (Pin 7)

Installing the Required Libraries

We’ll use the Adafruit DHT library to interface with the DHT22 sensor and the smtplib library to send email alerts. To install the Adafruit DHT library, run the following commands:

sudo apt-get update
sudo apt-get install python3-pip
pip3 install Adafruit_DHT

Writing the Python Script

Create a new Python script called freezer_monitor.py and open it in your favorite text editor.

import Adafruit_DHT
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time

# Define sensor type and GPIO pin
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

# Email setup
EMAIL_ADDRESS = "your_email@example.com"
EMAIL_PASSWORD = "your_email_password"
RECIPIENT_EMAIL = "recipient@example.com"
SMTP_SERVER = "smtp.example.com"
SMTP_PORT = 587

# Temperature threshold
TEMP_THRESHOLD = 5

def send_email_alert(current_temp, initial_temp):
    subject = "Freezer Temperature Alert"
    body = f"The temperature has risen more than {TEMP_THRESHOLD} degrees.\n"
    body += f"Initial Temperature: {initial_temp}°C\n"
    body += f"Current Temperature: {current_temp}°C\n"
    
    msg = MIMEMultipart()
    msg['From'] = EMAIL_ADDRESS
    msg['To'] = RECIPIENT_EMAIL
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))
    
    try:
        server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        server.starttls()
        server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
        text = msg.as_string()
        server.sendmail(EMAIL_ADDRESS, RECIPIENT_EMAIL, text)
        server.quit()
        print("Email sent successfully")
    except Exception as e:
        print(f"Failed to send email: {e}")

def main():
    initial_temp = None
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
        if temperature is not None:
            if initial_temp is None:
                initial_temp = temperature
            
            print(f"Temperature: {temperature:.2f}°C")
            if temperature - initial_temp > TEMP_THRESHOLD:
                send_email_alert(temperature, initial_temp)
                initial_temp = temperature  # Reset initial temp after alert
        
        time.sleep(300)  # Check every 5 minutes

if __name__ == "__main__":
    main()

Explanation

  1. Sensor Reading: We read the temperature from the DHT22 sensor using the Adafruit_DHT.read_retry() function.
  2. Email Alert: The send_email_alert() function sends an email if the temperature rises more than the threshold.
  3. Main Loop: The main() function continuously reads the temperature and checks if it has risen more than the specified threshold since the initial reading. If it has, an email alert is sent.

Running the Script

To run the script, execute the following command:

python3 freezer_monitor.py

This will start monitoring the freezer temperature and send an email alert if the temperature rises more than 5 degrees.

Conclusion

With this setup, you can ensure that you’ll be promptly alerted if your freezer starts to fail, giving you time to take action and save your food. This project showcases the power of the Raspberry Pi and Python in creating practical solutions for everyday problems.

Feel free to modify the script to suit your needs, such as changing the email server settings, adjusting the temperature threshold, or adding more sensors for a comprehensive monitoring system. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.