How to send mail where dth11 temperature critical raspberry pi

To send an email notification when the DHT11 temperature sensor on your Raspberry Pi reaches a critical temperature, you'll need to follow these steps:

Hardware Requirements:

Software Requirements:

Step 1: Install the dht11 library

Open a terminal on your Raspberry Pi and run the following command:

sudo pip3 install dht11

This will install the dht11 library, which allows you to read data from the DHT11 sensor.

Step 2: Write a Python script to read temperature data and send email

Create a new file called temperature_monitor.py and add the following code:

import dht11
import smtplib
from email.mime.text import MIMEText

# Set up the DHT11 sensor
dht = dht11.DHT11(pin=4)  # Pin 4 is the default pin for the DHT11 on Raspberry Pi

# Set up the email settings
SMTP_SERVER = "your_smtp_server.com"
SMTP_PORT = 587
FROM_EMAIL = "[email protected]"
TO_EMAIL = "[email protected]"
PASSWORD = "your_email_password"

# Set the critical temperature threshold
CRITICAL_TEMPERATURE = 30  # Celsius

while True:
    # Read the temperature data from the DHT11 sensor
    temperature = dht.read_temperature()
    if temperature is not None:
        # Check if the temperature is critical
        if temperature > CRITICAL_TEMPERATURE:
            # Send an email notification
            subject = "Temperature Critical: {}°C".format(temperature)
            body = "The temperature has reached a critical level of {}°C. Please take action to cool down the system.".format(temperature)
            msg = MIMEText(body)
            msg["Subject"] = subject
            msg["From"] = FROM_EMAIL
            msg["To"] = TO_EMAIL

            # Send the email using SMTP
            server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
            server.starttls()
            server.login(FROM_EMAIL, PASSWORD)
            server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
            server.quit()
    time.sleep(60)  # Check the temperature every 60 seconds

Replace the placeholders with your own values:

Step 3: Run the Python script

Save the temperature_monitor.py file and run it using the following command:

python3 temperature_monitor.py

The script will run indefinitely, checking the temperature every 60 seconds and sending an email notification if the temperature exceeds the critical threshold.

Note: Make sure to replace the placeholders with your own values and test the script before leaving it running unattended.